JJS341 commited on
Commit
445ad48
·
verified ·
1 Parent(s): 7000d71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -39
app.py CHANGED
@@ -1,62 +1,45 @@
1
  import os
2
- os.system("python -m spacy download en_core_web_sm")
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  import gradio as gr
5
  from fastcoref import FCoref
6
 
7
-
8
- # 1. 初始化指代消解模型
9
- print("正在載入 AI 模型,請稍候...")
10
  model = FCoref()
11
 
12
  def coref_chat(user_input):
13
- # 執行模型推論
14
  preds = model.predict(texts=[user_input])
15
  clusters = preds[0].get_clusters()
16
 
17
  if not clusters:
18
  return user_input, "模型未偵測到明確的指代群組。"
19
 
20
- # 整理輸出結果
21
  result_text = "🎯 AI 偵測到的連連看群組:\n"
22
  for i, cluster in enumerate(clusters):
23
- result_text += f"【群組 {i+1}】(指的是同一個主體):{cluster}\n"
24
 
25
  return user_input, result_text
26
 
27
- # 2. 建立 Gradio 網頁介面
28
- with gr.Blocks(title="指代消解 AI 助理") as demo:
29
- gr.Markdown("# 🤖 指代消解 (Coreference Resolution) 聊天機器人")
30
- gr.Markdown("輸入一段包含多個角色的對話,看 AI 如何幫你「連連看」!")
31
-
32
  with gr.Row():
33
- with gr.Column():
34
- txt_input = gr.Textbox(
35
- label="請輸入測試文本",
36
- placeholder="例如: Professor James and Mary are in the lab. She smiled at him.",
37
- lines=5
38
- )
39
- submit_btn = gr.Button("開始分析")
40
-
41
- with gr.Column():
42
- txt_original = gr.Textbox(label="你輸入的文字", interactive=False)
43
- txt_output = gr.Textbox(label="AI 分析結果", lines=8, interactive=False)
44
-
45
- submit_btn.click(
46
- fn=coref_chat,
47
- inputs=txt_input,
48
- outputs=[txt_original, txt_output]
49
- )
50
-
51
- gr.Examples(
52
- examples=[
53
- ["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."],
54
- ["Ben brought some pizza for his friends, and they all enjoyed it."]
55
- ],
56
- inputs=txt_input
57
- )
58
 
59
- # 3. 啟動機器人
60
  if __name__ == "__main__":
61
- # share=True 會產生一個臨時網址,你可以傳給老師或組員看
62
  demo.launch()
 
1
  import os
2
+ import sys
3
+
4
+ # 1. 強制修正環境變數,避免雲端安裝模型時權限不足
5
+ os.environ['TRANSFORMERS_CACHE'] = '/tmp/huggingface'
6
+ os.environ['SPACY_CACHE'] = '/tmp/spacy'
7
+
8
+ # 2. 在所有 import 之前,先確保 spacy 的模型已經下載完成
9
+ try:
10
+ import spacy
11
+ if not spacy.util.is_package("en_core_web_sm"):
12
+ os.system(f"{sys.executable} -m spacy download en_core_web_sm")
13
+ except Exception as e:
14
+ os.system(f"{sys.executable} -m spacy download en_core_web_sm")
15
 
16
  import gradio as gr
17
  from fastcoref import FCoref
18
 
19
+ # 3. 初始化模型 (這裡建議直接在啟動時下載)
 
 
20
  model = FCoref()
21
 
22
  def coref_chat(user_input):
 
23
  preds = model.predict(texts=[user_input])
24
  clusters = preds[0].get_clusters()
25
 
26
  if not clusters:
27
  return user_input, "模型未偵測到明確的指代群組。"
28
 
 
29
  result_text = "🎯 AI 偵測到的連連看群組:\n"
30
  for i, cluster in enumerate(clusters):
31
+ result_text += f"【群組 {i+1}】:{cluster}\n"
32
 
33
  return user_input, result_text
34
 
35
+ # 建立介面
36
+ with gr.Blocks() as demo:
37
+ gr.Markdown("# 🤖 指代消解 AI 助理 (Cloud Version)")
 
 
38
  with gr.Row():
39
+ txt_input = gr.Textbox(label="請輸入測試文本", lines=3)
40
+ submit_btn = gr.Button("開始分析")
41
+ txt_output = gr.Textbox(label="AI 分析結果", lines=5)
42
+ submit_btn.click(fn=coref_chat, inputs=txt_input, outputs=[txt_input, txt_output])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
 
44
  if __name__ == "__main__":
 
45
  demo.launch()