JJS341 commited on
Commit
c8bb4b7
·
verified ·
1 Parent(s): c5cbc82

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -57
app.py CHANGED
@@ -1,58 +1,58 @@
1
- import gradio as gr
2
- from fastcoref import FCoref
3
-
4
- # 1. 初始化指代消解模型
5
- print("正在載入 AI 模型,請稍候...")
6
- model = FCoref()
7
-
8
- def coref_chat(user_input):
9
- # 執行模型推論
10
- preds = model.predict(texts=[user_input])
11
- clusters = preds[0].get_clusters()
12
-
13
- if not clusters:
14
- return user_input, "模型未偵測到明確的指代群組。"
15
-
16
- # 整理輸出結果
17
- result_text = "🎯 AI 偵測到的連連看群組:\n"
18
- for i, cluster in enumerate(clusters):
19
- result_text += f"【群組 {i+1}】(指的是同一個主體):{cluster}\n"
20
-
21
- return user_input, result_text
22
-
23
- # 2. 建立 Gradio 網頁介面
24
- with gr.Blocks(title="指代消解 AI 助理") as demo:
25
- gr.Markdown("# 🤖 指代消解 (Coreference Resolution) 聊天機器人")
26
- gr.Markdown("輸入一段包含多個角色的對話,看 AI 如何幫你「連連看」!")
27
-
28
- with gr.Row():
29
- with gr.Column():
30
- txt_input = gr.Textbox(
31
- label="請輸入測試文本",
32
- placeholder="例如: Professor James and Mary are in the lab. She smiled at him.",
33
- lines=5
34
- )
35
- submit_btn = gr.Button("開始分析")
36
-
37
- with gr.Column():
38
- txt_original = gr.Textbox(label="你輸入的文字", interactive=False)
39
- txt_output = gr.Textbox(label="AI 分析結果", lines=8, interactive=False)
40
-
41
- submit_btn.click(
42
- fn=coref_chat,
43
- inputs=txt_input,
44
- outputs=[txt_original, txt_output]
45
- )
46
-
47
- gr.Examples(
48
- examples=[
49
- ["Professor James and his assistant Mary are in the lab. The woman told the man that the experiment was a success. She smiled at him."],
50
- ["Ben brought some pizza for his friends, and they all enjoyed it."]
51
- ],
52
- inputs=txt_input
53
- )
54
-
55
- # 3. 啟動機器人
56
- if __name__ == "__main__":
57
- # share=True 會產生一個臨時網址,你可以傳給老師或組員看
58
  demo.launch()
 
1
+ import os
2
+ os.system("python -m spacy download en_core_web_sm")
3
+
4
+ # 1. 初始化指代消解模型
5
+ print("正在載入 AI 模型,請稍候...")
6
+ model = FCoref()
7
+
8
+ def coref_chat(user_input):
9
+ # 執行模型推論
10
+ preds = model.predict(texts=[user_input])
11
+ clusters = preds[0].get_clusters()
12
+
13
+ if not clusters:
14
+ return user_input, "模型未偵測到明確的指代群組。"
15
+
16
+ # 整理輸出結果
17
+ result_text = "🎯 AI 偵測到的連連看群組:\n"
18
+ for i, cluster in enumerate(clusters):
19
+ result_text += f"【群組 {i+1}】(指的是同一個主體):{cluster}\n"
20
+
21
+ return user_input, result_text
22
+
23
+ # 2. 建立 Gradio 網頁介面
24
+ with gr.Blocks(title="指代消解 AI 助理") as demo:
25
+ gr.Markdown("# 🤖 指代消解 (Coreference Resolution) 聊天機器人")
26
+ gr.Markdown("輸入一段包含多個角色的對話,看 AI 如何幫你「連連看」!")
27
+
28
+ with gr.Row():
29
+ with gr.Column():
30
+ txt_input = gr.Textbox(
31
+ label="請輸入測試文本",
32
+ placeholder="例如: Professor James and Mary are in the lab. She smiled at him.",
33
+ lines=5
34
+ )
35
+ submit_btn = gr.Button("開始分析")
36
+
37
+ with gr.Column():
38
+ txt_original = gr.Textbox(label="你輸入的文字", interactive=False)
39
+ txt_output = gr.Textbox(label="AI 分析結果", lines=8, interactive=False)
40
+
41
+ submit_btn.click(
42
+ fn=coref_chat,
43
+ inputs=txt_input,
44
+ outputs=[txt_original, txt_output]
45
+ )
46
+
47
+ gr.Examples(
48
+ examples=[
49
+ ["Professor James and his assistant Mary are in the lab. The woman told the man that the experiment was a success. She smiled at him."],
50
+ ["Ben brought some pizza for his friends, and they all enjoyed it."]
51
+ ],
52
+ inputs=txt_input
53
+ )
54
+
55
+ # 3. 啟動機器人
56
+ if __name__ == "__main__":
57
+ # share=True 會產生一個臨時網址,你可以傳給老師或組員看
58
  demo.launch()