Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,59 +1,54 @@
|
|
1 |
import gradio as gr
|
2 |
-
from vocab import get_sources,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
with gr.Blocks() as demo:
|
5 |
-
gr.Markdown(
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
## 🛠️ 技術架構
|
29 |
-
- **Gradio Blocks** 前端介面 + API。
|
30 |
-
- **Hugging Face Transformers** 語言模型:
|
31 |
-
- 模型:`EleutherAI/pythia-410m`(小型 GPT 模型)
|
32 |
-
|
33 |
-
## 👨💻 開發者資訊
|
34 |
-
- 開發者:余彦志 (大宇 ian)
|
35 |
-
- 信箱:[email protected]
|
36 |
-
- GitHub:[https://github.com/dayuian](https://github.com/dayuian)
|
37 |
-
"""
|
38 |
-
)
|
39 |
-
|
40 |
-
source_dropdown = gr.Dropdown(
|
41 |
-
choices=get_sources(),
|
42 |
-
value="common3000",
|
43 |
-
label="選擇單字庫",
|
44 |
-
interactive=True
|
45 |
-
)
|
46 |
-
|
47 |
-
num_input = gr.Number(value=10, label="抽幾個單字")
|
48 |
-
result_output = gr.HTML(label="生成結果")
|
49 |
-
status_output = gr.Textbox(label="狀態更新", lines=8, interactive=False)
|
50 |
-
|
51 |
-
submit_btn = gr.Button("生成例句")
|
52 |
-
|
53 |
-
submit_btn.click(
|
54 |
-
fn=get_words_with_sentences,
|
55 |
-
inputs=[source_dropdown, num_input],
|
56 |
-
outputs=[result_output, status_output]
|
57 |
)
|
58 |
|
59 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from vocab import get_sources, get_sentences_by_word, get_words_from_source
|
3 |
+
from ai_sentence import generate_sentence, MODEL_LIST
|
4 |
+
|
5 |
+
def fetch_sentence(word, source, use_ai, model_name):
|
6 |
+
valid_words = get_words_from_source(source)
|
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"<p>📖 {word} [{phonetic or '無'}]: {sentence} (來源: {src}{f' ({model})' if model else ''})</p>"
|
18 |
+
if not sentence_records:
|
19 |
+
result_display = f"<p style='color:red;'>❌ 查無句子</p>"
|
20 |
+
else:
|
21 |
+
try:
|
22 |
+
sentence = generate_sentence(word, model_name)
|
23 |
+
result_display = f"<p>🤖 AI 生成: {sentence} (模型: {model_name})</p>"
|
24 |
+
except Exception as e:
|
25 |
+
result_display = f"<p style='color:red;'>❌ AI 生成失敗:{str(e)}</p>"
|
26 |
+
|
27 |
+
return result_display, "完成"
|
28 |
|
29 |
with gr.Blocks() as demo:
|
30 |
+
gr.Markdown("# 📖 VocabLine 單字例句查詢 & 生成平台")
|
31 |
+
gr.Markdown("開發者:余彦志 (大宇 ian) | Email: [email protected] | [GitHub](https://github.com/dayuian)")
|
32 |
+
|
33 |
+
mode = gr.Radio(["查詢模式", "隨機抽單字模式"], value="查詢模式")
|
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="輸入單字")
|
38 |
+
use_ai_checkbox = gr.Checkbox(label="使用 AI 生成句子")
|
39 |
+
model_dropdown = gr.Dropdown(choices=MODEL_LIST, value=MODEL_LIST[0], label="選擇 AI 模型", visible=False)
|
40 |
+
query_result = gr.HTML()
|
41 |
+
query_status = gr.Textbox()
|
42 |
+
query_button = gr.Button("查詢例句")
|
43 |
+
|
44 |
+
def toggle_model(use_ai):
|
45 |
+
return gr.update(visible=use_ai)
|
46 |
+
|
47 |
+
use_ai_checkbox.change(toggle_model, inputs=use_ai_checkbox, outputs=model_dropdown)
|
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()
|