import gradio as gr from question_generator import generate_microbiology_question import logging import traceback # Set up logging logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') current_question = None def generate_question(): global current_question try: current_question = generate_microbiology_question() logging.info("Successfully generated a new 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'], "" # Clear any previous error messages ) except Exception as e: error_message = f"Error generating question: {str(e)}\n\n{traceback.format_exc()}" logging.error(error_message) return ("Error generating question. Please try again.", "", "", "", "", "", error_message) 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) with gr.Row(): error_message = gr.Textbox(label="Error", lines=5) generate_btn.click( generate_question, outputs=[question_text, option_a, option_b, option_c, option_d, option_e, error_message] ) 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__": logging.info("Starting the Microbiology NBME Question Generator app") demo.launch()