JJS341 commited on
Commit
91694e5
·
verified ·
1 Parent(s): a2cf0dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -39
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. 欄位二:生成妳要的【AI 智慧單字本】
56
  vocab_output = ""
57
- if clusters:
58
- extracted_words = set()
59
- for cluster in clusters:
60
- for item in cluster:
61
- # 清理冠詞與代名詞,只留下核心實體名詞
62
- clean_word = item.lower().replace("the ", "").replace("a ", "").replace("her ", "").replace("his ", "").strip()
63
- if len(clean_word) > 2 and clean_word not in ['she', 'he', 'him', 'her', 'it', 'they', 'i', 'you']:
64
- extracted_words.add(clean_word)
65
-
66
- if extracted_words:
67
- for word in sorted(extracted_words):
68
- try:
69
- # 翻成繁體中文
70
- word_zh = GoogleTranslator(source='en', target='zh-TW').translate(word)
71
- # 格式化成妳要的:【Farmer (農夫)樣式
72
- vocab_output += f"🔸 {word.capitalize()} ➔ {word_zh}\n"
73
- except:
74
- pass
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 介面設計(利用 Blocks 切分獨立區塊)
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 Farmer. Mary had a little lamb.",
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=4)
122
- out_report = gr.Textbox(label="🎯 AI 語意消解報告 (Coreference Report)", lines=6)
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()