Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -260,21 +260,43 @@ def two_stage_qa(question, candidate_paragraphs_str, max_seq_len_mc=512, max_seq
|
|
260 |
return final_answer, f"選中的段落 (索引 {selected_idx}):\n{selected_paragraph}", predictions_dict
|
261 |
|
262 |
# --- 創建 Gradio 界面 ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
263 |
iface = gr.Interface(
|
264 |
-
fn=two_stage_qa,
|
265 |
inputs=[
|
266 |
-
gr.Textbox(
|
267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
268 |
],
|
269 |
outputs=[
|
270 |
gr.Textbox(label="預測答案 (Predicted Answer)"),
|
271 |
gr.Textbox(label="選中的相關段落 (Selected Relevant Paragraph)"),
|
272 |
-
gr.JSON(label="原始預測字典 (Raw Predictions Dict - for debugging)")
|
273 |
],
|
274 |
title="兩階段中文抽取式問答系統",
|
275 |
description="輸入一個問題和多個候選段落(每行一個段落)。系統會先選擇最相關的段落,然後從中抽取答案。",
|
276 |
-
allow_flagging="never"
|
277 |
)
|
278 |
|
279 |
if __name__ == "__main__":
|
280 |
-
|
|
|
|
|
|
|
|
260 |
return final_answer, f"選中的段落 (索引 {selected_idx}):\n{selected_paragraph}", predictions_dict
|
261 |
|
262 |
# --- 創建 Gradio 界面 ---
|
263 |
+
# 定義預設的問題和段落內容
|
264 |
+
DEFAULT_QUESTION = "世界最高峰是什麼?"
|
265 |
+
DEFAULT_PARAGRAPHS = (
|
266 |
+
"珠穆朗瑪峰是喜馬拉雅山脈的主峰,位於中國與尼泊爾邊界上,是世界海拔最高的山峰。\n"
|
267 |
+
"喬戈里峰,又稱K2,是喀喇崑崙山脈的主峰,海拔8611米,是世界第二高峰,位於中國與巴基斯坦邊界。\n"
|
268 |
+
"干城章嘉峰位於喜馬拉雅山脈中段尼泊爾和印度邊界線上,海拔8586米,為世界第三高峰。\n"
|
269 |
+
"洛子峰,海拔8516米,為世界第四高峰,位於珠穆朗瑪峰以南約3公里處,同屬喜馬拉雅山脈。"
|
270 |
+
)
|
271 |
+
|
272 |
iface = gr.Interface(
|
273 |
+
fn=two_stage_qa, # 您的兩階段問答處理函數
|
274 |
inputs=[
|
275 |
+
gr.Textbox(
|
276 |
+
lines=2,
|
277 |
+
placeholder="輸入您的問題...",
|
278 |
+
label="問題 (Question)",
|
279 |
+
value=DEFAULT_QUESTION # <--- 為問題設置預設值
|
280 |
+
),
|
281 |
+
gr.Textbox(
|
282 |
+
lines=10,
|
283 |
+
placeholder="在此處輸入候選段落,每段一行...",
|
284 |
+
label="候選段落 (Candidate Paragraphs - One per line)",
|
285 |
+
value=DEFAULT_PARAGRAPHS # <--- 為段落設置預設值
|
286 |
+
)
|
287 |
],
|
288 |
outputs=[
|
289 |
gr.Textbox(label="預測答案 (Predicted Answer)"),
|
290 |
gr.Textbox(label="選中的相關段落 (Selected Relevant Paragraph)"),
|
291 |
+
gr.JSON(label="原始預測字典 (Raw Predictions Dict - for debugging)")
|
292 |
],
|
293 |
title="兩階段中文抽取式問答系統",
|
294 |
description="輸入一個問題和多個候選段落(每行一個段落)。系統會先選擇最相關的段落,然後從中抽取答案。",
|
295 |
+
allow_flagging="never" # 或者您希望的標記設置
|
296 |
)
|
297 |
|
298 |
if __name__ == "__main__":
|
299 |
+
if models_loaded_successfully: # 確保模型已加載才啟動
|
300 |
+
iface.launch()
|
301 |
+
else:
|
302 |
+
print(f"Gradio 應用無法啟動,因為模型加載失敗: {error_message if 'error_message' in locals() else '未知錯誤'}")
|