Yan1881 commited on
Commit
40b84e8
·
verified ·
1 Parent(s): 112e94c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -0
app.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import re
3
+ import json
4
+ import requests
5
+ import gradio as gr
6
+
7
+ HF_TOKEN = "hf_你的Token這邊要換掉"
8
+
9
+ task_options = ["重點整理", "問答", "翻譯"]
10
+
11
+ MODEL_URLS = {
12
+ "重點整理": {
13
+ "翻譯": "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-zh",
14
+ "摘要": "https://api-inference.huggingface.co/models/csebuetnlp/mT5_multilingual_XLSum"
15
+ },
16
+ "問答": "https://api-inference.huggingface.co/models/luhua/chinese_pretrain_mrc_roberta_wwm_ext_large",
17
+ "翻譯": "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-zh"
18
+ }
19
+
20
+ def clean_rtf(text):
21
+ text = re.sub(r"\'.", "", text)
22
+ text = re.sub(r"\[a-z]+[0-9]* ?", "", text)
23
+ text = re.sub(r"[{}]", "", text)
24
+ return text.strip()
25
+
26
+ def run_model(task, text, question=None):
27
+ headers = {
28
+ "Authorization": f"Bearer {HF_TOKEN}",
29
+ "Content-Type": "application/json"
30
+ }
31
+
32
+ if task == "重點整理":
33
+ translation_payload = {"inputs": text}
34
+ translation_url = MODEL_URLS["重點整理"]["翻譯"]
35
+ translation_response = requests.post(translation_url, headers=headers, json=translation_payload)
36
+
37
+ if translation_response.status_code != 200:
38
+ return "❌ 翻譯失敗:" + translation_response.text
39
+
40
+ try:
41
+ translated_text = translation_response.json()[0]['translation_text']
42
+ except Exception as e:
43
+ return f"❌ 翻譯結果解析錯誤:{str(e)}"
44
+
45
+ summarization_payload = {"inputs": f"summarize: {translated_text}"}
46
+ summarization_url = MODEL_URLS["重點整理"]["摘要"]
47
+ summarization_response = requests.post(summarization_url, headers=headers, json=summarization_payload)
48
+
49
+ if summarization_response.status_code != 200:
50
+ return "❌ 摘要失敗:" + summarization_response.text
51
+
52
+ try:
53
+ summary_text = summarization_response.json()[0]['summary_text']
54
+ except Exception as e:
55
+ return f"❌ 摘要結果解析錯誤:{str(e)}"
56
+
57
+ return summary_text
58
+
59
+ elif task == "問答":
60
+ if not question:
61
+ return "❌ 請輸入問題"
62
+
63
+ qa_payload = {
64
+ "inputs": {
65
+ "question": question,
66
+ "context": text
67
+ }
68
+ }
69
+ qa_url = MODEL_URLS["問答"]
70
+ qa_response = requests.post(qa_url, headers=headers, json=qa_payload)
71
+
72
+ if qa_response.status_code != 200:
73
+ return "❌ 問答失敗:" + qa_response.text
74
+
75
+ try:
76
+ answer = qa_response.json().get("answer", "⚠️ 找不到答案")
77
+ trans_payload = {"inputs": answer}
78
+ trans_url = MODEL_URLS["翻譯"]
79
+ trans_response = requests.post(trans_url, headers=headers, json=trans_payload)
80
+ if trans_response.status_code == 200:
81
+ return trans_response.json()[0]['translation_text']
82
+ else:
83
+ return answer
84
+ except Exception as e:
85
+ return f"❌ 回答解析錯誤:{str(e)}"
86
+
87
+ elif task == "翻譯":
88
+ translation_payload = {"inputs": text}
89
+ translation_url = MODEL_URLS["翻譯"]
90
+ translation_response = requests.post(translation_url, headers=headers, json=translation_payload)
91
+
92
+ if translation_response.status_code != 200:
93
+ return "❌ 翻譯失敗:" + translation_response.text
94
+
95
+ try:
96
+ return translation_response.json()[0]['translation_text']
97
+ except Exception as e:
98
+ return f"❌ 翻譯結果解析錯誤:{str(e)}"
99
+
100
+ else:
101
+ return "❌ 不支援的任務"
102
+
103
+ with gr.Blocks() as demo:
104
+ gr.Markdown("# 🌐 多功能語言處理器(繁體中文)\n支援:重點整理(英文→中)、問答、翻譯(英翻中)")
105
+
106
+ with gr.Row():
107
+ task = gr.Dropdown(choices=task_options, label="請選擇任務")
108
+
109
+ with gr.Row():
110
+ text_input = gr.Textbox(lines=10, label="輸入文章 / 內容")
111
+
112
+ with gr.Row():
113
+ question_input = gr.Textbox(label="問題(問答任務用)", placeholder="選擇問答任務時必填")
114
+
115
+ with gr.Row():
116
+ output = gr.Textbox(lines=5, label="輸出結果")
117
+
118
+ with gr.Row():
119
+ run_button = gr.Button("執行")
120
+
121
+ run_button.click(fn=run_model, inputs=[task, text_input, question_input], outputs=output)
122
+
123
+ if __name__ == "__main__":
124
+ demo.launch()