Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,51 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
os.
|
| 6 |
-
os.environ['SPACY_CACHE'] = '/tmp/spacy'
|
| 7 |
-
|
| 8 |
-
# 2. 在所有 import 之前,先確保 spacy 的模型已經下載完成
|
| 9 |
-
try:
|
| 10 |
-
import spacy
|
| 11 |
-
if not spacy.util.is_package("en_core_web_sm"):
|
| 12 |
-
os.system(f"{sys.executable} -m spacy download en_core_web_sm")
|
| 13 |
-
except Exception as e:
|
| 14 |
-
os.system(f"{sys.executable} -m spacy download en_core_web_sm")
|
| 15 |
|
| 16 |
import gradio as gr
|
| 17 |
from fastcoref import FCoref
|
|
|
|
| 18 |
|
| 19 |
-
#
|
| 20 |
model = FCoref()
|
| 21 |
|
| 22 |
def coref_chat(user_input):
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
clusters = preds[0].get_clusters()
|
| 25 |
|
| 26 |
if not clusters:
|
| 27 |
-
return user_input, "
|
| 28 |
|
| 29 |
-
|
|
|
|
| 30 |
for i, cluster in enumerate(clusters):
|
| 31 |
result_text += f"【群組 {i+1}】:{cluster}\n"
|
| 32 |
|
|
|
|
|
|
|
|
|
|
| 33 |
return user_input, result_text
|
| 34 |
|
| 35 |
# 建立介面
|
| 36 |
-
with gr.Blocks() as demo:
|
| 37 |
-
gr.Markdown("# 🤖 指代消解 AI 助理 (
|
|
|
|
| 38 |
with gr.Row():
|
| 39 |
-
txt_input = gr.Textbox(label="
|
| 40 |
-
submit_btn = gr.Button("開始分析")
|
| 41 |
-
txt_output = gr.Textbox(label="
|
|
|
|
| 42 |
submit_btn.click(fn=coref_chat, inputs=txt_input, outputs=[txt_input, txt_output])
|
| 43 |
|
| 44 |
if __name__ == "__main__":
|
|
|
|
| 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 |
model = FCoref()
|
| 13 |
|
| 14 |
def coref_chat(user_input):
|
| 15 |
+
# 1. 偵測是否包含中文字
|
| 16 |
+
is_chinese = any('\u4e00' <= char <= '\u9fff' for char in user_input)
|
| 17 |
+
|
| 18 |
+
process_text = user_input
|
| 19 |
+
if is_chinese:
|
| 20 |
+
# 中翻英
|
| 21 |
+
process_text = GoogleTranslator(source='auto', target='en').translate(user_input)
|
| 22 |
+
|
| 23 |
+
# 2. 執行指代消解
|
| 24 |
+
preds = model.predict(texts=[process_text])
|
| 25 |
clusters = preds[0].get_clusters()
|
| 26 |
|
| 27 |
if not clusters:
|
| 28 |
+
return user_input, "AI 沒能成功建立關聯。/ No clusters detected."
|
| 29 |
|
| 30 |
+
# 3. 整理輸出
|
| 31 |
+
result_text = f"📝 處理文本 (Processed): {process_text}\n\n🎯 AI 偵測到的連連看群組:\n"
|
| 32 |
for i, cluster in enumerate(clusters):
|
| 33 |
result_text += f"【群組 {i+1}】:{cluster}\n"
|
| 34 |
|
| 35 |
+
if is_chinese:
|
| 36 |
+
result_text += "\n💡 提示:此為中文橋接模式,AI 先將您的文字轉為英文進行語意邏輯分析。"
|
| 37 |
+
|
| 38 |
return user_input, result_text
|
| 39 |
|
| 40 |
# 建立介面
|
| 41 |
+
with gr.Blocks(title="中英雙語指代消解助理") as demo:
|
| 42 |
+
gr.Markdown("# 🤖 指代消解 AI 助理 (中英雙語支援版)")
|
| 43 |
+
gr.Markdown("您可以直接輸入中文或英文,系統會自動分析人物/物體關係。")
|
| 44 |
with gr.Row():
|
| 45 |
+
txt_input = gr.Textbox(label="輸入對話 (Input Text)", placeholder="例如:小明和老師在實驗室,她對他點了點頭。", lines=3)
|
| 46 |
+
submit_btn = gr.Button("開始分析 (Analyze)")
|
| 47 |
+
txt_output = gr.Textbox(label="分析結果 (Result)", lines=8)
|
| 48 |
+
|
| 49 |
submit_btn.click(fn=coref_chat, inputs=txt_input, outputs=[txt_input, txt_output])
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|