dean22029 commited on
Commit
0bfe5de
·
1 Parent(s): 0e93045

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -11
app.py CHANGED
@@ -1,24 +1,38 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
  # 加載預訓練模型
5
- model = pipeline("text2text-generation", model="dean22029/mt5_finetuning")
6
 
7
- def predict_label(text):
8
- # 生成預測
9
- predictions = model(text)
10
- # 通常,text2text-generation 模型的輸出是一個列表,其中包含字典。我們需要從這個字典中提取 'generated_text' 鍵的值。
11
- return predictions[0]['generated_text']
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  # 建立 Gradio 界面
14
  interface = gr.Interface(
15
  fn=predict_label,
16
  inputs=gr.Textbox(lines=2, placeholder="輸入您想分析的文字..."),
17
- outputs="text",
18
- theme="huggingface",
19
- title="議員對話分類器",
20
- description="這是一個議員對話的分類器。請在下方輸入框中輸入對話文字,模型將預測其類別。",
21
- allow_flagging="never"
 
22
  )
23
 
24
  # 啟動界面
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import time
4
 
5
  # 加載預訓練模型
6
+ model = pipeline("text-classification", model="dean22029/Taiwan_Legislator_multilabel_classification")
7
 
8
+ label_mapping = {'LABEL_0': '要求資訊', 'LABEL_1': '要求說明', 'LABEL_2': '要求改變', 'LABEL_3': '威脅制裁'}
9
+
10
+ def predict_label(text, progress=gr.Progress()):
11
+ progress(0, desc="開始處理")
12
+ time.sleep(1) # 模擬處理延遲
13
+ progress(0.5, desc="正在分析文本")
14
+ predictions = model(text)
15
+ progress(1, desc="完成分析")
16
+
17
+ # 提取標籤
18
+ labels = [label_mapping.get(prediction['label']) for prediction in predictions if prediction['score'] > 0.5]
19
+ return ", ".join(labels) if labels else "無相關標籤"
20
+
21
+ custom_html = """
22
+ <h3>歡迎使用議員對話分類器</h3>
23
+ <p>是不是很好奇選區的議員到底都在議會說了什麼? 請輸入議員的逐字稿, 讓我們來幫你解答</p>
24
+ """
25
 
26
  # 建立 Gradio 界面
27
  interface = gr.Interface(
28
  fn=predict_label,
29
  inputs=gr.Textbox(lines=2, placeholder="輸入您想分析的文字..."),
30
+ outputs="label",
31
+ theme="huggingface", # 自定義主題風格
32
+ title="議員對話分類器", # 界面標題
33
+ description="這是一個議員對話的分類器。請在下方輸入框中輸入對話文字,模型將預測其類別。", # 說明文字
34
+ allow_flagging="never",
35
+ examples=[["接下來想針對大巨蛋問題詢問一下目前進度,是這樣子的,之前你們都在說大巨蛋會蓋好,所以到底什麼時候會蓋好呢?怎麼拖這麼久?"]]
36
  )
37
 
38
  # 啟動界面