Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,122 @@
|
|
1 |
import gradio as gr
|
2 |
-
from vocab import get_sources,
|
|
|
3 |
from ai_sentence import generate_sentence, MODEL_LIST
|
4 |
|
|
|
|
|
5 |
def fetch_sentence(word, source, use_ai, model_name):
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
7 |
if word not in valid_words:
|
8 |
return f"<p style='color:red;'>❌ 單字 {word} 不在 {source} 單字庫中</p>", "檢查失敗"
|
9 |
|
|
|
|
|
|
|
|
|
|
|
10 |
sentence_records = get_sentences_by_word(word)
|
11 |
-
result_display = ""
|
12 |
-
status_log = []
|
13 |
|
14 |
if not use_ai:
|
|
|
15 |
for record in sentence_records:
|
16 |
_, phonetic, sentence, src, model = record
|
17 |
-
result_display += f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
if not sentence_records:
|
19 |
-
result_display = f"<p style='color:red;'>❌
|
|
|
20 |
else:
|
|
|
|
|
21 |
try:
|
22 |
sentence = generate_sentence(word, model_name)
|
23 |
-
result_display = f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
except Exception as e:
|
25 |
-
result_display = f"<p style='color:red;'>❌ AI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
return result_display, "完成"
|
28 |
|
29 |
with gr.Blocks() as demo:
|
30 |
-
gr.Markdown(
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
|
|
|
|
|
|
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
with gr.Group(visible=True) as query_mode:
|
36 |
source_dropdown_query = gr.Dropdown(choices=get_sources(), value="common3000", label="選擇單字庫")
|
37 |
word_input = gr.Textbox(label="輸入單字")
|
@@ -41,14 +126,41 @@ with gr.Blocks() as demo:
|
|
41 |
query_status = gr.Textbox()
|
42 |
query_button = gr.Button("查詢例句")
|
43 |
|
44 |
-
|
45 |
-
|
|
|
|
|
46 |
|
47 |
-
|
|
|
|
|
|
|
|
|
48 |
query_button.click(
|
49 |
fetch_sentence,
|
50 |
inputs=[word_input, source_dropdown_query, use_ai_checkbox, model_dropdown],
|
51 |
-
outputs=[query_result, query_status]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
)
|
53 |
|
54 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from vocab import get_sources, get_words_from_source, get_word_info
|
3 |
+
from sentences import get_sentences_by_word, get_words_with_sentences, save_sentence
|
4 |
from ai_sentence import generate_sentence, MODEL_LIST
|
5 |
|
6 |
+
|
7 |
+
# 查詢例句模式 - 處理函數
|
8 |
def fetch_sentence(word, source, use_ai, model_name):
|
9 |
+
status_log = []
|
10 |
+
result_display = ""
|
11 |
+
|
12 |
+
# 1️⃣ 檢查單字是否存在於該單字庫
|
13 |
+
words_data = get_words_from_source(source)
|
14 |
+
valid_words = {w['word'] for w in words_data}
|
15 |
if word not in valid_words:
|
16 |
return f"<p style='color:red;'>❌ 單字 {word} 不在 {source} 單字庫中</p>", "檢查失敗"
|
17 |
|
18 |
+
# 2️⃣ 查詢單字音標(用於顯示 & AI 生成時存入資料庫)
|
19 |
+
word_info = get_word_info(source, word)
|
20 |
+
phonetic = word_info['phonetic'] if word_info else "無"
|
21 |
+
|
22 |
+
# 3️⃣ 查詢句庫
|
23 |
sentence_records = get_sentences_by_word(word)
|
|
|
|
|
24 |
|
25 |
if not use_ai:
|
26 |
+
# 不使用 AI,只查句庫
|
27 |
for record in sentence_records:
|
28 |
_, phonetic, sentence, src, model = record
|
29 |
+
result_display += f"""
|
30 |
+
<div style="margin-bottom: 10px; padding: 8px; border-left: 4px solid #4CAF50; background-color: #f9f9f9;">
|
31 |
+
<strong>單字:</strong> {word} <br>
|
32 |
+
<strong>音標:</strong> {phonetic or '無'} <br>
|
33 |
+
<strong>句子:</strong> {sentence} <br>
|
34 |
+
<strong>來源:</strong> {src} {f'({model})' if model else ''}
|
35 |
+
</div>
|
36 |
+
"""
|
37 |
if not sentence_records:
|
38 |
+
result_display = f"<p style='color:red;'>❌ 查無例句:{word}</p>"
|
39 |
+
|
40 |
else:
|
41 |
+
# 使用 AI 生成
|
42 |
+
status_log.append(f"🤖 使用 AI 模型 {model_name} 生成句子中...")
|
43 |
try:
|
44 |
sentence = generate_sentence(word, model_name)
|
45 |
+
result_display = f"""
|
46 |
+
<div style="margin-bottom: 10px; padding: 8px; border-left: 4px solid #2196F3; background-color: #f9f9f9;">
|
47 |
+
<strong>單字:</strong> {word} <br>
|
48 |
+
<strong>音標:</strong> {phonetic} <br>
|
49 |
+
<strong>句子:</strong> {sentence} <br>
|
50 |
+
<strong>來源:</strong> AI 生成 ({model_name})
|
51 |
+
</div>
|
52 |
+
"""
|
53 |
+
|
54 |
+
# 若句庫沒這個單字,才存入資料庫
|
55 |
+
if not sentence_records:
|
56 |
+
save_sentence(word, phonetic, sentence, 'ai', model_name)
|
57 |
+
status_log.append(f"✅ AI 生成句子已儲存:{word} ({model_name})")
|
58 |
+
else:
|
59 |
+
status_log.append(f"🔄 AI 生成句子,但句庫已有紀錄,未覆蓋:{word}")
|
60 |
+
|
61 |
except Exception as e:
|
62 |
+
result_display = f"<p style='color:red;'>❌ AI 生成句子失敗:{str(e)}</p>"
|
63 |
+
status_log.append(f"❌ 生成失敗:{str(e)}")
|
64 |
+
|
65 |
+
return result_display, "\n".join(status_log)
|
66 |
+
|
67 |
+
|
68 |
+
# 模式切換邏輯
|
69 |
+
def toggle_mode(mode):
|
70 |
+
return {
|
71 |
+
"query": (gr.update(visible=True), gr.update(visible=False)),
|
72 |
+
"random": (gr.update(visible=False), gr.update(visible=True)),
|
73 |
+
}[mode]
|
74 |
|
|
|
75 |
|
76 |
with gr.Blocks() as demo:
|
77 |
+
gr.Markdown(
|
78 |
+
"""
|
79 |
+
# 📖 VocabLine 單字例句查詢 & 生成平台
|
80 |
+
|
81 |
+
## 📝 專案簡介
|
82 |
+
VocabLine 是一個單字例句工具,提供查詢句庫例句、AI 生成句子功能。
|
83 |
+
可作為 LINE 單字推播、自學工具、英文教學輔助等用途。
|
84 |
|
85 |
+
## ⚙️ 主要功能
|
86 |
+
- 支援從多個單字庫選擇。
|
87 |
+
- 可查詢單字句庫例句或使用 GPT AI 生成例句。
|
88 |
+
- 隨機抽取單字,生成例句。
|
89 |
|
90 |
+
## 🗂️ 資料來源
|
91 |
+
- 單字庫:common3000 等
|
92 |
+
- 句庫來源:Tatoeba 例句、AI 生成句子(GPT)
|
93 |
+
|
94 |
+
## 🛠️ 技術架構
|
95 |
+
- Gradio Blocks 前端介面
|
96 |
+
- Hugging Face Transformers 語言模型(GPT)
|
97 |
+
- SQLite 句庫
|
98 |
+
|
99 |
+
## 👨💻 開發者資訊
|
100 |
+
- 開發者:余彦志 (大宇 ian)
|
101 |
+
- 信箱:[email protected]
|
102 |
+
- GitHub:[https://github.com/dayuian](https://github.com/dayuian)
|
103 |
+
|
104 |
+
## 📲 LINE 推播(未來開發)
|
105 |
+
- 本工具將結合 LINE,透過 Make.com 每日推播單字和例句,敬請期待!
|
106 |
+
"""
|
107 |
+
)
|
108 |
+
|
109 |
+
# 模式選擇
|
110 |
+
mode_radio = gr.Radio(
|
111 |
+
["query", "random"],
|
112 |
+
label="選擇模式",
|
113 |
+
choices=["query", "random"],
|
114 |
+
value="query",
|
115 |
+
interactive=True,
|
116 |
+
info="query:單字查詢模式 | random:隨機抽單字模式"
|
117 |
+
)
|
118 |
+
|
119 |
+
# 查詢模式區塊
|
120 |
with gr.Group(visible=True) as query_mode:
|
121 |
source_dropdown_query = gr.Dropdown(choices=get_sources(), value="common3000", label="選擇單字庫")
|
122 |
word_input = gr.Textbox(label="輸入單字")
|
|
|
126 |
query_status = gr.Textbox()
|
127 |
query_button = gr.Button("查詢例句")
|
128 |
|
129 |
+
# 隨機抽單字模式區塊
|
130 |
+
with gr.Group(visible=False) as random_mode:
|
131 |
+
source_dropdown_random = gr.Dropdown(choices=get_sources(), value="common3000", label="選擇單字庫")
|
132 |
+
num_input = gr.Number(value=5, label="抽幾個單字")
|
133 |
|
134 |
+
random_result_output = gr.HTML()
|
135 |
+
random_status_output = gr.Textbox()
|
136 |
+
submit_btn = gr.Button("生成例句")
|
137 |
+
|
138 |
+
# 查詢模式互動
|
139 |
query_button.click(
|
140 |
fetch_sentence,
|
141 |
inputs=[word_input, source_dropdown_query, use_ai_checkbox, model_dropdown],
|
142 |
+
outputs=[query_result, query_status],
|
143 |
+
)
|
144 |
+
|
145 |
+
# 隨機抽單字模式互動
|
146 |
+
submit_btn.click(
|
147 |
+
fn=get_words_with_sentences,
|
148 |
+
inputs=[source_dropdown_random, num_input],
|
149 |
+
outputs=[random_result_output, random_status_output]
|
150 |
+
)
|
151 |
+
|
152 |
+
# 模式切換邏輯
|
153 |
+
mode_radio.change(
|
154 |
+
toggle_mode,
|
155 |
+
inputs=[mode_radio],
|
156 |
+
outputs=[query_mode, random_mode],
|
157 |
+
)
|
158 |
+
|
159 |
+
# 切換 AI 模型下拉選單可見性
|
160 |
+
use_ai_checkbox.change(
|
161 |
+
lambda use_ai: gr.update(visible=use_ai),
|
162 |
+
inputs=[use_ai_checkbox],
|
163 |
+
outputs=[model_dropdown]
|
164 |
)
|
165 |
|
166 |
demo.launch()
|