Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -172,8 +172,9 @@ generator = load_generator()
|
|
| 172 |
|
| 173 |
def generate_ai_sentence(idiom, examples_map):
|
| 174 |
prompt = (
|
| 175 |
-
f"Write
|
| 176 |
-
f"Replace the idiom with a blank (_____).
|
|
|
|
| 177 |
)
|
| 178 |
|
| 179 |
try:
|
|
@@ -183,25 +184,34 @@ def generate_ai_sentence(idiom, examples_map):
|
|
| 183 |
do_sample=True,
|
| 184 |
temperature=0.9
|
| 185 |
)
|
| 186 |
-
text = result[0]['generated_text'].strip()
|
| 187 |
|
| 188 |
-
|
|
|
|
|
|
|
| 189 |
if prompt.lower() in text.lower():
|
| 190 |
text = text.replace(prompt, "").strip()
|
| 191 |
|
| 192 |
-
#
|
| 193 |
-
if "_____" in text and len(text) >
|
| 194 |
return text
|
| 195 |
|
| 196 |
except:
|
| 197 |
pass
|
| 198 |
|
| 199 |
-
#
|
| 200 |
examples = examples_map.get(idiom.lower(), [])
|
| 201 |
-
if examples:
|
| 202 |
-
sentence = random.choice(examples)["en"]
|
| 203 |
-
return sentence.replace(idiom, "_____")
|
| 204 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
|
| 207 |
def generate_distractors(correct_idiom, all_idioms):
|
|
@@ -212,16 +222,22 @@ def generate_distractors(correct_idiom, all_idioms):
|
|
| 212 |
return options
|
| 213 |
|
| 214 |
def generate_ai_question_dynamic(all_idioms, examples_map):
|
| 215 |
-
correct = random.choice(all_idioms)
|
| 216 |
|
| 217 |
-
|
| 218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
|
|
|
| 225 |
|
| 226 |
# DETECT IDIOMS
|
| 227 |
def detect_idioms(text, idioms):
|
|
|
|
| 172 |
|
| 173 |
def generate_ai_sentence(idiom, examples_map):
|
| 174 |
prompt = (
|
| 175 |
+
f"Write ONE natural, realistic English sentence using the idiom '{idiom}'. "
|
| 176 |
+
f"Replace the idiom with a blank (_____). "
|
| 177 |
+
f"Do NOT explain. Only output the sentence."
|
| 178 |
)
|
| 179 |
|
| 180 |
try:
|
|
|
|
| 184 |
do_sample=True,
|
| 185 |
temperature=0.9
|
| 186 |
)
|
|
|
|
| 187 |
|
| 188 |
+
text = result[0]["generated_text"].strip()
|
| 189 |
+
|
| 190 |
+
# Remove prompt if model echoes it
|
| 191 |
if prompt.lower() in text.lower():
|
| 192 |
text = text.replace(prompt, "").strip()
|
| 193 |
|
| 194 |
+
# accept only GOOD outputs
|
| 195 |
+
if "_____" in text and len(text) > 25:
|
| 196 |
return text
|
| 197 |
|
| 198 |
except:
|
| 199 |
pass
|
| 200 |
|
| 201 |
+
# FALLBACK
|
| 202 |
examples = examples_map.get(idiom.lower(), [])
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
+
valid_examples = []
|
| 205 |
+
for ex in examples:
|
| 206 |
+
sentence = ex.get("en", "")
|
| 207 |
+
if idiom.lower() in sentence.lower():
|
| 208 |
+
valid_examples.append(sentence)
|
| 209 |
+
|
| 210 |
+
if valid_examples:
|
| 211 |
+
chosen = random.choice(valid_examples)
|
| 212 |
+
return chosen.replace(idiom, "_____")
|
| 213 |
+
|
| 214 |
+
return None
|
| 215 |
|
| 216 |
|
| 217 |
def generate_distractors(correct_idiom, all_idioms):
|
|
|
|
| 222 |
return options
|
| 223 |
|
| 224 |
def generate_ai_question_dynamic(all_idioms, examples_map):
|
|
|
|
| 225 |
|
| 226 |
+
# Try multiple times to get a valid question
|
| 227 |
+
for _ in range(5):
|
| 228 |
+
|
| 229 |
+
correct = random.choice(all_idioms)
|
| 230 |
+
sentence = generate_ai_sentence(correct, examples_map)
|
| 231 |
+
|
| 232 |
+
if sentence: # ✅ only accept valid ones
|
| 233 |
+
options = generate_distractors(correct, all_idioms)
|
| 234 |
|
| 235 |
+
return {
|
| 236 |
+
"question": sentence,
|
| 237 |
+
"options": options,
|
| 238 |
+
"answer": correct
|
| 239 |
+
}
|
| 240 |
+
raise ValueError("Failed to generate valid quiz question.")
|
| 241 |
|
| 242 |
# DETECT IDIOMS
|
| 243 |
def detect_idioms(text, idioms):
|