Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,60 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
|
| 4 |
-
#
|
| 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
|
| 20 |
from deep_translator import GoogleTranslator
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
| 27 |
def coref_chat(user_input):
|
| 28 |
if not user_input.strip():
|
| 29 |
return "請輸入內容", "等待輸入..."
|
| 30 |
|
| 31 |
try:
|
| 32 |
-
#
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
preds = model.predict(texts=[
|
| 37 |
clusters = preds[0].get_clusters()
|
| 38 |
|
| 39 |
-
#
|
| 40 |
if not clusters:
|
| 41 |
-
return user_input, f"📝
|
| 42 |
|
| 43 |
-
|
| 44 |
-
result_text =
|
| 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 |
-
|
| 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.
|
| 60 |
gr.Markdown("# 🤖 Janice's AI Coreference Engine")
|
| 61 |
-
gr.Markdown("### 應用:跨語言實體追蹤與
|
| 62 |
|
| 63 |
with gr.Row():
|
| 64 |
-
txt_input = gr.Textbox(label="輸入待分析文本 (
|
| 65 |
-
txt_output = gr.Textbox(label="深度語意分析
|
| 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()
|