Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,80 +1,64 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
-
|
| 4 |
-
# 強制下載模型與補強環境
|
| 5 |
-
os.system(f"{sys.executable} -m pip install spacy")
|
| 6 |
-
os.system(f"{sys.executable} -m spacy download en_core_web_sm")
|
| 7 |
-
|
| 8 |
import gradio as gr
|
| 9 |
-
from fastcoref import FCoref
|
| 10 |
from deep_translator import GoogleTranslator
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def coref_chat(user_input):
|
| 16 |
if not user_input.strip():
|
| 17 |
return "請輸入內容", "等待輸入..."
|
| 18 |
|
| 19 |
-
# 1. 執行翻譯
|
| 20 |
try:
|
|
|
|
| 21 |
translated_text = GoogleTranslator(source='auto', target='en').translate(user_input)
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
if not clusters:
|
| 31 |
-
error_msg = (
|
| 32 |
-
f"📝 翻譯後的英文:{translated_text}\n\n"
|
| 33 |
-
"❌ 狀態:AI 未能建立實體連結。\n"
|
| 34 |
-
"💡 建議:嘗試輸入更長的段落,並明確提到『人名』或『職業』。例如:『教授和助理在聊天,他很開心,而她很忙。』"
|
| 35 |
-
)
|
| 36 |
-
return user_input, error_msg
|
| 37 |
-
|
| 38 |
-
# 4. 成功抓到群組,整理輸出 (方案三的對照展示)
|
| 39 |
-
result_text = f"📝 英文語意空間 (English space):\n{translated_text}\n\n"
|
| 40 |
-
result_text += "🎯 【實體追蹤結果 Entity Tracking】:\n"
|
| 41 |
-
|
| 42 |
-
for i, cluster in enumerate(clusters):
|
| 43 |
-
result_text += f" ● 群組 {i+1}: {' ↔ '.join(cluster)}\n"
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
with gr.Blocks(theme=gr.themes.
|
| 54 |
-
gr.Markdown("# 🤖 Janice's AI Coreference
|
| 55 |
-
gr.Markdown("###
|
| 56 |
|
| 57 |
with gr.Row():
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
submit_btn = gr.Button("執行深度語意分析", variant="primary")
|
| 65 |
-
|
| 66 |
-
with gr.Column():
|
| 67 |
-
txt_output = gr.Textbox(label="系統分析報告", lines=10)
|
| 68 |
-
|
| 69 |
gr.Examples(
|
| 70 |
-
examples=[
|
| 71 |
-
["教授走進實驗室,他在看燒瓶。助教過來了,她對他笑了笑。"],
|
| 72 |
-
["The surgeon performed the operation. She was very careful because her patient was weak."]
|
| 73 |
-
],
|
| 74 |
inputs=txt_input
|
| 75 |
)
|
| 76 |
-
|
| 77 |
-
submit_btn.click(fn=coref_chat, inputs=txt_input, outputs=[txt_input, txt_output])
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
| 80 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
from deep_translator import GoogleTranslator
|
| 5 |
|
| 6 |
+
# 補強環境套件
|
| 7 |
+
os.system(f"{sys.executable} -m pip install torch transformers")
|
| 8 |
+
|
| 9 |
+
from fastcoref import LingmessCoref
|
| 10 |
+
|
| 11 |
+
# 1. 預先下載並加載 Lingmess 模型 (這在雲端更穩定)
|
| 12 |
+
print("正在初始化深度語意模型...")
|
| 13 |
+
model = LingmessCoref(device='cpu')
|
| 14 |
|
| 15 |
def coref_chat(user_input):
|
| 16 |
if not user_input.strip():
|
| 17 |
return "請輸入內容", "等待輸入..."
|
| 18 |
|
|
|
|
| 19 |
try:
|
| 20 |
+
# 2. 翻譯橋接
|
| 21 |
translated_text = GoogleTranslator(source='auto', target='en').translate(user_input)
|
| 22 |
+
|
| 23 |
+
# 3. 執行預測
|
| 24 |
+
preds = model.predict(texts=[translated_text])
|
| 25 |
+
clusters = preds[0].get_clusters()
|
| 26 |
+
|
| 27 |
+
# 4. 如果還是沒抓到,列出除錯資訊
|
| 28 |
+
if not clusters:
|
| 29 |
+
return user_input, f"📝 翻譯文本:{translated_text}\n\n❌ 狀態:模型已運作但未偵測到明確指代。\n💡 提示:請嘗試輸入更明確的關係,例如:'The doctor met the nurse. She gave him a book.'"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# 5. 格式化結果
|
| 32 |
+
result_text = f"📝 英文語意路徑:{translated_text}\n\n"
|
| 33 |
+
result_text += "🎯 【指代消解群組 Coref Clusters】:\n"
|
| 34 |
+
for i, cluster in enumerate(clusters):
|
| 35 |
+
result_text += f" ● 群組 {i+1}: {' ↔ '.join(cluster)}\n"
|
| 36 |
+
|
| 37 |
+
# 方案三應用:偵測性別歧義修正
|
| 38 |
+
if "she" in translated_text.lower() and "he" in translated_text.lower():
|
| 39 |
+
result_text += "\n✨ [技術應用] 已成功解析跨性別主體關聯,可應用於消除翻譯軟體之偏見。"
|
| 40 |
+
|
| 41 |
+
return user_input, result_text
|
| 42 |
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return user_input, f"系統錯誤: {str(e)}"
|
| 45 |
|
| 46 |
+
# Gradio 介面設定
|
| 47 |
+
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
| 48 |
+
gr.Markdown("# 🤖 Janice's AI Coreference Engine")
|
| 49 |
+
gr.Markdown("### 應用:跨語言實體追蹤與翻譯去偏見測試")
|
| 50 |
|
| 51 |
with gr.Row():
|
| 52 |
+
txt_input = gr.Textbox(label="輸入待分析文本 (中/英)", lines=4)
|
| 53 |
+
txt_output = gr.Textbox(label="深度語意分析報告", lines=10)
|
| 54 |
+
|
| 55 |
+
btn = gr.Button("執行分析", variant="primary")
|
| 56 |
+
btn.click(fn=coref_chat, inputs=txt_input, outputs=[txt_input, txt_output])
|
| 57 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
gr.Examples(
|
| 59 |
+
examples=["The doctor asked the nurse to help her. He was busy.", "教授走進教室,學生們對他打招呼。"],
|
|
|
|
|
|
|
|
|
|
| 60 |
inputs=txt_input
|
| 61 |
)
|
|
|
|
|
|
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
demo.launch()
|