Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,108 +1,51 @@
|
|
1 |
import gradio as gr
|
2 |
from vocab import get_sources, get_words_from_source, get_word_info
|
3 |
-
from sentences import
|
4 |
-
from ai_sentence import
|
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 |
-
|
77 |
-
|
78 |
-
|
79 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
可作為 LINE 單字推播、自學工具、英文教學輔助等用途。
|
84 |
|
85 |
-
|
86 |
-
- 支援從多個單字庫選擇。
|
87 |
-
- 可查詢單字句庫例句或使用 GPT AI 生成例句。
|
88 |
-
- 隨機抽取單字,生成例句。
|
89 |
|
90 |
-
|
91 |
-
|
92 |
-
- 句庫來源:Tatoeba 例句、AI 生成句子(GPT)
|
93 |
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
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 |
|
@@ -112,55 +55,59 @@ with gr.Blocks() as demo:
|
|
112 |
label="選擇模式",
|
113 |
choices=["query", "random"],
|
114 |
value="query",
|
115 |
-
interactive=True
|
116 |
-
info="query:單字查詢模式 | random:隨機抽單字模式"
|
117 |
)
|
118 |
|
119 |
# 查詢模式區塊
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
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 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
|
|
|
|
150 |
)
|
151 |
|
152 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
mode_radio.change(
|
154 |
-
|
155 |
inputs=[mode_radio],
|
156 |
-
outputs=[
|
157 |
)
|
158 |
|
159 |
-
#
|
160 |
use_ai_checkbox.change(
|
161 |
-
lambda
|
162 |
inputs=[use_ai_checkbox],
|
163 |
outputs=[model_dropdown]
|
164 |
)
|
165 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from vocab import get_sources, get_words_from_source, get_word_info
|
3 |
+
from sentences import generate_sentences
|
4 |
+
from ai_sentence import MODEL_LIST
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
|
7 |
+
# 核心:處理單字模式 & 句子生成方式
|
8 |
+
def process_sentence(mode, word, source, num, use_ai, model_name):
|
9 |
+
try:
|
10 |
+
# 1️⃣ 根據模式獲取單字列表
|
11 |
+
if mode == 'query':
|
12 |
+
if not word:
|
13 |
+
return "<p style='color:red;'>❌ 請輸入單字</p>", "未輸入單字"
|
14 |
+
words = [word.strip()]
|
15 |
+
|
16 |
+
elif mode == 'random':
|
17 |
+
num = int(num)
|
18 |
+
if num <= 0:
|
19 |
+
return "<p style='color:red;'>❌ 抽取數量須大於0</p>", "數量錯誤"
|
20 |
+
words_data = get_words_from_source(source)
|
21 |
+
words = [w['word'] for w in words_data]
|
22 |
+
words = random.sample(words, num)
|
23 |
+
|
24 |
+
else:
|
25 |
+
return "<p style='color:red;'>❌ 模式錯誤</p>", "模式選擇異常"
|
26 |
|
27 |
+
# 2️⃣ 呼叫 sentences.py 核心處理句子的函數
|
28 |
+
result_display, status_log = generate_sentences(words, source, use_ai, model_name)
|
|
|
29 |
|
30 |
+
return result_display, status_log
|
|
|
|
|
|
|
31 |
|
32 |
+
except Exception as e:
|
33 |
+
return f"<p style='color:red;'>❌ 發生錯誤:{str(e)}</p>", f"錯誤:{str(e)}"
|
|
|
34 |
|
35 |
+
|
36 |
+
# Gradio UI
|
37 |
+
with gr.Blocks() as demo:
|
38 |
+
gr.Markdown(
|
39 |
+
"""
|
40 |
+
# 📖 VocabLine 單字例句工具
|
41 |
+
|
42 |
+
## 📝 簡介
|
43 |
+
支援單字例句查詢,AI 自動生成句子。適合作為 LINE 單字推播、英文學習輔助工具。
|
44 |
|
45 |
## 👨💻 開發者資訊
|
46 |
- 開發者:余彦志 (大宇 ian)
|
47 |
- 信箱:[email protected]
|
48 |
- GitHub:[https://github.com/dayuian](https://github.com/dayuian)
|
|
|
|
|
|
|
49 |
"""
|
50 |
)
|
51 |
|
|
|
55 |
label="選擇模式",
|
56 |
choices=["query", "random"],
|
57 |
value="query",
|
58 |
+
interactive=True
|
|
|
59 |
)
|
60 |
|
61 |
# 查詢模式區塊
|
62 |
+
word_input = gr.Textbox(label="輸入單字", visible=True)
|
63 |
+
num_input = gr.Number(label="抽取單字數量", value=5, visible=False)
|
64 |
+
|
65 |
+
# 單字庫選擇
|
66 |
+
source_dropdown = gr.Dropdown(
|
67 |
+
choices=get_sources(),
|
68 |
+
value="common3000",
|
69 |
+
label="選擇單字庫"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
)
|
71 |
|
72 |
+
# AI 控制區塊
|
73 |
+
use_ai_checkbox = gr.Checkbox(label="使用 AI 生成句子")
|
74 |
+
model_dropdown = gr.Dropdown(
|
75 |
+
choices=MODEL_LIST,
|
76 |
+
value=MODEL_LIST[0],
|
77 |
+
label="選擇 AI 模型",
|
78 |
+
visible=False
|
79 |
)
|
80 |
|
81 |
+
# 按鈕 & 結果顯示
|
82 |
+
submit_btn = gr.Button("生成句子")
|
83 |
+
result_output = gr.HTML(label="結果")
|
84 |
+
status_output = gr.Textbox(label="處理狀態", interactive=False)
|
85 |
+
|
86 |
+
# 模式切換時,顯示不同輸入框
|
87 |
+
def switch_mode(mode):
|
88 |
+
if mode == 'query':
|
89 |
+
return gr.update(visible=True), gr.update(visible=False)
|
90 |
+
else:
|
91 |
+
return gr.update(visible=False), gr.update(visible=True)
|
92 |
+
|
93 |
mode_radio.change(
|
94 |
+
switch_mode,
|
95 |
inputs=[mode_radio],
|
96 |
+
outputs=[word_input, num_input]
|
97 |
)
|
98 |
|
99 |
+
# 勾選 AI 時,顯示模型選擇
|
100 |
use_ai_checkbox.change(
|
101 |
+
lambda x: gr.update(visible=x),
|
102 |
inputs=[use_ai_checkbox],
|
103 |
outputs=[model_dropdown]
|
104 |
)
|
105 |
|
106 |
+
# 按鈕綁定處理函數
|
107 |
+
submit_btn.click(
|
108 |
+
process_sentence,
|
109 |
+
inputs=[mode_radio, word_input, source_dropdown, num_input, use_ai_checkbox, model_dropdown],
|
110 |
+
outputs=[result_output, status_output]
|
111 |
+
)
|
112 |
+
|
113 |
demo.launch()
|