| |
|
| | import os |
| | import gradio as gr |
| | from openai import OpenAI |
| | from dotenv import load_dotenv |
| | import re |
| |
|
| | |
| | load_dotenv() |
| | api_key = os.getenv("OPENAI_API_KEY") |
| | client = OpenAI(api_key=api_key) |
| |
|
| | |
| | last_user_question = "" |
| | last_correct_answer = "" |
| | quiz_question_text = "" |
| | quiz_options = [] |
| |
|
| | |
| | def python_tutor(user_input): |
| | global last_user_question |
| | last_user_question = user_input |
| | response = client.chat.completions.create( |
| | model="gpt-4.1-mini", |
| | messages=[ |
| | { |
| | "role": "system", |
| | "content": """You are a Python tutor. Help users understand Python with short examples. Be kind, clear, and only answer Python-related questions.""" |
| | }, |
| | {"role": "user", "content": user_input} |
| | ], |
| | temperature=0.1, |
| | max_tokens=500 |
| | ) |
| | return response.choices[0].message.content |
| |
|
| | |
| | def generate_quiz(): |
| | global last_user_question, last_correct_answer, quiz_question_text, quiz_options |
| |
|
| | if not last_user_question: |
| | return "Please ask a Python question first in the tutor tab.", gr.update(choices=[]) |
| |
|
| | quiz_prompt = f"""Generate one simple multiple-choice quiz question based on this Python topic/question: "{last_user_question}". |
| | Format: |
| | Question: ... |
| | Options: |
| | A. ... |
| | B. ... |
| | C. ... |
| | D. ... |
| | Answer: ...""" |
| |
|
| | response = client.chat.completions.create( |
| | model="gpt-4.1-mini", |
| | messages=[ |
| | {"role": "system", "content": "You are a quiz generator for Python topics."}, |
| | {"role": "user", "content": quiz_prompt} |
| | ], |
| | temperature=0.3, |
| | max_tokens=300 |
| | ) |
| |
|
| | content = response.choices[0].message.content.strip() |
| |
|
| | |
| | question_match = re.search(r"Question:\s*(.*)", content) |
| | options_match = re.findall(r"[A-D]\.\s*(.*)", content) |
| | answer_match = re.search(r"Answer:\s*([A-D])", content) |
| |
|
| | if not (question_match and options_match and answer_match): |
| | return "β Error parsing quiz content.", gr.update(choices=[]) |
| |
|
| | quiz_question_text = question_match.group(1) |
| | quiz_options = options_match |
| | last_correct_answer = answer_match.group(1).strip().upper() |
| |
|
| | return quiz_question_text, gr.update(choices=quiz_options, value=None) |
| |
|
| | |
| | def evaluate_user_answer(user_choice): |
| | if not user_choice: |
| | return "Please select an answer." |
| |
|
| | correct_index = ord(last_correct_answer) - ord("A") |
| | correct_option = quiz_options[correct_index] |
| |
|
| | if user_choice.strip() == correct_option.strip(): |
| | return "β
Correct! Well done!" |
| | else: |
| | return f"β Incorrect. The correct answer is: {last_correct_answer}. {correct_option}" |
| |
|
| | |
| | with gr.Blocks() as app: |
| | with gr.Tab("π Python Tutor"): |
| | gr.Markdown("### Ask your Python question:") |
| | question_input = gr.Textbox(lines=2, label="Your Question") |
| | answer_output = gr.Textbox(label="Tutor's Answer") |
| | gr.Button("Ask").click(python_tutor, inputs=question_input, outputs=answer_output) |
| |
|
| | with gr.Tab("π§ͺ Mini Quiz"): |
| | gr.Markdown("### Quiz Based on Your Tutor Question") |
| | quiz_question = gr.Textbox(label="Generated Quiz Question", interactive=False) |
| | options_dropdown = gr.Dropdown(label="Choose Your Answer", choices=[]) |
| | result_output = gr.Textbox(label="Result") |
| | generate_btn = gr.Button("π Generate Quiz") |
| | submit_btn = gr.Button("β
Submit Answer") |
| |
|
| | generate_btn.click(fn=generate_quiz, outputs=[quiz_question, options_dropdown]) |
| | submit_btn.click(fn=evaluate_user_answer, inputs=options_dropdown, outputs=result_output) |
| |
|
| | app.launch() |
| |
|