Update app.py
Browse files
app.py
CHANGED
@@ -19,7 +19,7 @@ def generate_math_quiz(role, topic, difficulty, num_questions):
|
|
19 |
difficulty (str): The difficulty level (e.g., Easy, Medium, Hard).
|
20 |
num_questions (int): The number of questions to generate.
|
21 |
Returns:
|
22 |
-
|
23 |
"""
|
24 |
# Construct the prompt for the model
|
25 |
prompt = (
|
@@ -32,62 +32,7 @@ def generate_math_quiz(role, topic, difficulty, num_questions):
|
|
32 |
response = llm.invoke(prompt)
|
33 |
|
34 |
# Extract the content from the response
|
35 |
-
|
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 |
-
"""
|
64 |
-
Display quiz questions and collect answers from the user.
|
65 |
-
Args:
|
66 |
-
questions (list): List of quiz questions.
|
67 |
-
Returns:
|
68 |
-
dict: User's answers.
|
69 |
-
"""
|
70 |
-
user_answers = {}
|
71 |
-
st.subheader("Quiz Questions")
|
72 |
-
for i, question in enumerate(questions):
|
73 |
-
user_answers[i] = st.text_input(f"Q{i + 1}: {question}", key=f"q{i}")
|
74 |
-
|
75 |
-
return user_answers
|
76 |
-
|
77 |
-
def calculate_score(user_answers, correct_answers):
|
78 |
-
"""
|
79 |
-
Calculate the score based on the user's answers.
|
80 |
-
Args:
|
81 |
-
user_answers (dict): User's answers.
|
82 |
-
correct_answers (dict): Correct answers.
|
83 |
-
Returns:
|
84 |
-
int: User's score.
|
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 |
|
92 |
# Create a form for user input
|
93 |
with st.form("quiz_form"):
|
@@ -110,29 +55,7 @@ with st.form("quiz_form"):
|
|
110 |
if submitted:
|
111 |
if role and topic and difficulty:
|
112 |
# Generate quiz questions based on the user input
|
113 |
-
|
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 |
-
|
|
|
19 |
difficulty (str): The difficulty level (e.g., Easy, Medium, Hard).
|
20 |
num_questions (int): The number of questions to generate.
|
21 |
Returns:
|
22 |
+
str: The generated quiz questions.
|
23 |
"""
|
24 |
# Construct the prompt for the model
|
25 |
prompt = (
|
|
|
32 |
response = llm.invoke(prompt)
|
33 |
|
34 |
# Extract the content from the response
|
35 |
+
return response.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
# Create a form for user input
|
38 |
with st.form("quiz_form"):
|
|
|
55 |
if submitted:
|
56 |
if role and topic and difficulty:
|
57 |
# Generate quiz questions based on the user input
|
58 |
+
quiz = generate_math_quiz(role, topic, difficulty, num_questions)
|
59 |
+
st.info(quiz) # Display the generated quiz questions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
else:
|
61 |
st.error("Please fill in all fields to generate the quiz.")
|
|