Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,93 +1,66 @@
|
|
| 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 |
-
#
|
|
|
|
| 12 |
try:
|
| 13 |
-
print("
|
|
|
|
| 14 |
model = FCoref(model_name='biu-nlp/f-coref', device='cpu')
|
|
|
|
| 15 |
except Exception as e:
|
| 16 |
-
print(f"
|
| 17 |
model = None
|
| 18 |
|
| 19 |
def coref_chat(user_input):
|
| 20 |
if not user_input.strip():
|
| 21 |
return "請輸入內容", "等待輸入..."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
try:
|
| 24 |
-
#
|
| 25 |
translator = GoogleTranslator(source='auto', target='en')
|
| 26 |
translated = translator.translate(user_input)
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
if not clusters:
|
| 40 |
-
result_text += "❌ 模型分析:未偵測到明確的人物指代關係。"
|
| 41 |
-
else:
|
| 42 |
-
result_text += "🎯【AI 偵測到的連連看群組】:\n"
|
| 43 |
-
for i, cluster in enumerate(clusters):
|
| 44 |
-
# 這裡讓輸出更華麗一點
|
| 45 |
-
result_text += f" 🔗 實體鏈 {i+1}: {' ↔ '.join(cluster)}\n"
|
| 46 |
-
|
| 47 |
-
# 增加技術價值的說明
|
| 48 |
-
result_text += "\n💡 技術分析:系統已將中文代名詞映射至英文實體,成功跨越語言障礙進行邏輯消解。"
|
| 49 |
else:
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
|
| 54 |
except Exception as e:
|
| 55 |
-
return user_input, f"
|
| 56 |
-
|
| 57 |
-
# 2. 執行指代消解
|
| 58 |
-
if model:
|
| 59 |
-
try:
|
| 60 |
-
preds = model.predict(texts=[translated])
|
| 61 |
-
clusters = preds[0].get_clusters()
|
| 62 |
-
|
| 63 |
-
if not clusters:
|
| 64 |
-
result_text = f"📝 翻譯文本:{translated}\n\n❌ 狀態:AI 未能建立實體連結。\n💡 建議:輸入關係更明確的句子,例如:'The doctor asked the nurse to help her.'"
|
| 65 |
-
else:
|
| 66 |
-
result_text = f"📝 英文語意路徑:{translated}\n\n🎯 【實體追蹤分析報告】:\n"
|
| 67 |
-
for i, cluster in enumerate(clusters):
|
| 68 |
-
result_text += f" ● 關聯群組 {i+1}: {' ↔ '.join(cluster)}\n"
|
| 69 |
-
|
| 70 |
-
# 方案三應用亮點
|
| 71 |
-
if "she" in translated.lower() and "doctor" in translated.lower():
|
| 72 |
-
result_text += "\n✨ [技術亮點] 成功鎖定女性職業主體,有效導正 AI 性別偏見。"
|
| 73 |
-
except Exception as e:
|
| 74 |
-
result_text = f"📝 翻譯成功:{translated}\n⚠️ 模型推論超時或資源不足: {str(e)}"
|
| 75 |
-
else:
|
| 76 |
-
result_text = f"📝 翻譯成功:{translated}\n⚠️ 核心模型初始化失敗,請檢查 requirements.txt。"
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
gr.
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
txt_input = gr.Textbox(label="請輸入中文或英文段落", lines=4, placeholder="例如:張醫生走進病房,她拿起了聽診器。")
|
| 87 |
-
txt_output = gr.Textbox(label="分析報告", lines=10)
|
| 88 |
-
|
| 89 |
-
btn = gr.Button("執行分析", variant="primary")
|
| 90 |
-
btn.click(fn=coref_chat, inputs=txt_input, outputs=[txt_input, txt_output])
|
| 91 |
|
| 92 |
if __name__ == "__main__":
|
| 93 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
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__":
|
| 66 |
demo.launch()
|