Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -223,13 +223,13 @@ def generate_distractors(correct_idiom, all_idioms):
|
|
| 223 |
|
| 224 |
def generate_ai_question_dynamic(all_idioms, examples_map):
|
| 225 |
|
| 226 |
-
# Try multiple times
|
| 227 |
for _ in range(5):
|
| 228 |
|
| 229 |
correct = random.choice(all_idioms)
|
| 230 |
sentence = generate_ai_sentence(correct, examples_map)
|
| 231 |
|
| 232 |
-
if sentence:
|
| 233 |
options = generate_distractors(correct, all_idioms)
|
| 234 |
|
| 235 |
return {
|
|
@@ -237,7 +237,34 @@ def generate_ai_question_dynamic(all_idioms, examples_map):
|
|
| 237 |
"options": options,
|
| 238 |
"answer": correct
|
| 239 |
}
|
| 240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
|
| 242 |
# DETECT IDIOMS
|
| 243 |
def detect_idioms(text, idioms):
|
|
|
|
| 223 |
|
| 224 |
def generate_ai_question_dynamic(all_idioms, examples_map):
|
| 225 |
|
| 226 |
+
# ---------- 1. Try AI multiple times ----------
|
| 227 |
for _ in range(5):
|
| 228 |
|
| 229 |
correct = random.choice(all_idioms)
|
| 230 |
sentence = generate_ai_sentence(correct, examples_map)
|
| 231 |
|
| 232 |
+
if sentence:
|
| 233 |
options = generate_distractors(correct, all_idioms)
|
| 234 |
|
| 235 |
return {
|
|
|
|
| 237 |
"options": options,
|
| 238 |
"answer": correct
|
| 239 |
}
|
| 240 |
+
|
| 241 |
+
# ---------- 2. FORCE DATASET FALLBACK ----------
|
| 242 |
+
for idiom in all_idioms:
|
| 243 |
+
examples = examples_map.get(idiom.lower(), [])
|
| 244 |
+
|
| 245 |
+
valid_examples = [
|
| 246 |
+
ex["en"] for ex in examples
|
| 247 |
+
if idiom.lower() in ex.get("en", "").lower()
|
| 248 |
+
]
|
| 249 |
+
|
| 250 |
+
if valid_examples:
|
| 251 |
+
sentence = random.choice(valid_examples)
|
| 252 |
+
sentence = sentence.replace(idiom, "_____")
|
| 253 |
+
|
| 254 |
+
options = generate_distractors(idiom, all_idioms)
|
| 255 |
+
|
| 256 |
+
return {
|
| 257 |
+
"question": sentence,
|
| 258 |
+
"options": options,
|
| 259 |
+
"answer": idiom
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
# ---------- 3. LAST RESORT (never crash UI) ----------
|
| 263 |
+
return {
|
| 264 |
+
"question": "No question available right now. Try again.",
|
| 265 |
+
"options": all_idioms[:4],
|
| 266 |
+
"answer": all_idioms[0]
|
| 267 |
+
}
|
| 268 |
|
| 269 |
# DETECT IDIOMS
|
| 270 |
def detect_idioms(text, idioms):
|