Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,7 +33,7 @@ def get_extraction_config():
|
|
| 33 |
]
|
| 34 |
return prompt_description, examples
|
| 35 |
|
| 36 |
-
# --- 2. Gradio 的核心处理函数 (
|
| 37 |
def process_input_and_visualize(input_text, input_file):
|
| 38 |
source_text = ""
|
| 39 |
if input_file is not None:
|
|
@@ -53,9 +53,8 @@ def process_input_and_visualize(input_text, input_file):
|
|
| 53 |
if not api_key:
|
| 54 |
raise gr.Error("错误:服务器未配置 LANGEXTRACT_API_KEY。")
|
| 55 |
|
| 56 |
-
# --- 新增:自动重试逻辑 ---
|
| 57 |
max_retries = 3
|
| 58 |
-
retry_delay = 5
|
| 59 |
result = None
|
| 60 |
|
| 61 |
for attempt in range(max_retries):
|
|
@@ -67,25 +66,24 @@ def process_input_and_visualize(input_text, input_file):
|
|
| 67 |
examples=examples,
|
| 68 |
model_id="gemini-2.5-flash",
|
| 69 |
api_key=api_key,
|
| 70 |
-
|
|
|
|
|
|
|
| 71 |
extraction_passes=2,
|
| 72 |
max_char_buffer=1500
|
| 73 |
)
|
| 74 |
-
# 如果成功,跳出循环
|
| 75 |
print("API call successful.")
|
| 76 |
break
|
| 77 |
except Exception as e:
|
| 78 |
-
# 捕获所有 langextract 相关的运行时错误
|
| 79 |
print(f"Attempt #{attempt + 1} failed with error: {e}")
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
| 83 |
time.sleep(retry_delay)
|
| 84 |
else:
|
| 85 |
-
# 如果是其他错误,或已达到最大重试次数,则直接抛出异常
|
| 86 |
raise gr.Error(f"信息提取过程中发生错误: {e}")
|
| 87 |
|
| 88 |
-
# 如果所有重试都失败了,result 仍然会是 None,这里可以加一个检查
|
| 89 |
if result is None:
|
| 90 |
raise gr.Error("所有重试均失败,无法从API获取结果。请稍后再试。")
|
| 91 |
|
|
|
|
| 33 |
]
|
| 34 |
return prompt_description, examples
|
| 35 |
|
| 36 |
+
# --- 2. Gradio 的核心处理函数 (最终修复版) ---
|
| 37 |
def process_input_and_visualize(input_text, input_file):
|
| 38 |
source_text = ""
|
| 39 |
if input_file is not None:
|
|
|
|
| 53 |
if not api_key:
|
| 54 |
raise gr.Error("错误:服务器未配置 LANGEXTRACT_API_KEY。")
|
| 55 |
|
|
|
|
| 56 |
max_retries = 3
|
| 57 |
+
retry_delay = 5
|
| 58 |
result = None
|
| 59 |
|
| 60 |
for attempt in range(max_retries):
|
|
|
|
| 66 |
examples=examples,
|
| 67 |
model_id="gemini-2.5-flash",
|
| 68 |
api_key=api_key,
|
| 69 |
+
# --- 最终关键修复 ---
|
| 70 |
+
# 将并行工作线程数设为 1,强制顺序处理,以避免超出免费套餐的速率限制。
|
| 71 |
+
max_workers=1,
|
| 72 |
extraction_passes=2,
|
| 73 |
max_char_buffer=1500
|
| 74 |
)
|
|
|
|
| 75 |
print("API call successful.")
|
| 76 |
break
|
| 77 |
except Exception as e:
|
|
|
|
| 78 |
print(f"Attempt #{attempt + 1} failed with error: {e}")
|
| 79 |
+
# 同时处理 503 (服务器超载) 和 429 (速率限制) 错误
|
| 80 |
+
if ("503" in str(e) or "429" in str(e)) and attempt < max_retries - 1:
|
| 81 |
+
error_type = "503" if "503" in str(e) else "429"
|
| 82 |
+
print(f"API Error ({error_type}). Retrying in {retry_delay} seconds...")
|
| 83 |
time.sleep(retry_delay)
|
| 84 |
else:
|
|
|
|
| 85 |
raise gr.Error(f"信息提取过程中发生错误: {e}")
|
| 86 |
|
|
|
|
| 87 |
if result is None:
|
| 88 |
raise gr.Error("所有重试均失败,无法从API获取结果。请稍后再试。")
|
| 89 |
|