Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,26 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import sys
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
from deep_translator import GoogleTranslator
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
# 載入 Lingmess 模型 (這是 fastcoref 的核心模型)
|
| 13 |
-
# 我們改用更底層的載入方式,確保穩定性
|
| 14 |
-
model_name = "biu-nlp/lingmess-coref"
|
| 15 |
-
try:
|
| 16 |
-
from fastcoref import FCoref
|
| 17 |
-
# 強制指定使用較小的 bert 模型,而不是大型的 lingmess
|
| 18 |
-
model = FCoref(model_name='biu-nlp/f-coref', device='cpu')
|
| 19 |
-
except Exception as e:
|
| 20 |
-
print(f"模型載入失敗: {e}")
|
| 21 |
-
model = None
|
| 22 |
|
| 23 |
def coref_chat(user_input):
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
# 2. 執行指代消解 (Coreference Resolution)
|
| 34 |
-
if model:
|
| 35 |
-
try:
|
| 36 |
-
preds = model.predict(texts=[translated_text])
|
| 37 |
-
clusters = preds[0].get_clusters()
|
| 38 |
-
|
| 39 |
-
if not clusters:
|
| 40 |
-
result_text = f"📝 翻譯文本:{translated_text}\n\n❌ 狀態:AI 未能建立實體連結。\n💡 建議:請輸入人物關係更明確的長句。"
|
| 41 |
-
else:
|
| 42 |
-
result_text = f"📝 英文語意路徑:{translated_text}\n\n🎯 【實體追蹤分析結果】:\n"
|
| 43 |
-
for i, cluster in enumerate(clusters):
|
| 44 |
-
result_text += f" ● 群組 {i+1}: {' ↔ '.join(cluster)}\n"
|
| 45 |
-
|
| 46 |
-
# 方案三:展示應用價值
|
| 47 |
-
if "she" in translated_text.lower() and ("doctor" in translated_text.lower() or "surgeon" in translated_text.lower()):
|
| 48 |
-
result_text += "\n✨ [技術亮點] 系統成功辨識女性職業身份,已自動校正翻譯偏見。"
|
| 49 |
-
except Exception as e:
|
| 50 |
-
result_text = f"📝 翻譯成功:{translated_text}\n⚠️ 模型推論失敗,可能是記憶體不足。錯誤代碼: {str(e)}"
|
| 51 |
-
else:
|
| 52 |
-
result_text = f"📝 翻譯成功:{translated_text}\n⚠️ 核心模型初始化失敗,目前僅能展示翻譯橋接邏輯。"
|
| 53 |
-
|
| 54 |
-
return user_input, result_text
|
| 55 |
|
| 56 |
# Gradio 介面
|
| 57 |
with gr.Blocks(theme=gr.themes.Default()) as demo:
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
from deep_translator import GoogleTranslator
|
| 4 |
|
| 5 |
+
# 這裡填寫 Hugging Face 的 API 網址 (免費限額內可使用)
|
| 6 |
+
API_URL = "https://api-inference.huggingface.co/models/biu-nlp/lingmess-coref"
|
| 7 |
+
# 妳可以在 HF 的 Settings 申請一組 Token 填入
|
| 8 |
+
headers = {"Authorization": "Bearer 妳的_HF_TOKEN"}
|
| 9 |
|
| 10 |
+
def query(payload):
|
| 11 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 12 |
+
return response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def coref_chat(user_input):
|
| 15 |
+
# 1. 翻譯橋接 (同前)
|
| 16 |
+
translated = GoogleTranslator(source='auto', target='en').translate(user_input)
|
| 17 |
+
|
| 18 |
+
# 2. 呼叫遠端大腦
|
| 19 |
+
# 注意:API 回傳格式與本地套件不同,需要解析
|
| 20 |
+
output = query({"inputs": translated})
|
| 21 |
+
|
| 22 |
+
# 這裡直接回傳翻譯結果與 AI 解析數據
|
| 23 |
+
return user_input, f"📝 翻譯:{translated}\n\n🎯 遠端分析數據:{output}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Gradio 介面
|
| 26 |
with gr.Blocks(theme=gr.themes.Default()) as demo:
|