JJS341 commited on
Commit
c75af26
·
verified ·
1 Parent(s): c3efc1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -41
app.py CHANGED
@@ -1,76 +1,60 @@
1
  import os
2
  import sys
3
 
4
- # 1. 強制下載 Spacy 模型
5
  os.system(f"{sys.executable} -m spacy download en_core_web_sm")
6
 
7
- # 2. 防錯補丁:解決 huggingface_hub 版本太新導致的 ImportError
8
- try:
9
- import huggingface_hub
10
- if not hasattr(huggingface_hub, 'HfFolder'):
11
- class MockHfFolder:
12
- @staticmethod
13
- def get_token(): return os.getenv("HF_TOKEN")
14
- huggingface_hub.HfFolder = MockHfFolder
15
- except:
16
- pass
17
-
18
  import gradio as gr
19
- from fastcoref import LingmessCoref
20
  from deep_translator import GoogleTranslator
21
 
22
- # 3. 初始化 Lingmess 模型 (這行會跑比較久,因為要下載 Lingmess 權重)
23
- model = LingmessCoref(device='cpu')
24
-
25
-
26
 
27
  def coref_chat(user_input):
28
  if not user_input.strip():
29
  return "請輸入內容", "等待輸入..."
30
 
31
  try:
32
- # 2. 翻譯橋接
33
- translated_text = GoogleTranslator(source='auto', target='en').translate(user_input)
 
 
 
34
 
35
- # 3. 執行預測
36
- preds = model.predict(texts=[translated_text])
37
  clusters = preds[0].get_clusters()
38
 
39
- # 4. 如果還是沒抓到,列除錯資訊
40
  if not clusters:
41
- return user_input, f"📝 翻譯文本:{translated_text}\n\n❌ 狀態:模型已運作但偵測到明確指代。\n💡 提示:請嘗試輸入更明確的關係,例如:'The doctor met the nurse. She gave him a book.'"
42
 
43
- # 5. 格式化結果
44
- result_text = f"📝 英文語意路徑:{translated_text}\n\n"
45
- result_text += "🎯 【指代消解群組 Coref Clusters】:\n"
46
  for i, cluster in enumerate(clusters):
47
- result_text += f" ● 群組 {i+1}: {' ↔ '.join(cluster)}\n"
48
 
49
- # 方案三應用:偵測性別歧義修正
50
- if "she" in translated_text.lower() and "he" in translated_text.lower():
51
- result_text += "\n✨ [技術應用] 已成功解析跨性別主體關聯,可應用於消除翻譯軟體之偏見。"
52
 
53
  return user_input, result_text
54
 
55
  except Exception as e:
56
- return user_input, f"系統錯誤: {str(e)}"
57
 
58
  # Gradio 介面設定
59
- with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
60
  gr.Markdown("# 🤖 Janice's AI Coreference Engine")
61
- gr.Markdown("### 應用:跨語言實體追蹤與翻譯去偏見測試")
62
 
63
  with gr.Row():
64
- txt_input = gr.Textbox(label="輸入待分析文本 (中/英)", lines=4)
65
- txt_output = gr.Textbox(label="深度語意分析報告", lines=10)
66
 
67
- btn = gr.Button("執行分析", variant="primary")
68
  btn.click(fn=coref_chat, inputs=txt_input, outputs=[txt_input, txt_output])
69
 
70
- gr.Examples(
71
- examples=["The doctor asked the nurse to help her. He was busy.", "教授走進教室,學生們對他打招呼。"],
72
- inputs=txt_input
73
- )
74
-
75
  if __name__ == "__main__":
76
  demo.launch()
 
1
  import os
2
  import sys
3
 
4
+ # 環境:強制下載模型
5
  os.system(f"{sys.executable} -m spacy download en_core_web_sm")
6
 
 
 
 
 
 
 
 
 
 
 
 
7
  import gradio as gr
8
+ from fastcoref import FCoref
9
  from deep_translator import GoogleTranslator
10
 
11
+ # 初始化模型 (FCoref fastcoref 的標準入口)
12
+ # 它會自動處理 Lingmess 或其他預訓練權重
13
+ model = FCoref(device='cpu')
 
14
 
15
  def coref_chat(user_input):
16
  if not user_input.strip():
17
  return "請輸入內容", "等待輸入..."
18
 
19
  try:
20
+ # 1. 偵測並處理中文翻譯
21
+ is_chinese = any('\u4e00' <= char <= '\u9fff' for char in user_input)
22
+ process_text = user_input
23
+ if is_chinese:
24
+ process_text = GoogleTranslator(source='auto', target='en').translate(user_input)
25
 
26
+ # 2. 執行指代消解
27
+ preds = model.predict(texts=[process_text])
28
  clusters = preds[0].get_clusters()
29
 
30
+ # 3. 整理
31
  if not clusters:
32
+ return user_input, f"📝 處理文本:{process_text}\n\n❌ 狀態:AI 能建立實體連結。\n💡 建議:請輸入人物關係更明確的長句。"
33
 
34
+ result_text = f"📝 分析文本 (English Context): {process_text}\n\n"
35
+ result_text += "🎯 【實體追蹤分析報告 Entity Tracking】:\n"
 
36
  for i, cluster in enumerate(clusters):
37
+ result_text += f" ● 關聯群組 {i+1}: {' ↔ '.join(cluster)}\n"
38
 
39
+ if is_chinese:
40
+ result_text += "\n💡 提示:本分析基於『翻譯橋接技術』,成功將中文語意映射至英文邏輯空間。"
 
41
 
42
  return user_input, result_text
43
 
44
  except Exception as e:
45
+ return user_input, f"系統運行錯誤: {str(e)}"
46
 
47
  # Gradio 介面設定
48
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
49
  gr.Markdown("# 🤖 Janice's AI Coreference Engine")
50
+ gr.Markdown("### 專題應用:跨語言實體追蹤與去偏見分析層")
51
 
52
  with gr.Row():
53
+ txt_input = gr.Textbox(label="輸入待分析文本 (Input Text)", lines=4, placeholder="例如:張醫生正在動手術,她很冷靜。")
54
+ txt_output = gr.Textbox(label="深度語意分析結果", lines=10)
55
 
56
+ btn = gr.Button("執行分析 (Analyze)", variant="primary")
57
  btn.click(fn=coref_chat, inputs=txt_input, outputs=[txt_input, txt_output])
58
 
 
 
 
 
 
59
  if __name__ == "__main__":
60
  demo.launch()