harshith1411 commited on
Commit
34e467f
·
verified ·
1 Parent(s): 3d7aaca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -10
app.py CHANGED
@@ -38,9 +38,9 @@ def generate_quiz(standard, topic, quiz_type, difficulty, num_questions):
38
  {
39
  "questions": [
40
  {
41
- "question": "What is the chemical symbol for water?",
42
- "options": ["A) O2", "B) CO2", "C) H2O", "D) N2"],
43
- "answer": "C) H2O"
44
  }
45
  ]
46
  }
@@ -49,8 +49,8 @@ def generate_quiz(standard, topic, quiz_type, difficulty, num_questions):
49
  {
50
  "questions": [
51
  {
52
- "question": "The Sun is a planet.",
53
- "answer": "False"
54
  }
55
  ]
56
  }
@@ -59,8 +59,8 @@ def generate_quiz(standard, topic, quiz_type, difficulty, num_questions):
59
  {
60
  "questions": [
61
  {
62
- "question": "The process by which plants make food is called ____.",
63
- "answer": "Photosynthesis"
64
  }
65
  ]
66
  }
@@ -68,16 +68,20 @@ def generate_quiz(standard, topic, quiz_type, difficulty, num_questions):
68
  }
69
 
70
  prompt = f"""
71
- Generate {num_questions} {quiz_type} questions for a {standard} grade student on the topic '{topic}' with {difficulty} difficulty.
72
 
73
- Ensure the quiz is strictly about '{topic}'.
74
 
75
- Return strictly in JSON format like this:
76
  {format_example[quiz_type]}
77
  """
78
 
79
  try:
80
  response = model.predict(prompt).strip()
 
 
 
 
81
  quiz_data = json.loads(response)
82
  return quiz_data if "questions" in quiz_data else None
83
  except json.JSONDecodeError:
@@ -165,3 +169,4 @@ if st.button("✅ Submit Quiz"):
165
  st.error("⚠️ Quiz data not available. Please regenerate the quiz.")
166
 
167
 
 
 
38
  {
39
  "questions": [
40
  {
41
+ "question": "What is the capital of France?",
42
+ "options": ["A) Berlin", "B) Madrid", "C) Paris", "D) Rome"],
43
+ "answer": "C) Paris"
44
  }
45
  ]
46
  }
 
49
  {
50
  "questions": [
51
  {
52
+ "question": "The Sun is a star.",
53
+ "answer": "True"
54
  }
55
  ]
56
  }
 
59
  {
60
  "questions": [
61
  {
62
+ "question": "The powerhouse of the cell is the ____.",
63
+ "answer": "Mitochondria"
64
  }
65
  ]
66
  }
 
68
  }
69
 
70
  prompt = f"""
71
+ You are a quiz generator bot. Generate a {quiz_type} quiz with {num_questions} questions for a {standard} grade student on the topic '{topic}' with {difficulty} difficulty.
72
 
73
+ Strictly return the response in JSON format.
74
 
75
+ Format Example:
76
  {format_example[quiz_type]}
77
  """
78
 
79
  try:
80
  response = model.predict(prompt).strip()
81
+ json_start = response.find('{')
82
+ json_end = response.rfind('}')
83
+ if json_start != -1 and json_end != -1:
84
+ response = response[json_start:json_end+1] # Extract valid JSON
85
  quiz_data = json.loads(response)
86
  return quiz_data if "questions" in quiz_data else None
87
  except json.JSONDecodeError:
 
169
  st.error("⚠️ Quiz data not available. Please regenerate the quiz.")
170
 
171
 
172
+