Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -35,37 +35,43 @@ except TypeError:
|
|
| 35 |
def coref_chat(user_input):
|
| 36 |
if not user_input.strip():
|
| 37 |
return "請輸入內容", "等待輸入..."
|
|
|
|
| 38 |
try:
|
| 39 |
-
#
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
translated = translator.translate(user_input)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
# 執行消解
|
| 49 |
-
preds = model.predict(texts=[
|
| 50 |
clusters = preds[0].get_clusters()
|
| 51 |
|
| 52 |
-
|
| 53 |
-
result
|
|
|
|
| 54 |
result += "---------------------------------\n"
|
| 55 |
|
| 56 |
if not clusters:
|
| 57 |
-
result += "🔍 分析結果:
|
| 58 |
-
result += "\n💡 建議測試句:'The doctor asked the nurse to help her. He was busy.'"
|
| 59 |
else:
|
| 60 |
-
result += "🎯【偵測到之實體鏈
|
| 61 |
for i, cluster in enumerate(clusters):
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
result += "
|
| 67 |
|
| 68 |
return user_input, result
|
|
|
|
| 69 |
except Exception as e:
|
| 70 |
return user_input, f"運行錯誤: {str(e)}"
|
| 71 |
|
|
|
|
| 35 |
def coref_chat(user_input):
|
| 36 |
if not user_input.strip():
|
| 37 |
return "請輸入內容", "等待輸入..."
|
| 38 |
+
|
| 39 |
try:
|
| 40 |
+
# 1. 偵測語系並統一轉換為「英文」供模型運算
|
| 41 |
+
# 判斷是否含有中文字
|
| 42 |
+
has_chinese = any('\u4e00' <= char <= '\u9fff' for char in user_input)
|
|
|
|
| 43 |
|
| 44 |
+
if has_chinese:
|
| 45 |
+
# 中翻英 (橋接運算)
|
| 46 |
+
working_text = GoogleTranslator(source='zh-CN', target='en').translate(user_input)
|
| 47 |
+
mode_notice = "【模式:中文輸入 ➔ 英文解析】"
|
| 48 |
+
else:
|
| 49 |
+
# 已經是英文,直接運算
|
| 50 |
+
working_text = user_input
|
| 51 |
+
mode_notice = "【模式:英文原語解析】"
|
| 52 |
|
| 53 |
+
# 2. 執行指代消解 (Coreference Resolution)
|
| 54 |
+
preds = model.predict(texts=[working_text])
|
| 55 |
clusters = preds[0].get_clusters()
|
| 56 |
|
| 57 |
+
# 3. 整理結果並「翻譯回中文」
|
| 58 |
+
result = f"✨ {mode_notice}\n"
|
| 59 |
+
result += f"📝 英文邏輯空間: {working_text}\n"
|
| 60 |
result += "---------------------------------\n"
|
| 61 |
|
| 62 |
if not clusters:
|
| 63 |
+
result += "🔍 分析結果:指代關係明確,或模型判定關聯度未達門檻。"
|
|
|
|
| 64 |
else:
|
| 65 |
+
result += "🎯【偵測到之實體鏈】:\n"
|
| 66 |
for i, cluster in enumerate(clusters):
|
| 67 |
+
# 將英文實體鏈翻譯回中文,讓使用者更好懂
|
| 68 |
+
cluster_str = ' ↔ '.join(cluster)
|
| 69 |
+
translated_cluster = GoogleTranslator(source='en', target='zh-CN').translate(cluster_str)
|
| 70 |
+
result += f" 🔗 鏈結 {i+1} (中): {translated_cluster}\n"
|
| 71 |
+
result += f" └─ (英): {cluster_str}\n"
|
| 72 |
|
| 73 |
return user_input, result
|
| 74 |
+
|
| 75 |
except Exception as e:
|
| 76 |
return user_input, f"運行錯誤: {str(e)}"
|
| 77 |
|