rivapereira123 commited on
Commit
dc7ef19
Β·
verified Β·
1 Parent(s): da764a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -36
app.py CHANGED
@@ -39,6 +39,7 @@ scenario_builder = None
39
 
40
  import gradio as gr
41
 
 
42
  def build_dynamic_mcq_ui(scenario_data: list[dict[str, any]]):
43
  with gr.Blocks() as mcq_interface:
44
  gr.Markdown("## πŸ§ͺ Interactive Scenario Quiz")
@@ -95,51 +96,61 @@ def generate_scenario_ui(query: str, num_questions: int = 5):
95
  loop.close()
96
 
97
  if "error" in result:
98
- return gr.Markdown(f"❌ Error: {result['error']}\nDetails: {result['details']}")
99
 
100
  questions = result.get("questions", [])
101
 
102
- with gr.Blocks() as quiz_ui:
103
- gr.Markdown(f"## πŸ§ͺ Interactive Quiz for: {query}")
104
- answer_radios = []
105
-
106
- for i, q in enumerate(questions):
107
- gr.Markdown(f"**Q{i+1}: {q['question']}**")
108
- opts = [f"{k}: {v}" for k, v in q["options"].items()]
109
- radio = gr.Radio(choices=opts, label=None, type="index")
110
- answer_radios.append(radio)
111
-
112
- submit_btn = gr.Button("βœ… Submit")
113
- result_output = gr.Markdown()
114
-
115
- def evaluate(*user_indices):
116
- results = []
117
- for idx, selected in enumerate(user_indices):
118
- correct_key = questions[idx]["correct_answer"]
119
- correct_label = f"{correct_key}: {questions[idx]['options'][correct_key]}"
120
- selected_key = list(questions[idx]["options"].keys())[selected] if selected is not None else "N/A"
121
- selected_label = f"{selected_key}: {questions[idx]['options'].get(selected_key, 'No answer')}"
122
- is_correct = selected_key == correct_key
123
- results.append(
124
- f"""
 
 
 
 
 
 
 
125
  ### Q{idx+1}
126
  **Your answer:** {selected_label}
127
  **Correct answer:** {correct_label}
128
- {"βœ… Correct!" if is_correct else "❌ Incorrect."}
129
- 🧠 {questions[idx]['feedback']}
 
130
  """)
131
- return "\n".join(results)
132
 
133
- submit_btn.click(
134
- fn=evaluate,
135
- inputs=answer_radios,
136
- outputs=result_output
137
- )
138
 
139
- return quiz_ui
140
 
141
  except Exception as e:
142
- return gr.Markdown(f"❌ Unexpected error: {str(e)}")
 
 
143
 
144
 
145
 
@@ -314,13 +325,20 @@ def create_optimized_gradio_interface():
314
  with gr.Tab("πŸ§ͺ Scenario Generator"):
315
  gr.Markdown("### Generate an Interactive Medical Scenario")
316
  num_questions_slider = gr.Slider(1, 10, value=5, step=1, label="Number of Questions")
317
- scenario_output = gr.Column()
318
  scenario_submit = gr.Button("πŸš€ Generate Scenario")
319
  # βœ… Keep the click handler INSIDE here
320
  scenario_submit.click(
321
  fn=generate_scenario_ui,
322
  inputs=[query_input, num_questions_slider],
323
- outputs=scenario_output
 
 
 
 
 
 
 
324
  )
325
 
326
  submit_btn.click(
 
39
 
40
  import gradio as gr
41
 
42
+
43
  def build_dynamic_mcq_ui(scenario_data: list[dict[str, any]]):
44
  with gr.Blocks() as mcq_interface:
45
  gr.Markdown("## πŸ§ͺ Interactive Scenario Quiz")
 
96
  loop.close()
97
 
98
  if "error" in result:
99
+ return [gr.Markdown(f"❌ Error: {result['error']}\nDetails: {result['details']}")]
100
 
101
  questions = result.get("questions", [])
102
 
103
+ # Create a list to hold the Gradio components for the quiz
104
+ quiz_components = []
105
+ answer_radios = []
106
+
107
+ quiz_components.append(gr.Markdown(f"## πŸ§ͺ Interactive Quiz for: {query}", elem_classes=["quiz-header"]))
108
+
109
+ for i, q in enumerate(questions):
110
+ quiz_components.append(gr.Markdown(f"**Q{i+1}: {q['question']}**", elem_classes=["quiz-question"]))
111
+ opts = [f"{k}: {v}" for k, v in q["options"].items()]
112
+ radio = gr.Radio(choices=opts, label=None, type="index", elem_classes=["quiz-options"])
113
+ answer_radios.append(radio)
114
+ quiz_components.append(radio)
115
+
116
+ submit_btn = gr.Button("βœ… Submit", elem_classes=["quiz-submit-btn"])
117
+ result_output = gr.Markdown(elem_classes=["quiz-results"])
118
+ quiz_components.append(submit_btn)
119
+ quiz_components.append(result_output)
120
+
121
+ def evaluate(*user_indices):
122
+ results = []
123
+ for idx, selected in enumerate(user_indices):
124
+ correct_key = questions[idx]["correct_answer"]
125
+ correct_label = f"{correct_key}: {questions[idx]['options'][correct_key]}"
126
+ selected_key = list(questions[idx]["options"].keys())[selected] if selected is not None else "N/A"
127
+ selected_label = f"{selected_key}: {questions[idx]['options'].get(selected_key, 'No answer')}"
128
+ is_correct = selected_key == correct_key
129
+ result_class = "quiz-result-correct" if is_correct else "quiz-result-incorrect"
130
+ results.append(
131
+ f"""
132
+ <div class="quiz-result-item {result_class}">
133
  ### Q{idx+1}
134
  **Your answer:** {selected_label}
135
  **Correct answer:** {correct_label}
136
+ {"βœ… Correct!" if is_correct else "❌ Incorrect."}
137
+ <div class="quiz-feedback">🧠 {questions[idx]['feedback']}</div>
138
+ </div>
139
  """)
140
+ return "\n".join(results)
141
 
142
+ submit_btn.click(
143
+ fn=evaluate,
144
+ inputs=answer_radios,
145
+ outputs=result_output
146
+ )
147
 
148
+ return quiz_components
149
 
150
  except Exception as e:
151
+ return [gr.Markdown(f"❌ Unexpected error: {str(e)}")]
152
+
153
+
154
 
155
 
156
 
 
325
  with gr.Tab("πŸ§ͺ Scenario Generator"):
326
  gr.Markdown("### Generate an Interactive Medical Scenario")
327
  num_questions_slider = gr.Slider(1, 10, value=5, step=1, label="Number of Questions")
328
+ scenario_output = gr.Column(visible=False, elem_classes=["quiz-container"])
329
  scenario_submit = gr.Button("πŸš€ Generate Scenario")
330
  # βœ… Keep the click handler INSIDE here
331
  scenario_submit.click(
332
  fn=generate_scenario_ui,
333
  inputs=[query_input, num_questions_slider],
334
+ outputs=scenario_output,
335
+ show_progress='full'
336
+ )
337
+
338
+ scenario_submit.click(
339
+ lambda: gr.update(visible=True),
340
+ outputs=[scenario_output],
341
+ queue=False
342
  )
343
 
344
  submit_btn.click(