Muthuraja18 commited on
Commit
c5293f0
·
verified ·
1 Parent(s): 967620f
Files changed (1) hide show
  1. app.py +54 -31
app.py CHANGED
@@ -147,28 +147,23 @@ questions_db = {
147
 
148
  def generate_ai_questions(topic, n=5):
149
  """
150
- Returns list of (question, options[list], correct)
151
- Works ONLY in Online mode.
152
  """
 
153
  if not GROQ_API_KEY:
 
154
  return []
155
 
156
- prompt = f"""
157
- Generate {n} multiple choice quiz questions on topic: "{topic}"
158
-
159
- Rules:
160
- - 4 options per question
161
- - One correct answer
162
- - JSON output ONLY
163
- - Format:
164
- [
165
- {{
166
- "question": "...",
167
- "options": ["A","B","C","D"],
168
- "answer": "A"
169
- }}
170
- ]
171
- """
172
 
173
  try:
174
  r = requests.post(
@@ -178,27 +173,55 @@ Rules:
178
  "Content-Type": "application/json"
179
  },
180
  json={
181
- "model": GROQ_MODEL,
182
- "messages": [{"role": "user", "content": prompt}],
183
- "temperature": 0.4
 
 
 
 
 
184
  },
185
  timeout=30
186
  )
187
- data = r.json()
188
- text = data["choices"][0]["message"]["content"]
189
- parsed = json.loads(text)
190
 
191
- out = []
192
- for q in parsed:
193
- out.append(
194
- (q["question"], q["options"], q["answer"])
195
- )
196
- return out
197
 
 
 
 
 
198
  except Exception as e:
199
- print("Groq error:", e)
 
200
  return []
201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
  def load_json(path, default):
203
  if os.path.exists(path):
204
  try:
 
147
 
148
  def generate_ai_questions(topic, n=5):
149
  """
150
+ Generates n questions for a topic using Groq Chat API.
151
+ Returns list of (question, options, answer) tuples.
152
  """
153
+
154
  if not GROQ_API_KEY:
155
+ st.error("Groq API key not set.")
156
  return []
157
 
158
+ # 1) Clear instruction for Groq model
159
+ system_prompt = (
160
+ f"You are a quiz maker. Create {n} multiple‑choice questions about '{topic}'. "
161
+ "Respond ONLY as valid JSON in this format:\n"
162
+ "[\n"
163
+ " {\"question\":\"...\",\"options\":[\"A\",\"B\",\"C\",\"D\"],\"answer\":\"A\"},\n"
164
+ " ...\n"
165
+ "]"
166
+ )
 
 
 
 
 
 
 
167
 
168
  try:
169
  r = requests.post(
 
173
  "Content-Type": "application/json"
174
  },
175
  json={
176
+ "model": "llama3‑70b‑8192",
177
+ "messages": [
178
+ {"role": "system", "content": system_prompt},
179
+ {"role": "user", "content": topic}
180
+ ],
181
+ # Provide enough tokens for a JSON list
182
+ "max_tokens": 2048,
183
+ "temperature": 0.7
184
  },
185
  timeout=30
186
  )
187
+ except Exception as e:
188
+ st.error(f"Groq API request failed: {e}")
189
+ return []
190
 
191
+ # Check HTTP status
192
+ if r.status_code != 200:
193
+ st.error(f"Groq API error {r.status_code}: {r.text}")
194
+ return []
195
+
196
+ data = r.json()
197
 
198
+ # API returns text in: data["choices"][0]["message"]["content"]
199
+ text = ""
200
+ try:
201
+ text = data["choices"][0]["message"]["content"]
202
  except Exception as e:
203
+ st.error(f"Unexpected Groq response format: {e}")
204
+ st.write(data)
205
  return []
206
 
207
+ # Debug: show raw response
208
+ st.write("Raw Groq response:", text)
209
+
210
+ # Parse JSON from text
211
+ try:
212
+ parsed = json.loads(text)
213
+ except json.JSONDecodeError as e:
214
+ st.error(f"Failed to parse JSON from Groq output: {e}")
215
+ return []
216
+
217
+ # Convert to tuple list
218
+ out = []
219
+ for q in parsed:
220
+ if "question" in q and "options" in q and "answer" in q:
221
+ out.append((q["question"], q["options"], q["answer"]))
222
+ return out
223
+
224
+
225
  def load_json(path, default):
226
  if os.path.exists(path):
227
  try: