Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,11 +3,8 @@ import random
|
|
3 |
from vocab import get_sources, get_words_from_source
|
4 |
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 |
-
# ================================
|
11 |
def process_sentence(mode, word, source, num, use_ai, model_name):
|
12 |
try:
|
13 |
if mode == '查詢單字':
|
@@ -31,9 +28,6 @@ def process_sentence(mode, word, source, num, use_ai, model_name):
|
|
31 |
except Exception as e:
|
32 |
return f"<p style='color:red;'>❌ 發生錯誤:{str(e)}</p>", f"錯誤:{str(e)}"
|
33 |
|
34 |
-
# ================================
|
35 |
-
# 專案說明段落
|
36 |
-
# ================================
|
37 |
def project_description():
|
38 |
return """
|
39 |
# 📖 VocabLine 單字例句工具
|
@@ -55,96 +49,104 @@ def project_description():
|
|
55 |
- GitHub:[https://github.com/dayuian](https://github.com/dayuian)
|
56 |
"""
|
57 |
|
58 |
-
# ================================
|
59 |
-
# 模式切換:查詢 or 抽單字
|
60 |
-
# ================================
|
61 |
-
def switch_mode(mode):
|
62 |
-
return gr.update(visible=(mode == '查詢單字')), gr.update(visible=(mode == '隨機抽單字'))
|
63 |
-
|
64 |
-
# ================================
|
65 |
-
# 切換 AI 控制區塊
|
66 |
-
# ================================
|
67 |
-
def toggle_ai_button(use_ai):
|
68 |
-
return gr.update(visible=use_ai), gr.update(visible=use_ai)
|
69 |
-
|
70 |
-
# ================================
|
71 |
-
# 介面配置 - 分頁版本
|
72 |
-
# ================================
|
73 |
with gr.Blocks(css="""
|
74 |
#card-group { padding: 15px; border-radius: 12px; background-color: rgba(255, 255, 255, 0.05); box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); margin-bottom: 15px; }
|
75 |
.gradio-container { max-width: 800px; margin: auto; }
|
76 |
""") as demo:
|
77 |
gr.Markdown(project_description())
|
78 |
|
79 |
-
with gr.
|
80 |
-
with gr.
|
81 |
-
with gr.
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
interactive=True
|
88 |
)
|
|
|
89 |
|
90 |
-
|
91 |
-
word_input = gr.Textbox(label="輸入單字", visible=True)
|
92 |
-
num_input = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="抽取單字數量")
|
93 |
-
source_dropdown = gr.Dropdown(
|
94 |
-
choices=get_sources(),
|
95 |
-
value="common3000",
|
96 |
-
label="選擇單字庫"
|
97 |
-
)
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
|
116 |
-
result_output = gr.HTML(label="結果")
|
117 |
-
status_output = gr.Textbox(label="處理狀態", interactive=False)
|
118 |
|
119 |
-
with gr.Row():
|
120 |
-
generate_button = gr.Button("✨ 生成句子", elem_id="generate-button")
|
121 |
-
|
122 |
-
mode_radio.change(switch_mode, inputs=[mode_radio], outputs=[word_input, num_input])
|
123 |
-
use_ai_checkbox.change(toggle_ai_button, inputs=[use_ai_checkbox], outputs=[model_dropdown, ai_warning])
|
124 |
-
generate_button.click(process_sentence, inputs=[mode_radio, word_input, source_dropdown, num_input, use_ai_checkbox, model_dropdown], outputs=[result_output, status_output])
|
125 |
-
|
126 |
-
with gr.Tab("英文小考"):
|
127 |
-
quiz_source_dropdown = gr.Dropdown(
|
128 |
-
choices=get_sources(),
|
129 |
-
value="common3000",
|
130 |
-
label="選擇單字庫"
|
131 |
-
)
|
132 |
-
|
133 |
-
quiz_num_slider = gr.Slider(
|
134 |
-
minimum=1,
|
135 |
-
maximum=5,
|
136 |
-
value=3,
|
137 |
-
step=1,
|
138 |
-
label="選擇題數量"
|
139 |
-
)
|
140 |
-
|
141 |
-
quiz_generate_button = gr.Button("生成試卷")
|
142 |
-
quiz_output_area = gr.HTML(label="試卷區")
|
143 |
-
quiz_submit_button = gr.Button("提交答案")
|
144 |
-
quiz_score_display = gr.HTML(label="分數")
|
145 |
-
|
146 |
-
quiz_generate_button.click(fn=generate_fill_in_blank_exam, inputs=[quiz_source_dropdown, quiz_num_slider], outputs=[quiz_output_area])
|
147 |
-
quiz_submit_button.click(fn=check_exam, inputs=[], outputs=[quiz_score_display])
|
148 |
-
|
149 |
-
# 啟動
|
150 |
demo.launch()
|
|
|
3 |
from vocab import get_sources, get_words_from_source
|
4 |
from sentences import generate_sentences
|
5 |
from ai_sentence import MODEL_LIST
|
6 |
+
from quiz import generate_fill_in_blank_exam, check_exam, render_exam_interface
|
7 |
|
|
|
|
|
|
|
8 |
def process_sentence(mode, word, source, num, use_ai, model_name):
|
9 |
try:
|
10 |
if mode == '查詢單字':
|
|
|
28 |
except Exception as e:
|
29 |
return f"<p style='color:red;'>❌ 發生錯誤:{str(e)}</p>", f"錯誤:{str(e)}"
|
30 |
|
|
|
|
|
|
|
31 |
def project_description():
|
32 |
return """
|
33 |
# 📖 VocabLine 單字例句工具
|
|
|
49 |
- GitHub:[https://github.com/dayuian](https://github.com/dayuian)
|
50 |
"""
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
with gr.Blocks(css="""
|
53 |
#card-group { padding: 15px; border-radius: 12px; background-color: rgba(255, 255, 255, 0.05); box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); margin-bottom: 15px; }
|
54 |
.gradio-container { max-width: 800px; margin: auto; }
|
55 |
""") as demo:
|
56 |
gr.Markdown(project_description())
|
57 |
|
58 |
+
with gr.Tab("單字查詢/生成例句"):
|
59 |
+
with gr.Group():
|
60 |
+
with gr.Row():
|
61 |
+
mode_radio = gr.Radio(
|
62 |
+
["查詢單字", "隨機抽單字"],
|
63 |
+
label="選擇模式",
|
64 |
+
value="查詢單字",
|
65 |
+
interactive=True
|
66 |
+
)
|
67 |
+
|
68 |
+
with gr.Group(elem_id="card-group"):
|
69 |
+
word_input = gr.Textbox(label="輸入單字", visible=True)
|
70 |
+
num_input = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="抽取單字數量")
|
71 |
+
source_dropdown = gr.Dropdown(
|
72 |
+
choices=get_sources(),
|
73 |
+
value="common3000",
|
74 |
+
label="選擇單字庫"
|
75 |
+
)
|
76 |
+
|
77 |
+
with gr.Group(elem_id="card-group"):
|
78 |
+
use_ai_checkbox = gr.Checkbox(label="使用 AI 生成句子(較慢,約 30 秒)", elem_id="use-ai-checkbox")
|
79 |
+
|
80 |
+
with gr.Row():
|
81 |
+
model_dropdown = gr.Dropdown(
|
82 |
+
choices=MODEL_LIST,
|
83 |
+
value="gpt2",
|
84 |
+
label="選擇 AI 模型",
|
85 |
+
visible=False
|
86 |
+
)
|
87 |
+
gr.Markdown("🔷 **建議使用 GPT2(表現較佳),Pythia-410m 很爛慎選!**", visible=False)
|
88 |
+
|
89 |
+
ai_warning = gr.Textbox(
|
90 |
+
value="⚠️ 使用 AI 生成句子為功能測試,每一個單字的生成過程可能需要 30 秒以上,請耐心等待。",
|
91 |
+
visible=False,
|
92 |
+
interactive=False,
|
93 |
+
label=""
|
94 |
+
)
|
95 |
+
|
96 |
+
result_output = gr.HTML(label="結果")
|
97 |
+
status_output = gr.Textbox(label="處理狀態", interactive=False)
|
98 |
+
|
99 |
+
with gr.Row():
|
100 |
+
generate_button = gr.Button("✨ 生成句子", elem_id="generate-button")
|
101 |
+
|
102 |
+
with gr.Tab("英文小考"):
|
103 |
+
quiz_source_dropdown = gr.Dropdown(
|
104 |
+
choices=get_sources(),
|
105 |
+
value="common3000",
|
106 |
+
label="選擇單字庫"
|
107 |
+
)
|
108 |
+
quiz_num_slider = gr.Slider(minimum=1, maximum=10, value=2, step=1, label="題目數量")
|
109 |
+
|
110 |
+
quiz_generate_button = gr.Button("📄 生成試卷")
|
111 |
+
quiz_submit_button = gr.Button("✅ 提交試卷")
|
112 |
+
|
113 |
+
quiz_questions_container = gr.Column()
|
114 |
+
quiz_score_display = gr.HTML()
|
115 |
+
|
116 |
+
quiz_questions_state = gr.State([])
|
117 |
+
|
118 |
+
def display_exam(source, num):
|
119 |
+
questions = generate_fill_in_blank_exam(source, num)
|
120 |
+
quiz_questions_state.value = questions
|
121 |
+
|
122 |
+
radios = []
|
123 |
+
for i, q in enumerate(questions):
|
124 |
+
radios.append(
|
125 |
+
gr.Radio(
|
126 |
+
choices=q['options'],
|
127 |
+
label=f"第 {i + 1} 題:{q['sentence']}",
|
128 |
interactive=True
|
129 |
)
|
130 |
+
)
|
131 |
|
132 |
+
return radios
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
|
134 |
+
def submit_exam(*user_answers):
|
135 |
+
questions = quiz_questions_state.value
|
136 |
+
score_html = check_exam(user_answers, questions)
|
137 |
+
return score_html
|
138 |
+
|
139 |
+
quiz_generate_button.click(
|
140 |
+
display_exam,
|
141 |
+
inputs=[quiz_source_dropdown, quiz_num_slider],
|
142 |
+
outputs=quiz_questions_container
|
143 |
+
)
|
144 |
+
|
145 |
+
quiz_submit_button.click(
|
146 |
+
submit_exam,
|
147 |
+
inputs=quiz_questions_container,
|
148 |
+
outputs=quiz_score_display
|
149 |
+
)
|
150 |
|
|
|
|
|
151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
demo.launch()
|