Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 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 |
|
|
@@ -9,45 +9,70 @@ import gradio as gr
|
|
| 9 |
from fastcoref import FCoref
|
| 10 |
from deep_translator import GoogleTranslator
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# 初始化英文核心大腦
|
| 15 |
model = FCoref()
|
| 16 |
|
| 17 |
def coref_chat(user_input):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
# 2. 執行指代消解
|
| 27 |
-
preds = model.predict(texts=[
|
| 28 |
clusters = preds[0].get_clusters()
|
| 29 |
|
|
|
|
| 30 |
if not clusters:
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
# 3. 整理輸出
|
| 34 |
-
result_text = f"📝 處理文本 (Processed): {process_text}\n\n🎯 AI 偵測到的連連看群組:\n"
|
| 35 |
for i, cluster in enumerate(clusters):
|
| 36 |
-
result_text += f"
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# 建立介面
|
| 44 |
-
with gr.Blocks(
|
| 45 |
-
gr.Markdown("# 🤖
|
| 46 |
-
gr.Markdown("
|
|
|
|
| 47 |
with gr.Row():
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
submit_btn.click(fn=coref_chat, inputs=txt_input, outputs=[txt_input, txt_output])
|
| 53 |
|
|
|
|
| 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 |
|
|
|
|
| 9 |
from fastcoref import FCoref
|
| 10 |
from deep_translator import GoogleTranslator
|
| 11 |
|
| 12 |
+
# 初始化模型
|
|
|
|
|
|
|
| 13 |
model = FCoref()
|
| 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 |
+
except Exception as e:
|
| 23 |
+
return user_input, f"翻譯失敗: {str(e)}"
|
| 24 |
+
|
| 25 |
# 2. 執行指代消解
|
| 26 |
+
preds = model.predict(texts=[translated_text])
|
| 27 |
clusters = preds[0].get_clusters()
|
| 28 |
|
| 29 |
+
# 3. 處理「沒抓到群組」的情況
|
| 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 |
+
bias_check = ""
|
| 47 |
+
if "she" in translated_text.lower() or "her" in translated_text.lower():
|
| 48 |
+
bias_check = "\n✨ [技術亮點] 偵測到女性代名詞,系統已成功鎖定實體對象,避免翻譯偏見。"
|
| 49 |
+
|
| 50 |
+
return user_input, result_text + bias_check
|
| 51 |
|
| 52 |
# 建立介面
|
| 53 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 54 |
+
gr.Markdown("# 🤖 Janice's AI Coreference Assistant")
|
| 55 |
+
gr.Markdown("### 專題應用:跨語言實體追蹤與翻譯去偏見測試層")
|
| 56 |
+
|
| 57 |
with gr.Row():
|
| 58 |
+
with gr.Column():
|
| 59 |
+
txt_input = gr.Textbox(
|
| 60 |
+
label="請輸入中文或英文段落",
|
| 61 |
+
placeholder="例如:醫生走進病房,她拿起了聽診器,他在旁邊紀錄。",
|
| 62 |
+
lines=5
|
| 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 |
|