Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,14 +18,18 @@ except:
|
|
| 18 |
pass
|
| 19 |
|
| 20 |
import gradio as gr
|
|
|
|
| 21 |
from fastcoref import FCoref
|
| 22 |
from deep_translator import GoogleTranslator
|
| 23 |
|
| 24 |
-
# 3. 初始化
|
|
|
|
|
|
|
|
|
|
| 25 |
print("🚀 [System] 正在初始化 F-Coref 核心大腦...")
|
| 26 |
try:
|
| 27 |
model = FCoref(model_name_or_path='biu-nlp/f-coref', device='cpu')
|
| 28 |
-
print("✅ [System] 模型加載成功!")
|
| 29 |
except:
|
| 30 |
model = FCoref('biu-nlp/f-coref', device='cpu')
|
| 31 |
print("✅ [System] 備用路徑加載成功!")
|
|
@@ -52,30 +56,28 @@ def coref_learning_pipeline(user_input):
|
|
| 52 |
# C. 欄位一:生成「英翻中結果」
|
| 53 |
translation_text = GoogleTranslator(source='en', target='zh-TW').translate(working_text)
|
| 54 |
|
| 55 |
-
# D. 欄位二:
|
| 56 |
vocab_output = ""
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
else:
|
| 76 |
-
vocab_output = "ℹ️ 本句的核心實體多為代名詞,未偵測到適合學習的高階名詞。"
|
| 77 |
else:
|
| 78 |
-
vocab_output = "
|
| 79 |
|
| 80 |
# E. 欄位三:生成「AI 語意共指報告」
|
| 81 |
report_text = f"✨ 系統狀態:{mode_notice}\n"
|
|
@@ -100,26 +102,25 @@ def coref_learning_pipeline(user_input):
|
|
| 100 |
except Exception as e:
|
| 101 |
return f"錯誤: {str(e)}", "無法整合單字", f"運行異常: {str(e)}"
|
| 102 |
|
| 103 |
-
# 5. 精美 UI 介面設計
|
| 104 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="indigo")) as demo:
|
| 105 |
gr.Markdown("# 🤖 AI 跨語言智慧語意學習終端")
|
| 106 |
-
gr.Markdown("### 🚀 專題亮點:結合核心指代消解 (Coreference Resolution) 與
|
| 107 |
|
| 108 |
with gr.Row():
|
| 109 |
with gr.Column(scale=1):
|
| 110 |
txt_input = gr.Textbox(
|
| 111 |
label="📥 請輸入中文或英文段落 (Input Text)",
|
| 112 |
-
placeholder="例如:Mary is a
|
| 113 |
lines=5
|
| 114 |
)
|
| 115 |
btn_submit = gr.Button("🔥 執行多維度 AI 語意解析", variant="primary")
|
| 116 |
|
| 117 |
with gr.Column(scale=1):
|
| 118 |
-
# 這三個是各自獨立的漂亮框框!
|
| 119 |
out_translation = gr.Textbox(label="📖 完整文本翻譯 (Translation)", lines=3)
|
| 120 |
-
# 這裡就是
|
| 121 |
-
out_vocab = gr.Textbox(label="📚 AI 智慧單字本 (Vocabulary Booklet)", lines=
|
| 122 |
-
out_report = gr.Textbox(label="🎯 AI 語意消解報告 (Coreference Report)", lines=
|
| 123 |
|
| 124 |
btn_submit.click(
|
| 125 |
fn=coref_learning_pipeline,
|
|
@@ -127,13 +128,5 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="indigo"))
|
|
| 127 |
outputs=[out_translation, out_vocab, out_report]
|
| 128 |
)
|
| 129 |
|
| 130 |
-
gr.Examples(
|
| 131 |
-
examples=[
|
| 132 |
-
["Mary is a Farmer. Mary has a little lamb. John is a Florist. John has a flower shop."],
|
| 133 |
-
["張醫生正在動手術。她很冷靜。她的助手在旁邊遞器材,他看起來很緊張。"]
|
| 134 |
-
],
|
| 135 |
-
inputs=txt_input
|
| 136 |
-
)
|
| 137 |
-
|
| 138 |
if __name__ == "__main__":
|
| 139 |
demo.launch()
|
|
|
|
| 18 |
pass
|
| 19 |
|
| 20 |
import gradio as gr
|
| 21 |
+
import spacy
|
| 22 |
from fastcoref import FCoref
|
| 23 |
from deep_translator import GoogleTranslator
|
| 24 |
|
| 25 |
+
# 3. 初始化 Spacy NLP 詞性解析器與指代模型
|
| 26 |
+
print("🚀 [System] 正在初始化 NLP 詞性解析器...")
|
| 27 |
+
nlp = spacy.load("en_core_web_sm")
|
| 28 |
+
|
| 29 |
print("🚀 [System] 正在初始化 F-Coref 核心大腦...")
|
| 30 |
try:
|
| 31 |
model = FCoref(model_name_or_path='biu-nlp/f-coref', device='cpu')
|
| 32 |
+
print("✅ [System] 所有模型加載成功!")
|
| 33 |
except:
|
| 34 |
model = FCoref('biu-nlp/f-coref', device='cpu')
|
| 35 |
print("✅ [System] 備用路徑加載成功!")
|
|
|
|
| 56 |
# C. 欄位一:生成「英翻中結果」
|
| 57 |
translation_text = GoogleTranslator(source='en', target='zh-TW').translate(working_text)
|
| 58 |
|
| 59 |
+
# D. 💥 欄位二:全新重構【AI 智慧單字本】(利用詞性精準抓取名詞,消滅人名)
|
| 60 |
vocab_output = ""
|
| 61 |
+
doc = nlp(working_text)
|
| 62 |
+
extracted_words = set()
|
| 63 |
+
|
| 64 |
+
for token in doc:
|
| 65 |
+
# 只抓普通名詞 (NOUN),排除專有名詞/人名 (PROPN) 與代名詞 (PRON)
|
| 66 |
+
if token.pos_ == "NOUN" and len(token.text) > 2:
|
| 67 |
+
# 統一轉成單數原型原型,畫面更漂亮(例如把 lambs 變成 lamb)
|
| 68 |
+
extracted_words.add(token.lemma_.lower())
|
| 69 |
+
|
| 70 |
+
if extracted_words:
|
| 71 |
+
for word in sorted(extracted_words):
|
| 72 |
+
try:
|
| 73 |
+
# 翻成繁體中文
|
| 74 |
+
word_zh = GoogleTranslator(source='en', target='zh-TW').translate(word)
|
| 75 |
+
# 輸出格式:【Farmer ➔ 農夫】
|
| 76 |
+
vocab_output += f"🔸 {word.capitalize()} ➔ {word_zh}\n"
|
| 77 |
+
except:
|
| 78 |
+
pass
|
|
|
|
|
|
|
| 79 |
else:
|
| 80 |
+
vocab_output = "ℹ️ 未偵測到適合學習的核心英文單字。"
|
| 81 |
|
| 82 |
# E. 欄位三:生成「AI 語意共指報告」
|
| 83 |
report_text = f"✨ 系統狀態:{mode_notice}\n"
|
|
|
|
| 102 |
except Exception as e:
|
| 103 |
return f"錯誤: {str(e)}", "無法整合單字", f"運行異常: {str(e)}"
|
| 104 |
|
| 105 |
+
# 5. 精美 UI 介面設計
|
| 106 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="indigo")) as demo:
|
| 107 |
gr.Markdown("# 🤖 AI 跨語言智慧語意學習終端")
|
| 108 |
+
gr.Markdown("### 🚀 專題亮點:結合核心指代消解 (Coreference Resolution) 與 NLP 智慧名詞提取技術")
|
| 109 |
|
| 110 |
with gr.Row():
|
| 111 |
with gr.Column(scale=1):
|
| 112 |
txt_input = gr.Textbox(
|
| 113 |
label="📥 請輸入中文或英文段落 (Input Text)",
|
| 114 |
+
placeholder="例如:Mary is a farmer. Mary has a little lamb.",
|
| 115 |
lines=5
|
| 116 |
)
|
| 117 |
btn_submit = gr.Button("🔥 執行多維度 AI 語意解析", variant="primary")
|
| 118 |
|
| 119 |
with gr.Column(scale=1):
|
|
|
|
| 120 |
out_translation = gr.Textbox(label="📖 完整文本翻譯 (Translation)", lines=3)
|
| 121 |
+
# 這裡就是真正的智慧單字本!
|
| 122 |
+
out_vocab = gr.Textbox(label="📚 AI 智慧單字本 (Vocabulary Booklet)", lines=5)
|
| 123 |
+
out_report = gr.Textbox(label="🎯 AI 語意消解報告 (Coreference Report)", lines=5)
|
| 124 |
|
| 125 |
btn_submit.click(
|
| 126 |
fn=coref_learning_pipeline,
|
|
|
|
| 128 |
outputs=[out_translation, out_vocab, out_report]
|
| 129 |
)
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
if __name__ == "__main__":
|
| 132 |
demo.launch()
|