Hellowish commited on
Commit
2ddb6f3
·
verified ·
1 Parent(s): 908f774

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -18
app.py CHANGED
@@ -1,34 +1,84 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
 
3
 
4
  # 1. 載入 SQuAD v2.0 預訓練模型
 
5
  qa_model = pipeline("question-answering", model="deepset/roberta-base-squad2")
6
 
7
- # 2. 定義處理邏輯
8
- def predict(context, question):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  if not context or not question:
10
- return "請輸入文件內容。"
11
 
12
- # 執行問答
 
13
  result = qa_model(question=question, context=context)
14
 
15
- # 如果信心分數太低,回傳無法回答(SQuAD v2.0 特色)
16
  if result['score'] < 0.05:
17
- return "抱歉,在文件中找不到相關答案。"
 
 
 
 
 
 
 
18
 
19
- return result['answer']
 
 
 
 
 
 
 
 
20
 
21
- # 3. 建立 Gradio 網頁介面
22
- demo = gr.Interface(
23
- fn=predict,
24
- inputs=[
25
- gr.Textbox(lines=10, label="Context (文件內容)", placeholder="請貼上文件內容..."),
26
- gr.Textbox(lines=2, label="Question (提問)", placeholder="請問這份文件關於什麼?")
27
- ],
28
- outputs=gr.Textbox(label="Model Answer (模型回答)"),
29
- title="Case Study: Document QA System",
30
- description="根據提供的文本回答問題。"
31
- )
32
 
33
  if __name__ == "__main__":
34
  demo.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import pdfplumber
4
+ import docx
5
 
6
  # 1. 載入 SQuAD v2.0 預訓練模型
7
+ # 使用 deepset/roberta-base-squad2,它是針對 v2.0 優化的標準模型
8
  qa_model = pipeline("question-answering", model="deepset/roberta-base-squad2")
9
 
10
+ # 2. 定義文件讀取函式
11
+ def extract_text(file):
12
+ if file is None:
13
+ return ""
14
+
15
+ file_path = file.name
16
+ text = ""
17
+
18
+ # 處理 PDF
19
+ if file_path.endswith('.pdf'):
20
+ with pdfplumber.open(file_path) as pdf:
21
+ for page in pdf.pages:
22
+ text += page.extract_text() + "\n"
23
+
24
+ # 處理 Word (.docx)
25
+ elif file_path.endswith('.docx'):
26
+ doc = docx.Document(file_path)
27
+ for para in doc.paragraphs:
28
+ text += para.text + "\n"
29
+
30
+ # 處理純文字 (.txt)
31
+ elif file_path.endswith('.txt'):
32
+ with open(file_path, 'r', encoding='utf-8') as f:
33
+ text = f.read()
34
+
35
+ return text
36
+
37
+ # 3. 定義主預測邏輯
38
+ def predict(file, manual_context, question):
39
+ # 優先從上傳的文件提取內容,若無則使用手動輸入的內容
40
+ if file is not None:
41
+ context = extract_text(file)
42
+ else:
43
+ context = manual_context
44
+
45
  if not context or not question:
46
+ return "請先提供文件內容(上傳或貼上文字)並輸入提問。"
47
 
48
+ # 執行問答推理
49
+ # 加入 handle_impossible_answer=True 處理 SQuAD v2.0 特性
50
  result = qa_model(question=question, context=context)
51
 
52
+ # 信心門檻判斷
53
  if result['score'] < 0.05:
54
+ return "抱歉,在文件內容中找不到相關答案(模型信心程度較低)。"
55
+
56
+ return f"回答:{result['answer']}\n(信心分數: {round(result['score'], 4)})"
57
+
58
+ # 4. 建立 Gradio 網頁介面
59
+ with gr.Blocks(title="Case Study: AI Document QA") as demo:
60
+ gr.Markdown("# 📑 Case Study: 智慧文件問答系統")
61
+ gr.Markdown("利用語言模型進行文件自動化讀取與問答。")
62
 
63
+ with gr.Row():
64
+ with gr.Column():
65
+ file_input = gr.File(label="1. 上傳文件 (PDF, Word, TXT)")
66
+ text_input = gr.Textbox(lines=8, label="或是在此貼上文件內容", placeholder="若已上傳文件則無需填寫此處...")
67
+ question_input = gr.Textbox(lines=2, label="2. 輸入您的問題", placeholder="例如:這份文件的主要結論是什麼?")
68
+ submit_btn = gr.Button("開始分析", variant="primary")
69
+
70
+ with gr.Column():
71
+ answer_output = gr.Textbox(label="模型回答結果", lines=10)
72
 
73
+ # 綁定按鈕功能
74
+ submit_btn.click(
75
+ fn=predict,
76
+ inputs=[file_input, text_input, question_input],
77
+ outputs=answer_output
78
+ )
79
+
80
+ gr.Markdown("---")
81
+ gr.Markdown("💡 **提示:** 針對 SQuAD v2.0 資料集訓練的模型具備判斷『問題是否可回答』的能力。")
 
 
82
 
83
  if __name__ == "__main__":
84
  demo.launch()