Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
@@ -37,7 +37,7 @@ class PodcastGenerator:
|
|
37 |
gr.Error: 如果 API 金鑰或速率限制出現問題。
|
38 |
|
39 |
此方法使用 SambaNova API 根據使用者的輸入生成Podcast劇本。
|
40 |
-
|
41 |
"""
|
42 |
# 定義一個示例JSON結構,用於指導AI生成類似格式的Podcast劇本
|
43 |
example = """
|
@@ -268,6 +268,14 @@ class PodcastGenerator:
|
|
268 |
|
269 |
# 嘗試生成內容
|
270 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
271 |
logger.info(f"Sending request to SambaNova API with prompt: {user_prompt[:100]}...")
|
272 |
response = client.chat.completions.create(
|
273 |
model='Meta-Llama-3.1-405B-Instruct',
|
@@ -276,7 +284,7 @@ class PodcastGenerator:
|
|
276 |
{"role": "user", "content": user_prompt}
|
277 |
],
|
278 |
temperature=1,
|
279 |
-
max_tokens=
|
280 |
)
|
281 |
logger.info(f"Received response from API: {response}")
|
282 |
|
@@ -512,6 +520,12 @@ async def process_input(input_text: str, input_file, language: str, speaker1: st
|
|
512 |
gr.Error(f"Selected voices may not be compatible with the chosen language: {language}")
|
513 |
return None
|
514 |
|
|
|
|
|
|
|
|
|
|
|
|
|
515 |
# 如果提供了輸入檔案,則從檔案中提取文字
|
516 |
if input_file:
|
517 |
input_text = await TextExtractor.extract_text(input_file.name)
|
|
|
37 |
gr.Error: 如果 API 金鑰或速率限制出現問題。
|
38 |
|
39 |
此方法使用 SambaNova API 根據使用者的輸入生成Podcast劇本。
|
40 |
+
它處理語言選擇,使用適當配置設定 AI 模型,並處理生成的響應。
|
41 |
"""
|
42 |
# 定義一個示例JSON結構,用於指導AI生成類似格式的Podcast劇本
|
43 |
example = """
|
|
|
268 |
|
269 |
# 嘗試生成內容
|
270 |
try:
|
271 |
+
# Calculate the available tokens for generation
|
272 |
+
prompt_tokens = len(user_prompt.split()) # This is a rough estimate
|
273 |
+
system_tokens = len(system_prompt.split()) # This is a rough estimate
|
274 |
+
max_tokens = 4096 - prompt_tokens - system_tokens - 100 # 100 is a safety margin
|
275 |
+
|
276 |
+
if max_tokens <= 0:
|
277 |
+
return {"error": "Input prompt is too long. Please provide a shorter prompt."}
|
278 |
+
|
279 |
logger.info(f"Sending request to SambaNova API with prompt: {user_prompt[:100]}...")
|
280 |
response = client.chat.completions.create(
|
281 |
model='Meta-Llama-3.1-405B-Instruct',
|
|
|
284 |
{"role": "user", "content": user_prompt}
|
285 |
],
|
286 |
temperature=1,
|
287 |
+
max_tokens=max_tokens
|
288 |
)
|
289 |
logger.info(f"Received response from API: {response}")
|
290 |
|
|
|
520 |
gr.Error(f"Selected voices may not be compatible with the chosen language: {language}")
|
521 |
return None
|
522 |
|
523 |
+
# Check input text length
|
524 |
+
max_input_length = 1000 # Adjust this value as needed
|
525 |
+
if len(input_text) > max_input_length:
|
526 |
+
gr.Error(f"Input text is too long. Please limit your input to {max_input_length} characters.")
|
527 |
+
return None
|
528 |
+
|
529 |
# 如果提供了輸入檔案,則從檔案中提取文字
|
530 |
if input_file:
|
531 |
input_text = await TextExtractor.extract_text(input_file.name)
|