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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -29
app.py CHANGED
@@ -3,63 +3,46 @@ import sys
3
  import gradio as gr
4
  from deep_translator import GoogleTranslator
5
 
6
- # 1. 安裝基礎套件 (這行是雲端保險)
7
  os.system(f"{sys.executable} -m spacy download en_core_web_sm")
8
 
9
  from fastcoref import FCoref
10
 
11
- # 2. 修模型載入方式:加上緩衝機制與明確宣告
12
- # 我們改用更精簡的初始化,不讓它背景偷偷做太多檢查
13
- try:
14
- print("🚀 啟動極速載入模式...")
15
- # 設定為低精度或簡化載入 (device='cpu' 是關鍵)
16
- model = FCoref(model_name='biu-nlp/f-coref', device='cpu')
17
- print("✅ 模型載入完成!")
18
- except Exception as e:
19
- print(f"❌ 載入異常: {e}")
20
- model = None
21
 
22
  def coref_chat(user_input):
23
  if not user_input.strip():
24
  return "請輸入內容", "等待輸入..."
25
-
26
- # 如果 model 還沒準備好,給出更具體的狀態
27
- if model is None:
28
- return user_input, "⚠️ 伺服器正在熱機中(模型權重下載中),請在 30 秒後重試一次。"
29
 
30
  try:
31
- # 語言橋接
32
- translator = GoogleTranslator(source='auto', target='en')
33
- translated = translator.translate(user_input)
34
-
35
- # 執行消解
36
  preds = model.predict(texts=[translated])
37
  clusters = preds[0].get_clusters()
38
 
39
- # 視覺化輸出
40
- result = f"✨【跨語言橋接成功】\n"
41
- result += f"解析空間 (English Context): {translated}\n"
42
  result += "---------------------------------\n"
43
 
44
  if not clusters:
45
- result += "🔍 AI 分析:這句話的代名詞與主體關係非常明確,模型判定不需額外消解。"
46
  else:
47
  result += "🎯【偵測到之實體鏈】:\n"
48
  for i, cluster in enumerate(clusters):
49
  result += f" 🔗 鏈結 {i+1}: {' ↔ '.join(cluster)}\n"
50
-
51
  return user_input, result
52
-
53
  except Exception as e:
54
  return user_input, f"運行錯誤: {str(e)}"
55
 
56
  # 介面設定
57
  demo = gr.Interface(
58
  fn=coref_chat,
59
- inputs=gr.Textbox(label="輸入文本", placeholder="輸入一段中文或英文...", lines=3),
60
  outputs=[gr.Textbox(label="原始輸入"), gr.Textbox(label="AI 語意報告")],
61
- title="Janice's AI 跨語言指代消解系統",
62
- description="本系統透過翻譯橋接技術,實現在雲端環境下的多語系實體追蹤。"
63
  )
64
 
65
  if __name__ == "__main__":
 
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):
35
  result += f" 🔗 鏈結 {i+1}: {' ↔ '.join(cluster)}\n"
 
36
  return user_input, result
 
37
  except Exception as e:
38
  return user_input, f"運行錯誤: {str(e)}"
39
 
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__":