Spaces:
Running
Running
Update app.py (#65)
Browse files- Update app.py (2f5736ee56cec38610ac47204e400171c898d03b)
app.py
CHANGED
|
@@ -147,28 +147,23 @@ questions_db = {
|
|
| 147 |
|
| 148 |
def generate_ai_questions(topic, n=5):
|
| 149 |
"""
|
| 150 |
-
|
| 151 |
-
|
| 152 |
"""
|
|
|
|
| 153 |
if not GROQ_API_KEY:
|
|
|
|
| 154 |
return []
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 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":
|
| 182 |
-
"messages": [
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
},
|
| 185 |
timeout=30
|
| 186 |
)
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
except Exception as e:
|
| 199 |
-
|
|
|
|
| 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:
|