Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,8 +5,6 @@ from sentences import generate_sentences
|
|
5 |
from ai_sentence import MODEL_LIST
|
6 |
from quiz import generate_fill_in_blank_exam, check_exam
|
7 |
|
8 |
-
|
9 |
-
|
10 |
def process_sentence(mode, word, source, num, use_ai, model_name):
|
11 |
try:
|
12 |
if mode == '查詢單字':
|
@@ -34,21 +32,17 @@ def project_description():
|
|
34 |
return """
|
35 |
# 📖 VocabLine 單字例句工具
|
36 |
支援單字例句查詢,AI 自動生成句子。適合作為 LINE 單字推播、英文學習輔助工具。
|
37 |
-
|
38 |
## 🔍 核心功能
|
39 |
- 查詢單字 → 獲取例句
|
40 |
- 抽取單字 → 批量獲取例句
|
41 |
- 可選 AI 生成句子(模型:GPT2 / Pythia)
|
42 |
-
|
43 |
## 🧑💻 技術架構
|
44 |
- Gradio Blocks + Transformers (Hugging Face)
|
45 |
- SQLite 句庫管理
|
46 |
- 支援多單字庫擴展
|
47 |
-
|
48 |
## 📚 資料來源
|
49 |
- 常用 3000 單字表
|
50 |
- 英文例句資料庫 (Tatoeba)
|
51 |
-
|
52 |
## 👨💻 開發資訊
|
53 |
- 開發者:余彦志 (大宇 ian)
|
54 |
- 信箱:[email protected]
|
@@ -66,14 +60,13 @@ with gr.Blocks(css="""
|
|
66 |
mode_radio = gr.Radio(
|
67 |
["查詢單字", "隨機抽單字"],
|
68 |
label="選擇模式",
|
69 |
-
choices=["查詢單字", "隨機抽單字"],
|
70 |
value="查詢單字",
|
71 |
interactive=True
|
72 |
)
|
73 |
|
74 |
with gr.Group(elem_id="card-group"):
|
75 |
word_input = gr.Textbox(label="輸入單字", visible=True)
|
76 |
-
num_input = gr.Slider(minimum=1, maximum=10,value=5,step=1,label="抽取單字數量")
|
77 |
source_dropdown = gr.Dropdown(
|
78 |
choices=get_sources(),
|
79 |
value="common3000",
|
@@ -85,10 +78,10 @@ with gr.Blocks(css="""
|
|
85 |
|
86 |
with gr.Row():
|
87 |
model_dropdown = gr.Dropdown(
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
)
|
93 |
gr.Markdown("🔷 **建議使用 GPT2(表現較佳),Pythia-410m 很爛慎選!**", visible=False)
|
94 |
|
@@ -97,47 +90,23 @@ with gr.Blocks(css="""
|
|
97 |
visible=False,
|
98 |
interactive=False,
|
99 |
label=""
|
100 |
-
|
101 |
|
102 |
-
|
103 |
-
|
104 |
-
generate_button = gr.Button("✨ 生成句子", elem_id="generate-button")
|
105 |
-
gr.HTML("<a href='https://你的空白quiz網址' target='_blank'><button style='background-color:#ff5722;color:white;'>📝 生成小考</button></a>")
|
106 |
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
|
111 |
-
|
112 |
-
|
113 |
-
if mode == '查詢單字':
|
114 |
-
return gr.update(visible=True), gr.update(visible=False)
|
115 |
-
else:
|
116 |
-
return gr.update(visible=False), gr.update(visible=True)
|
117 |
-
|
118 |
-
# ---- 切換 AI 按鈕函數 ----
|
119 |
-
def toggle_ai_button(use_ai):
|
120 |
-
model_visibility = gr.update(visible=use_ai)
|
121 |
-
warning_visibility = gr.update(visible=use_ai)
|
122 |
-
return model_visibility, warning_visibility
|
123 |
-
|
124 |
-
# ---- 把這些事件綁定「獨立寫」出來,不要放在任何 Group 或 Row 裡 ----
|
125 |
-
mode_radio.change(
|
126 |
-
switch_mode,
|
127 |
-
inputs=[mode_radio],
|
128 |
-
outputs=[word_input, num_input]
|
129 |
-
)
|
130 |
|
131 |
-
|
132 |
-
|
133 |
-
inputs=[use_ai_checkbox],
|
134 |
-
outputs=[model_dropdown, ai_warning]
|
135 |
-
)
|
136 |
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
outputs=[result_output, status_output]
|
141 |
-
)
|
142 |
|
143 |
demo.launch()
|
|
|
5 |
from ai_sentence import MODEL_LIST
|
6 |
from quiz import generate_fill_in_blank_exam, check_exam
|
7 |
|
|
|
|
|
8 |
def process_sentence(mode, word, source, num, use_ai, model_name):
|
9 |
try:
|
10 |
if mode == '查詢單字':
|
|
|
32 |
return """
|
33 |
# 📖 VocabLine 單字例句工具
|
34 |
支援單字例句查詢,AI 自動生成句子。適合作為 LINE 單字推播、英文學習輔助工具。
|
|
|
35 |
## 🔍 核心功能
|
36 |
- 查詢單字 → 獲取例句
|
37 |
- 抽取單字 → 批量獲取例句
|
38 |
- 可選 AI 生成句子(模型:GPT2 / Pythia)
|
|
|
39 |
## 🧑💻 技術架構
|
40 |
- Gradio Blocks + Transformers (Hugging Face)
|
41 |
- SQLite 句庫管理
|
42 |
- 支援多單字庫擴展
|
|
|
43 |
## 📚 資料來源
|
44 |
- 常用 3000 單字表
|
45 |
- 英文例句資料庫 (Tatoeba)
|
|
|
46 |
## 👨💻 開發資訊
|
47 |
- 開發者:余彦志 (大宇 ian)
|
48 |
- 信箱:[email protected]
|
|
|
60 |
mode_radio = gr.Radio(
|
61 |
["查詢單字", "隨機抽單字"],
|
62 |
label="選擇模式",
|
|
|
63 |
value="查詢單字",
|
64 |
interactive=True
|
65 |
)
|
66 |
|
67 |
with gr.Group(elem_id="card-group"):
|
68 |
word_input = gr.Textbox(label="輸入單字", visible=True)
|
69 |
+
num_input = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="抽取單字數量")
|
70 |
source_dropdown = gr.Dropdown(
|
71 |
choices=get_sources(),
|
72 |
value="common3000",
|
|
|
78 |
|
79 |
with gr.Row():
|
80 |
model_dropdown = gr.Dropdown(
|
81 |
+
choices=MODEL_LIST,
|
82 |
+
value="gpt2",
|
83 |
+
label="選擇 AI 模型",
|
84 |
+
visible=False
|
85 |
)
|
86 |
gr.Markdown("🔷 **建議使用 GPT2(表現較佳),Pythia-410m 很爛慎選!**", visible=False)
|
87 |
|
|
|
90 |
visible=False,
|
91 |
interactive=False,
|
92 |
label=""
|
93 |
+
)
|
94 |
|
95 |
+
result_output = gr.HTML(label="結果")
|
96 |
+
status_output = gr.Textbox(label="處理狀態", interactive=False)
|
|
|
|
|
97 |
|
98 |
+
with gr.Row():
|
99 |
+
generate_button = gr.Button("✨ 生成句子", elem_id="generate-button")
|
100 |
+
gr.HTML("<a href='https://你的空白quiz網址' target='_blank'><button style='background-color:#ff5722;color:white;'>📝 生成小考</button></a>")
|
101 |
|
102 |
+
def switch_mode(mode):
|
103 |
+
return gr.update(visible=(mode == '查詢單字')), gr.update(visible=(mode == '隨機抽單字'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
+
def toggle_ai_button(use_ai):
|
106 |
+
return gr.update(visible=use_ai), gr.update(visible=use_ai)
|
|
|
|
|
|
|
107 |
|
108 |
+
mode_radio.change(switch_mode, inputs=[mode_radio], outputs=[word_input, num_input])
|
109 |
+
use_ai_checkbox.change(toggle_ai_button, inputs=[use_ai_checkbox], outputs=[model_dropdown, ai_warning])
|
110 |
+
generate_button.click(process_sentence, inputs=[mode_radio, word_input, source_dropdown, num_input, use_ai_checkbox, model_dropdown], outputs=[result_output, status_output])
|
|
|
|
|
111 |
|
112 |
demo.launch()
|