md-qa-test / app.py
ambrosfitz's picture
Create app.py
a99257b verified
raw
history blame
1.94 kB
import gradio as gr
from question_generator import generate_microbiology_question
current_question = None
def generate_question():
global current_question
current_question = generate_microbiology_question()
return (
current_question['question'],
current_question['options']['A'],
current_question['options']['B'],
current_question['options']['C'],
current_question['options']['D'],
current_question['options']['E'],
)
def check_answer(choice):
if current_question is None:
return "Please generate a question first."
correct = current_question['correct_answer']
if choice == correct:
result = "Correct!"
else:
result = f"Incorrect. The correct answer is {correct}."
return f"{result}\n\nExplanation: {current_question['explanation']}\n\nMedical Reasoning: {current_question['medical_reasoning']}"
with gr.Blocks() as demo:
gr.Markdown("# Microbiology NBME Question Generator")
with gr.Row():
generate_btn = gr.Button("Generate Question")
with gr.Row():
question_text = gr.Textbox(label="Question", lines=4)
with gr.Row():
option_a = gr.Button("A")
option_b = gr.Button("B")
option_c = gr.Button("C")
option_d = gr.Button("D")
option_e = gr.Button("E")
with gr.Row():
result = gr.Textbox(label="Result", lines=10)
generate_btn.click(
generate_question,
outputs=[question_text, option_a, option_b, option_c, option_d, option_e]
)
option_a.click(lambda: check_answer("A"), outputs=result)
option_b.click(lambda: check_answer("B"), outputs=result)
option_c.click(lambda: check_answer("C"), outputs=result)
option_d.click(lambda: check_answer("D"), outputs=result)
option_e.click(lambda: check_answer("E"), outputs=result)
if __name__ == "__main__":
demo.launch()