Files changed (1) hide show
  1. app.py +24 -10
app.py CHANGED
@@ -11,6 +11,7 @@ import requests
11
  import csv
12
  import uuid
13
  import os
 
14
  from PyPDF2 import PdfReader
15
  import docx
16
  import pytesseract
@@ -163,7 +164,10 @@ questions_db = {
163
  ("Which sport uses the term 'love'?", ["Tennis","Badminton","Squash","Golf"], "Tennis")
164
  ]
165
  }
166
-
 
 
 
167
  # ----------------- JSON helpers ---------------
168
 
169
  client = Groq(api_key=st.secrets["GROQ_API_KEY"].strip())
@@ -298,27 +302,31 @@ def parse_questions(raw_text, topic):
298
  questions = []
299
 
300
  try:
301
- blocks = re.split(r'\n\d+[\).]', raw_text)
302
 
303
  for b in blocks:
304
  lines = [l.strip() for l in b.split("\n") if l.strip()]
305
 
306
  if len(lines) >= 5:
307
  q = lines[0]
308
- options = lines[1:5]
309
- answer = options[0] # default
310
 
311
- # detect answer line
 
 
 
 
 
 
312
  for l in lines:
313
  if "answer" in l.lower():
314
- ans_text = l.split(":")[-1].strip()
315
  for opt in options:
316
- if ans_text.lower() in opt.lower():
317
  answer = opt
318
 
319
  questions.append({
320
  "question": q,
321
- "options": options,
322
  "answer": answer
323
  })
324
 
@@ -328,6 +336,10 @@ def parse_questions(raw_text, topic):
328
  print("Parse error:", e)
329
  return []
330
 
 
 
 
 
331
  def save_json(path, data):
332
  with open(path, "w", encoding="utf-8") as f:
333
  json.dump(data, f, indent=2, ensure_ascii=False)
@@ -1001,11 +1013,13 @@ def create_game_page():
1001
  mode = st.session_state.get("mode_selection", "Offline")
1002
 
1003
  # 🔥 MERGE DEFAULT + CUSTOM TOPICS
1004
- all_topics = list(questions_db.keys())
 
1005
 
1006
  topics = st.multiselect(
1007
  "Select Topics",
1008
- all_topics,
 
1009
  key="topic_selector"
1010
  )
1011
 
 
11
  import csv
12
  import uuid
13
  import os
14
+ import fitz # PyMuPDF
15
  from PyPDF2 import PdfReader
16
  import docx
17
  import pytesseract
 
164
  ("Which sport uses the term 'love'?", ["Tennis","Badminton","Squash","Golf"], "Tennis")
165
  ]
166
  }
167
+ # 🔥 MERGE CUSTOM TOPICS INTO MAIN DB (FIX)
168
+ if "custom_topics" in st.session_state:
169
+ for t, qs in st.session_state["custom_topics"].items():
170
+ questions_db[t] = qs
171
  # ----------------- JSON helpers ---------------
172
 
173
  client = Groq(api_key=st.secrets["GROQ_API_KEY"].strip())
 
302
  questions = []
303
 
304
  try:
305
+ blocks = re.split(r'\n\d+[\).]|\nQ\d+[\).]?', raw_text)
306
 
307
  for b in blocks:
308
  lines = [l.strip() for l in b.split("\n") if l.strip()]
309
 
310
  if len(lines) >= 5:
311
  q = lines[0]
 
 
312
 
313
+ options = []
314
+ for l in lines[1:]:
315
+ if len(options) < 4:
316
+ options.append(l)
317
+
318
+ answer = options[0]
319
+
320
  for l in lines:
321
  if "answer" in l.lower():
322
+ ans = l.split(":")[-1].strip().lower()
323
  for opt in options:
324
+ if ans in opt.lower():
325
  answer = opt
326
 
327
  questions.append({
328
  "question": q,
329
+ "options": options[:4],
330
  "answer": answer
331
  })
332
 
 
336
  print("Parse error:", e)
337
  return []
338
 
339
+ except Exception as e:
340
+ print("Parse error:", e)
341
+ return []
342
+
343
  def save_json(path, data):
344
  with open(path, "w", encoding="utf-8") as f:
345
  json.dump(data, f, indent=2, ensure_ascii=False)
 
1013
  mode = st.session_state.get("mode_selection", "Offline")
1014
 
1015
  # 🔥 MERGE DEFAULT + CUSTOM TOPICS
1016
+ # Ensure latest topics always loaded
1017
+ all_topics = sorted(list(questions_db.keys()))
1018
 
1019
  topics = st.multiselect(
1020
  "Select Topics",
1021
+ options=all_topics,
1022
+ default=[],
1023
  key="topic_selector"
1024
  )
1025