rashid01 commited on
Commit
c555ce4
·
verified ·
1 Parent(s): 2517898

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -26
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
- # For demonstration, let's assume quiz_data is in the format "Q1: question1, A1: answer1; Q2: question2, A2: answer2"
 
 
 
38
  questions = []
39
- answers = {}
40
- for item in quiz_data.split(';'):
41
- if 'Q' in item and 'A' in item:
42
- q, a = item.split(', ')
43
- questions.append(q.split(': ')[1])
44
- answers[q.split(': ')[0]] = a.split(': ')[1]
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- return questions, answers
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'Q{i + 1}', '').strip().lower():
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
- # Display the quiz
102
- user_answers = display_quiz(questions)
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
- # Display results
112
- st.success(f"Your score: {score} / {len(questions)}")
113
- st.balloons()
114
 
115
- # Optional: Show correct answers
116
- st.info("Correct Answers:")
117
- for i, (question, answer) in enumerate(correct_answers.items()):
118
- st.write(f"{question}: {answer}")
 
 
 
 
 
 
 
 
 
 
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