Poojashetty357 commited on
Commit
bad5305
Β·
verified Β·
1 Parent(s): f0cb605

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -22
app.py CHANGED
@@ -8,21 +8,19 @@ load_dotenv()
8
  api_key = os.getenv("OPENAI_API_KEY")
9
  client = OpenAI(api_key=api_key)
10
 
 
 
 
11
  # 🐍 Python Tutor Function
12
  def python_tutor(user_input):
 
 
13
  response = client.chat.completions.create(
14
  model="gpt-4.1-mini",
15
  messages=[
16
  {
17
  "role": "system",
18
- "content": """You are a Python tutor. Help users understand Python with short examples. Be kind, clear, and only answer Python-related questions.
19
-
20
- 1. Confirm if the question is Python-related. If not, politely inform the user and refrain from answering.
21
- 2. Provide a concise initial answer:
22
- - Include a brief explanation.
23
- - Provide a straightforward code example.
24
- 3. Keep the quiz simple and give options to choose. If wrong answers are selected, correct them with an explanation.
25
- """
26
  },
27
  {"role": "user", "content": user_input}
28
  ],
@@ -31,16 +29,30 @@ def python_tutor(user_input):
31
  )
32
  return response.choices[0].message.content
33
 
34
- # πŸ“š Simple Quiz Generator (fixed question for now)
35
- def ask_question():
36
- return "What is the output of print(2 + 3 * 4)?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- def evaluate_answer(user_answer):
39
- correct_answer = "14"
40
- if user_answer.strip() == correct_answer:
41
- return "βœ… Correct! Great job!"
42
- else:
43
- return "❌ Incorrect. Try again. Hint: Use BODMAS."
44
 
45
  # 🎨 Gradio UI with Two Pages
46
  with gr.Blocks() as app:
@@ -51,10 +63,9 @@ with gr.Blocks() as app:
51
  gr.Button("Ask").click(python_tutor, inputs=question_input, outputs=answer_output)
52
 
53
  with gr.Tab("πŸ§ͺ Mini Quiz"):
54
- gr.Markdown("### Let's test your Python basics!")
55
- quiz_question = gr.Textbox(label="Question", interactive=False, value=ask_question())
56
- user_response = gr.Textbox(label="Your Answer")
57
- result_output = gr.Textbox(label="Result")
58
- gr.Button("Submit Answer").click(evaluate_answer, inputs=user_response, outputs=result_output)
59
 
60
  app.launch()
 
8
  api_key = os.getenv("OPENAI_API_KEY")
9
  client = OpenAI(api_key=api_key)
10
 
11
+ # ✨ Global variable to store last topic
12
+ last_user_question = ""
13
+
14
  # 🐍 Python Tutor Function
15
  def python_tutor(user_input):
16
+ global last_user_question
17
+ last_user_question = user_input # store latest question for quiz
18
  response = client.chat.completions.create(
19
  model="gpt-4.1-mini",
20
  messages=[
21
  {
22
  "role": "system",
23
+ "content": """You are a Python tutor. Help users understand Python with short examples. Be kind, clear, and only answer Python-related questions."""
 
 
 
 
 
 
 
24
  },
25
  {"role": "user", "content": user_input}
26
  ],
 
29
  )
30
  return response.choices[0].message.content
31
 
32
+ # πŸ“š Dynamic Quiz Generator Based on User Topic
33
+ def generate_quiz():
34
+ global last_user_question
35
+ if not last_user_question:
36
+ return "Please ask a Python question first in the tutor tab."
37
+
38
+ quiz_prompt = f"""Generate one simple multiple-choice quiz question based on this Python topic/question: "{last_user_question}".
39
+ Format:
40
+ Question: ...
41
+ Options: A. ... B. ... C. ... D. ...
42
+ Answer: ..."""
43
+
44
+ response = client.chat.completions.create(
45
+ model="gpt-4.1-mini",
46
+ messages=[
47
+ {"role": "system", "content": "You are a quiz generator for Python topics."},
48
+ {"role": "user", "content": quiz_prompt}
49
+ ],
50
+ temperature=0.3,
51
+ max_tokens=300
52
+ )
53
 
54
+ quiz_text = response.choices[0].message.content.strip()
55
+ return quiz_text
 
 
 
 
56
 
57
  # 🎨 Gradio UI with Two Pages
58
  with gr.Blocks() as app:
 
63
  gr.Button("Ask").click(python_tutor, inputs=question_input, outputs=answer_output)
64
 
65
  with gr.Tab("πŸ§ͺ Mini Quiz"):
66
+ gr.Markdown("### Your Custom Python Quiz Based on Tutor Topic")
67
+ quiz_box = gr.Textbox(label="Quiz Question", lines=4)
68
+ generate_btn = gr.Button("πŸ”„ Generate Quiz")
69
+ generate_btn.click(fn=generate_quiz, outputs=quiz_box)
 
70
 
71
  app.launch()