Update app.py
Browse files
app.py
CHANGED
@@ -34,16 +34,30 @@ def generate_math_quiz(role, topic, difficulty, num_questions):
|
|
34 |
# Extract the content from the response
|
35 |
quiz_data = response.content
|
36 |
|
37 |
-
#
|
|
|
|
|
|
|
38 |
questions = []
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
return questions,
|
47 |
|
48 |
def display_quiz(questions):
|
49 |
"""
|
@@ -71,7 +85,7 @@ def calculate_score(user_answers, correct_answers):
|
|
71 |
"""
|
72 |
score = 0
|
73 |
for i, answer in user_answers.items():
|
74 |
-
if answer.strip().lower() == correct_answers.get(f'
|
75 |
score += 1
|
76 |
return score
|
77 |
|
@@ -98,25 +112,27 @@ with st.form("quiz_form"):
|
|
98 |
# Generate quiz questions based on the user input
|
99 |
questions, correct_answers = generate_math_quiz(role, topic, difficulty, num_questions)
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
# Button to submit answers
|
105 |
-
submit_answers = st.button("Submit Answers")
|
106 |
-
|
107 |
-
if submit_answers:
|
108 |
-
# Calculate score
|
109 |
-
score = calculate_score(user_answers, correct_answers)
|
110 |
|
111 |
-
#
|
112 |
-
st.
|
113 |
-
st.balloons()
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
else:
|
120 |
st.error("Please fill in all fields to generate the quiz.")
|
121 |
|
122 |
-
|
|
|
34 |
# Extract the content from the response
|
35 |
quiz_data = response.content
|
36 |
|
37 |
+
# Debugging: Print the raw quiz data
|
38 |
+
st.write("Raw quiz data:", quiz_data)
|
39 |
+
|
40 |
+
# Initialize lists and dictionary
|
41 |
questions = []
|
42 |
+
correct_answers = {}
|
43 |
+
|
44 |
+
try:
|
45 |
+
# Example format parsing
|
46 |
+
items = quiz_data.split('\n')
|
47 |
+
for item in items:
|
48 |
+
if item.startswith("Q"):
|
49 |
+
# This is a question
|
50 |
+
q_number, q_text = item.split(': ', 1)
|
51 |
+
questions.append(q_text)
|
52 |
+
elif item.startswith("A"):
|
53 |
+
# This is an answer
|
54 |
+
a_number, a_text = item.split(': ', 1)
|
55 |
+
correct_answers[a_number] = a_text
|
56 |
+
|
57 |
+
except Exception as e:
|
58 |
+
st.error(f"Error parsing quiz data: {e}")
|
59 |
|
60 |
+
return questions, correct_answers
|
61 |
|
62 |
def display_quiz(questions):
|
63 |
"""
|
|
|
85 |
"""
|
86 |
score = 0
|
87 |
for i, answer in user_answers.items():
|
88 |
+
if answer.strip().lower() == correct_answers.get(f'A{i + 1}', '').strip().lower():
|
89 |
score += 1
|
90 |
return score
|
91 |
|
|
|
112 |
# Generate quiz questions based on the user input
|
113 |
questions, correct_answers = generate_math_quiz(role, topic, difficulty, num_questions)
|
114 |
|
115 |
+
if questions:
|
116 |
+
# Display the quiz
|
117 |
+
user_answers = display_quiz(questions)
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
|
119 |
+
# Button to submit answers
|
120 |
+
submit_answers = st.button("Submit Answers")
|
|
|
121 |
|
122 |
+
if submit_answers:
|
123 |
+
# Calculate score
|
124 |
+
score = calculate_score(user_answers, correct_answers)
|
125 |
+
|
126 |
+
# Display results
|
127 |
+
st.success(f"Your score: {score} / {len(questions)}")
|
128 |
+
st.balloons()
|
129 |
+
|
130 |
+
# Optional: Show correct answers
|
131 |
+
st.info("Correct Answers:")
|
132 |
+
for i, (question, answer) in enumerate(correct_answers.items()):
|
133 |
+
st.write(f"{question}: {answer}")
|
134 |
+
else:
|
135 |
+
st.error("No questions generated. Please try again.")
|
136 |
else:
|
137 |
st.error("Please fill in all fields to generate the quiz.")
|
138 |
|
|