JJS341 commited on
Commit
b9c7347
·
verified ·
1 Parent(s): 2149274

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -15
app.py CHANGED
@@ -1,34 +1,53 @@
1
  import os
2
  import sys
3
- import gradio as gr
4
- from deep_translator import GoogleTranslator
5
 
6
- # 1. 先把 Spacy 下載好
7
  os.system(f"{sys.executable} -m spacy download en_core_web_sm")
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  from fastcoref import FCoref
 
10
 
11
- # 關鍵改動直接在全域變數下載並加載,這會強迫容器等它載完才顯示介面
12
- print("🚀 [System] 正在從遠端同步模型權重(約 300MB)...")
13
- # 不用 try-except 包裹,讓它在背景完整跑完
14
- model = FCoref(model_name='biu-nlp/f-coref', device='cpu')
15
- print("✅ [System] 模型加載成功,準備開放服務!")
 
 
 
 
 
16
 
17
  def coref_chat(user_input):
18
  if not user_input.strip():
19
  return "請輸入內容", "等待輸入..."
20
-
21
  try:
22
- # 執行翻譯與分析
23
  translated = GoogleTranslator(source='auto', target='en').translate(user_input)
 
24
  preds = model.predict(texts=[translated])
25
  clusters = preds[0].get_clusters()
26
 
27
- result = f"✨【跨語言橋接成功】\n解析空間 (English): {translated}\n"
 
28
  result += "---------------------------------\n"
29
 
30
  if not clusters:
31
- result += "🔍 AI 分析:關係明確,無需額外消解。"
32
  else:
33
  result += "🎯【偵測到之實體鏈】:\n"
34
  for i, cluster in enumerate(clusters):
@@ -40,9 +59,9 @@ def coref_chat(user_input):
40
  # 介面設定
41
  demo = gr.Interface(
42
  fn=coref_chat,
43
- inputs=gr.Textbox(label="輸入文本", lines=3),
44
- outputs=[gr.Textbox(label="原始輸入"), gr.Textbox(label="AI 語意報告")],
45
- title="Janice's AI 跨語言指代消解系統"
46
  )
47
 
48
  if __name__ == "__main__":
 
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. 解決相容性問題的補丁
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
+ @staticmethod
15
+ def save_token(token): pass
16
+ huggingface_hub.HfFolder = MockHfFolder
17
+ except:
18
+ pass
19
+
20
+ import gradio as gr
21
  from fastcoref import FCoref
22
+ from deep_translator import GoogleTranslator
23
 
24
+ # 關鍵修正將 model_name 改為 model_name_or_path
25
+ print("🚀 [System] 正在進行阻塞式權重同步(約 300MB)...")
26
+ try:
27
+ # 使用相容性最高的參數名稱
28
+ model = FCoref(model_name_or_path='biu-nlp/f-coref', device='cpu')
29
+ print("✅ [System] 模型載入完成,介面即將開啟。")
30
+ except TypeError:
31
+ # 萬一連上面的都不行,就用最原始的 positional argument
32
+ model = FCoref('biu-nlp/f-coref', device='cpu')
33
+ print("✅ [System] 使用原始參數模式載入完成。")
34
 
35
  def coref_chat(user_input):
36
  if not user_input.strip():
37
  return "請輸入內容", "等待輸入..."
 
38
  try:
39
+ # 語言橋接
40
  translated = GoogleTranslator(source='auto', target='en').translate(user_input)
41
+ # 執行消解
42
  preds = model.predict(texts=[translated])
43
  clusters = preds[0].get_clusters()
44
 
45
+ result = f"✨【跨語言語意橋接成功】\n"
46
+ result += f"解析路徑 (English): {translated}\n"
47
  result += "---------------------------------\n"
48
 
49
  if not clusters:
50
+ result += "🔍 分析結果指代關係明確,無需額外消解。"
51
  else:
52
  result += "🎯【偵測到之實體鏈】:\n"
53
  for i, cluster in enumerate(clusters):
 
59
  # 介面設定
60
  demo = gr.Interface(
61
  fn=coref_chat,
62
+ inputs=gr.Textbox(label="輸入文本", lines=3, placeholder="輸入中文或英文段落..."),
63
+ outputs=[gr.Textbox(label="原始輸入"), gr.Textbox(label="AI 語意分析報告")],
64
+ title="Janice's AI 跨語言指代消解系統 (Stable Version)"
65
  )
66
 
67
  if __name__ == "__main__":