harshith1411 commited on
Commit
3d7aaca
Β·
verified Β·
1 Parent(s): a8e7a5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -26
app.py CHANGED
@@ -14,6 +14,9 @@ st.subheader('πŸš€ Test Your Knowledge!')
14
  standard = st.text_input('πŸ“š Enter Standard/Grade')
15
  topic = st.text_input('πŸ“ Enter Topic')
16
 
 
 
 
17
  # πŸ”Ή Difficulty Level
18
  difficulty = st.radio("πŸ“Š Select Difficulty Level", ["Low", "Medium", "Hard"])
19
 
@@ -27,30 +30,50 @@ model = ChatGroq(
27
  model_name="llama-3.3-70b-versatile"
28
  )
29
 
30
- def detect_quiz_type(topic):
31
- """AI determines the best quiz type for the topic."""
32
- prompt = f"""
33
- Given the topic '{topic}', select the most suitable quiz type:
34
- - 'MCQ' for subjects like Science, Math, History.
35
- - 'True/False' for fact-based topics.
36
- - 'Fill in the Blanks' for language-based topics.
37
-
38
- Return only one of these: "MCQ", "True/False", "Fill in the Blanks".
39
- """
40
- response = model.predict(prompt).strip()
41
- if response in ["MCQ", "True/False", "Fill in the Blanks"]:
42
- return response
43
- return "MCQ" # Default to MCQ
44
-
45
  def generate_quiz(standard, topic, quiz_type, difficulty, num_questions):
46
- """Generate quiz using AI with strict JSON formatting."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  prompt = f"""
48
- Generate {num_questions} {quiz_type} questions for a {standard} student on {topic}.
 
 
49
 
50
- Format:
51
- {{"questions": [
52
- {{"question": "Example?", "options": ["A) Option1", "B) Option2"], "answer": "A) Option1"}}
53
- ]}}
54
  """
55
 
56
  try:
@@ -86,11 +109,10 @@ if 'user_answers' not in st.session_state:
86
  # πŸ”Ή Generate Quiz Button
87
  if st.button("πŸš€ Generate Quiz"):
88
  if standard and topic:
89
- auto_quiz_type = detect_quiz_type(topic) # AI selects quiz type
90
- st.session_state.quiz_data = generate_quiz(standard, topic, auto_quiz_type, difficulty, num_questions)
91
  if st.session_state.quiz_data:
92
  st.session_state.user_answers = {}
93
- st.success(f"βœ… {auto_quiz_type} Quiz Generated on '{topic}' with {num_questions} Questions!")
94
  st.balloons()
95
  else:
96
  st.error("⚠️ Please enter both the standard and topic.")
@@ -103,10 +125,12 @@ if st.session_state.quiz_data:
103
  for i, q in enumerate(questions):
104
  st.write(f"**Q{i+1}: {q['question']}**")
105
 
106
- if "options" in q:
107
  user_answer = st.radio(f"🧐 Select your answer for Question {i+1}", options=q["options"], key=f"question_{i+1}")
 
 
108
  else:
109
- user_answer = st.text_input(f"🧐 Your answer for Question {i+1}", key=f"question_{i+1}")
110
 
111
  st.session_state.user_answers[f"question_{i+1}"] = user_answer
112
 
 
14
  standard = st.text_input('πŸ“š Enter Standard/Grade')
15
  topic = st.text_input('πŸ“ Enter Topic')
16
 
17
+ # πŸ”Ή User Selects Quiz Type
18
+ quiz_type = st.radio("🎭 Select Quiz Type", ["MCQ", "True/False", "Fill in the Blanks"])
19
+
20
  # πŸ”Ή Difficulty Level
21
  difficulty = st.radio("πŸ“Š Select Difficulty Level", ["Low", "Medium", "Hard"])
22
 
 
30
  model_name="llama-3.3-70b-versatile"
31
  )
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  def generate_quiz(standard, topic, quiz_type, difficulty, num_questions):
34
+ """Generate topic-specific quiz using AI in JSON format."""
35
+
36
+ format_example = {
37
+ "MCQ": '''
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
+ }
47
+ ''',
48
+ "True/False": '''
49
+ {
50
+ "questions": [
51
+ {
52
+ "question": "The Sun is a planet.",
53
+ "answer": "False"
54
+ }
55
+ ]
56
+ }
57
+ ''',
58
+ "Fill in the Blanks": '''
59
+ {
60
+ "questions": [
61
+ {
62
+ "question": "The process by which plants make food is called ____.",
63
+ "answer": "Photosynthesis"
64
+ }
65
+ ]
66
+ }
67
+ '''
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:
 
109
  # πŸ”Ή Generate Quiz Button
110
  if st.button("πŸš€ Generate Quiz"):
111
  if standard and topic:
112
+ st.session_state.quiz_data = generate_quiz(standard, topic, quiz_type, difficulty, num_questions)
 
113
  if st.session_state.quiz_data:
114
  st.session_state.user_answers = {}
115
+ st.success(f"βœ… {quiz_type} Quiz Generated on '{topic}' with {num_questions} Questions!")
116
  st.balloons()
117
  else:
118
  st.error("⚠️ Please enter both the standard and topic.")
 
125
  for i, q in enumerate(questions):
126
  st.write(f"**Q{i+1}: {q['question']}**")
127
 
128
+ if quiz_type == "MCQ":
129
  user_answer = st.radio(f"🧐 Select your answer for Question {i+1}", options=q["options"], key=f"question_{i+1}")
130
+ elif quiz_type == "True/False":
131
+ user_answer = st.radio(f"🧐 Select True or False for Question {i+1}", options=["True", "False"], key=f"question_{i+1}")
132
  else:
133
+ user_answer = st.text_input(f"🧐 Fill in the blank for Question {i+1}", key=f"question_{i+1}")
134
 
135
  st.session_state.user_answers[f"question_{i+1}"] = user_answer
136