dayuian commited on
Commit
53091ac
·
verified ·
1 Parent(s): f0b8f19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -23
app.py CHANGED
@@ -5,7 +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
  def process_sentence(mode, word, source, num, use_ai, model_name):
10
  try:
11
  if mode == '查詢單字':
@@ -29,7 +28,6 @@ def process_sentence(mode, word, source, num, use_ai, model_name):
29
  except Exception as e:
30
  return f"<p style='color:red;'>❌ 發生錯誤:{str(e)}</p>", f"錯誤:{str(e)}"
31
 
32
-
33
  def project_description():
34
  return """
35
  # 📖 VocabLine 單字例句工具
@@ -51,7 +49,6 @@ def project_description():
51
  - GitHub:[https://github.com/dayuian](https://github.com/dayuian)
52
  """
53
 
54
-
55
  with gr.Blocks(css="""
56
  #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; }
57
  .gradio-container { max-width: 800px; margin: auto; }
@@ -127,47 +124,44 @@ with gr.Blocks(css="""
127
  quiz_generate_button = gr.Button("📄 生成試卷")
128
  quiz_submit_button = gr.Button("✅ 提交試卷")
129
 
130
- quiz_questions_container = gr.Column()
131
  quiz_score_display = gr.HTML()
132
-
133
  quiz_questions_state = gr.State([])
134
 
 
 
 
 
 
 
135
  def display_exam(source, num):
136
  questions = generate_fill_in_blank_exam(source, num)
137
  quiz_questions_state.value = questions
138
 
139
- radios = []
140
  for i, q in enumerate(questions):
141
- radios.append(
142
- gr.Radio(
143
- choices=q['options'],
144
- label=f"第 {i + 1} 題:{q['sentence']}",
145
- interactive=True
146
- )
147
  )
 
 
148
 
149
- return radios
150
 
151
  def submit_exam(*user_answers):
152
  questions = quiz_questions_state.value
153
- score, results = check_exam(user_answers, questions)
154
- result_html = f"<p><strong>分數:</strong> {score}</p>"
155
-
156
- for res in results:
157
- result_html += f"<p>{res['question']}<br> 你的答案:{res['user_answer']} <br> 正確答案:{res['correct_answer']} <br> {'✅' if res['is_correct'] else '❌'}</p>"
158
-
159
- return result_html
160
 
161
  quiz_generate_button.click(
162
  display_exam,
163
  inputs=[quiz_source_dropdown, quiz_num_slider],
164
- outputs=quiz_questions_container
165
  )
166
 
167
  quiz_submit_button.click(
168
  submit_exam,
169
- inputs=quiz_questions_container,
170
  outputs=quiz_score_display
171
  )
172
 
173
- 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 == '查詢單字':
 
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; }
 
124
  quiz_generate_button = gr.Button("📄 生成試卷")
125
  quiz_submit_button = gr.Button("✅ 提交試卷")
126
 
 
127
  quiz_score_display = gr.HTML()
 
128
  quiz_questions_state = gr.State([])
129
 
130
+ radios = []
131
+ for i in range(5):
132
+ radios.append(
133
+ gr.Radio(choices=[], label=f"第 {i + 1} 題", visible=False, interactive=True)
134
+ )
135
+
136
  def display_exam(source, num):
137
  questions = generate_fill_in_blank_exam(source, num)
138
  quiz_questions_state.value = questions
139
 
140
+ updates = []
141
  for i, q in enumerate(questions):
142
+ updates.append(
143
+ gr.update(choices=q["options"], label=f"第 {i + 1} 題:{q['sentence']}", visible=True)
 
 
 
 
144
  )
145
+ for i in range(len(questions), 5):
146
+ updates.append(gr.update(visible=False))
147
 
148
+ return updates
149
 
150
  def submit_exam(*user_answers):
151
  questions = quiz_questions_state.value
152
+ score_html = check_exam(user_answers[:len(questions)], questions)
153
+ return score_html
 
 
 
 
 
154
 
155
  quiz_generate_button.click(
156
  display_exam,
157
  inputs=[quiz_source_dropdown, quiz_num_slider],
158
+ outputs=radios
159
  )
160
 
161
  quiz_submit_button.click(
162
  submit_exam,
163
+ inputs=radios,
164
  outputs=quiz_score_display
165
  )
166
 
167
+ demo.launch()