Spaces:
Running
Running
Chandima Prabhath
commited on
Commit
·
8ac1fbd
1
Parent(s):
ff069bf
Improve trivia question generation with clearer JSON format and enhanced error handling; log errors for better debugging.
Browse files
app.py
CHANGED
@@ -276,19 +276,26 @@ async def whatsapp_webhook(request: Request):
|
|
276 |
send_message(mid, chat, f"✨ {quote}")
|
277 |
return {"success": True}
|
278 |
|
|
|
279 |
if low == "/trivia":
|
280 |
raw = generate_llm(
|
281 |
-
"Generate a trivia question and answer in JSON: "
|
282 |
-
"{\"question\"
|
283 |
)
|
284 |
try:
|
285 |
obj = json.loads(raw)
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
|
|
|
|
|
|
|
|
|
|
290 |
return {"success": True}
|
291 |
|
|
|
292 |
if low == "/answer":
|
293 |
if chat in trivia_store:
|
294 |
ans = trivia_store.pop(chat)["answer"]
|
|
|
276 |
send_message(mid, chat, f"✨ {quote}")
|
277 |
return {"success": True}
|
278 |
|
279 |
+
# TRIVIA
|
280 |
if low == "/trivia":
|
281 |
raw = generate_llm(
|
282 |
+
"Generate a trivia question and answer in JSON format like this: "
|
283 |
+
"{\"question\": \"What is the capital of France?\", \"answer\": \"Paris\"}"
|
284 |
)
|
285 |
try:
|
286 |
obj = json.loads(raw)
|
287 |
+
if "question" in obj and "answer" in obj:
|
288 |
+
trivia_store[chat] = obj
|
289 |
+
send_message(mid, chat, f"❓ {obj['question']}\nReply with `/answer` to see the answer.")
|
290 |
+
else:
|
291 |
+
raise ValueError("Missing expected keys.")
|
292 |
+
except Exception as e:
|
293 |
+
# Fallback: notify the user and log the raw response
|
294 |
+
logging.error("Trivia JSON parse error: %s, raw response: %s", e, raw)
|
295 |
+
send_message(mid, chat, "Failed to generate trivia. Please try again.")
|
296 |
return {"success": True}
|
297 |
|
298 |
+
|
299 |
if low == "/answer":
|
300 |
if chat in trivia_store:
|
301 |
ans = trivia_store.pop(chat)["answer"]
|