pluem9 commited on
Commit
3d94eac
·
1 Parent(s): 9b23490

fixminilmTFIDF

Browse files
src/streamlit_app.py CHANGED
@@ -301,6 +301,7 @@ if generate_btn:
301
  n_questions=n_questions,
302
  level=level,
303
  seed=seed_value,
 
304
  )
305
 
306
  rows = mcq_to_baseline_rows(
@@ -319,6 +320,7 @@ if generate_btn:
319
  n_questions=n_questions,
320
  level=level,
321
  seed=seed_value,
 
322
  )
323
 
324
  rows = mcq_to_baseline_rows(
 
301
  n_questions=n_questions,
302
  level=level,
303
  seed=seed_value,
304
+ q_types=q_types,
305
  )
306
 
307
  rows = mcq_to_baseline_rows(
 
320
  n_questions=n_questions,
321
  level=level,
322
  seed=seed_value,
323
+ q_types=q_types,
324
  )
325
 
326
  rows = mcq_to_baseline_rows(
src/utils/__pycache__/utils_quiz_embedding.cpython-313.pyc CHANGED
Binary files a/src/utils/__pycache__/utils_quiz_embedding.cpython-313.pyc and b/src/utils/__pycache__/utils_quiz_embedding.cpython-313.pyc differ
 
src/utils/__pycache__/utils_quiz_quality.cpython-313.pyc CHANGED
Binary files a/src/utils/__pycache__/utils_quiz_quality.cpython-313.pyc and b/src/utils/__pycache__/utils_quiz_quality.cpython-313.pyc differ
 
src/utils/utils_quiz_embedding.py CHANGED
@@ -132,10 +132,117 @@ def gen_question(slide: str, emb: EmbIndex, level="medium") -> Dict:
132
  stem = random.choice(stem_templates)
133
  return {"question": stem, "options": options, "answer": ans, "meta": {"level": level}}
134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  # ---------------------------
136
  # Public API
137
  # ---------------------------
138
- def generate_quiz(slides: List[str], n_questions=10, level="medium", seed=42) -> Dict:
 
 
 
 
 
 
139
  random.seed(seed)
140
  np.random.seed(seed)
141
  slides = [s for s in slides if s.strip()]
@@ -150,11 +257,14 @@ def generate_quiz(slides: List[str], n_questions=10, level="medium", seed=42) ->
150
  all_q.append(q)
151
  if len(all_q) > n_questions:
152
  all_q = all_q[:n_questions]
 
 
 
153
  return {
154
- "questions": all_q,
155
  "qc_report": {
156
  "slides": len(slides),
157
- "generated": len(all_q),
158
  "level": clamp(level),
159
  "seed": seed
160
  }
 
132
  stem = random.choice(stem_templates)
133
  return {"question": stem, "options": options, "answer": ans, "meta": {"level": level}}
134
 
135
+ # ---------------------------
136
+ # Apply question types (MCQ / CLOZE / T/F)
137
+ # ---------------------------
138
+ def _apply_qtypes(
139
+ qq: List[Dict],
140
+ q_types: Optional[List[str]],
141
+ seed: int,
142
+ ) -> List[Dict]:
143
+ if not qq:
144
+ return qq
145
+
146
+ if not q_types:
147
+ q_types = ["MCQ"]
148
+
149
+ norm_types = [t.upper() for t in q_types if t]
150
+ if not norm_types:
151
+ norm_types = ["MCQ"]
152
+
153
+ rng = random.Random(seed)
154
+ out: List[Dict] = []
155
+
156
+ for i, q in enumerate(qq):
157
+ stem = str(q.get("question", ""))
158
+ ans = str(q.get("answer", ""))
159
+ opts = [
160
+ str(o)
161
+ for o in q.get("options", [])
162
+ if isinstance(o, (str, int, float)) and str(o).strip()
163
+ ]
164
+
165
+ if ans and all(norm(ans) != norm(o) for o in opts):
166
+ opts.insert(0, ans)
167
+ if not opts and ans:
168
+ opts = [ans]
169
+
170
+ while len(opts) < 4:
171
+ opts.append(f"option-{len(opts)+1}")
172
+ opts = dedup_keep_order(opts)[:4]
173
+
174
+ target_type = norm_types[i % len(norm_types)]
175
+
176
+ # CLOZE
177
+ if target_type == "CLOZE":
178
+ if ans:
179
+ pattern = re.compile(re.escape(ans), re.IGNORECASE)
180
+ new_stem, nsub = pattern.subn("____", stem, count=1)
181
+ if nsub > 0:
182
+ stem_cloze = new_stem
183
+ else:
184
+ stem_cloze = f"____ {stem}"
185
+ else:
186
+ stem_cloze = stem
187
+
188
+ out.append({
189
+ "type": "CLOZE",
190
+ "question": stem_cloze,
191
+ "options": opts,
192
+ "answer": ans,
193
+ "explanation": "",
194
+ "meta": q.get("meta", {}),
195
+ })
196
+
197
+ # T/F
198
+ elif target_type in ("T/F", "TF", "TRUE/FALSE"):
199
+ wrong = next(
200
+ (o for o in opts if ans and norm(o) != norm(ans)),
201
+ None,
202
+ )
203
+ make_true = (i % 2 == 0 or not wrong)
204
+
205
+ if make_true:
206
+ statement = f"\"{ans}\" ปรากฏเป็นคำสำคัญในสไลด์นี้"
207
+ tf_ans = "True"
208
+ expl = ""
209
+ else:
210
+ statement = f"\"{wrong}\" ปรากฏเป็นคำสำคัญในสไลด์นี้"
211
+ tf_ans = "False"
212
+ expl = f"คำตอบที่ถูกคือ {ans}"
213
+
214
+ out.append({
215
+ "type": "T/F",
216
+ "question": statement,
217
+ "options": ["True", "False"],
218
+ "answer": tf_ans,
219
+ "explanation": expl,
220
+ "meta": q.get("meta", {}),
221
+ })
222
+
223
+ # MCQ
224
+ else:
225
+ out.append({
226
+ "type": "MCQ",
227
+ "question": stem,
228
+ "options": opts,
229
+ "answer": ans,
230
+ "explanation": "",
231
+ "meta": q.get("meta", {}),
232
+ })
233
+
234
+ return out
235
+
236
  # ---------------------------
237
  # Public API
238
  # ---------------------------
239
+ def generate_quiz(
240
+ slides: List[str],
241
+ n_questions=10,
242
+ level="medium",
243
+ seed=42,
244
+ q_types: Optional[List[str]] = None,
245
+ ) -> Dict:
246
  random.seed(seed)
247
  np.random.seed(seed)
248
  slides = [s for s in slides if s.strip()]
 
257
  all_q.append(q)
258
  if len(all_q) > n_questions:
259
  all_q = all_q[:n_questions]
260
+
261
+ typed_questions = _apply_qtypes(all_q, q_types, seed)
262
+
263
  return {
264
+ "questions": typed_questions,
265
  "qc_report": {
266
  "slides": len(slides),
267
+ "generated": len(typed_questions),
268
  "level": clamp(level),
269
  "seed": seed
270
  }
src/utils/utils_quiz_quality.py CHANGED
@@ -339,11 +339,122 @@ def distribute(buckets: List[List[Dict]], total: int) -> List[Dict]:
339
  bs.pop(i % len(bs) if bs else 0)
340
  return out
341
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
342
 
343
  # ---------------------------
344
  # Public API (force-complete)
345
  # ---------------------------
346
- def generate_quiz(slides: List[str], n_questions=12, level="medium", seed=42) -> Dict:
 
 
 
 
 
 
347
  set_all_seeds(seed)
348
  slides = [s for s in slides if s and s.strip()]
349
  if not slides:
@@ -397,15 +508,19 @@ def generate_quiz(slides: List[str], n_questions=12, level="medium", seed=42) ->
397
 
398
  qc_set(final)
399
 
 
 
 
400
  total_raw = sum(len(x) for x in draft_by_slide)
401
  return {
402
- "questions": final[:n_questions],
403
  "qc_report": {
404
  "slides": len(slides),
405
  "draft_generated": total_raw,
406
- "selected": len(final[:n_questions]),
407
  "level": clamp(level),
408
  "seed": seed,
409
  "forced_fill_used": len(final) < n_questions
410
  }
411
  }
 
 
339
  bs.pop(i % len(bs) if bs else 0)
340
  return out
341
 
342
+ # ---------------------------
343
+ # Apply question types (MCQ / CLOZE / T/F)
344
+ # ---------------------------
345
+ def _apply_qtypes(
346
+ qq: List[Dict],
347
+ q_types: Optional[List[str]],
348
+ seed: int,
349
+ ) -> List[Dict]:
350
+ if not qq:
351
+ return qq
352
+
353
+ if not q_types:
354
+ q_types = ["MCQ"]
355
+
356
+ # normalize
357
+ norm_types = [t.upper() for t in q_types if t]
358
+ if not norm_types:
359
+ norm_types = ["MCQ"]
360
+
361
+ rng = random.Random(seed)
362
+ out: List[Dict] = []
363
+
364
+ for i, q in enumerate(qq):
365
+ stem = str(q.get("question", ""))
366
+ ans = str(q.get("answer", ""))
367
+ opts = [
368
+ str(o)
369
+ for o in q.get("options", [])
370
+ if isinstance(o, (str, int, float)) and str(o).strip()
371
+ ]
372
+
373
+ # ensure answer in options
374
+ if ans and all(norm(ans) != norm(o) for o in opts):
375
+ opts.insert(0, ans)
376
+ if not opts and ans:
377
+ opts = [ans]
378
+
379
+ while len(opts) < 4:
380
+ opts.append(f"option-{len(opts)+1}")
381
+ opts = dedup_keep_order(opts)[:4]
382
+
383
+ # เลือก type
384
+ target_type = norm_types[i % len(norm_types)]
385
+
386
+ # ---- CLOZE ----
387
+ if target_type == "CLOZE":
388
+ if ans:
389
+ pattern = re.compile(re.escape(ans), re.IGNORECASE)
390
+ new_stem, nsub = pattern.subn("____", stem, count=1)
391
+ if nsub > 0:
392
+ stem_cloze = new_stem
393
+ else:
394
+ stem_cloze = f"____ {stem}"
395
+ else:
396
+ stem_cloze = stem
397
+
398
+ out.append({
399
+ "type": "CLOZE",
400
+ "question": stem_cloze,
401
+ "options": opts,
402
+ "answer": ans,
403
+ "explanation": "",
404
+ "meta": q.get("meta", {}),
405
+ })
406
+
407
+ # ---- T/F ----
408
+ elif target_type in ("T/F", "TF", "TRUE/FALSE"):
409
+ # ทำ statement จริง/เท็จจาก answer / distractor
410
+ wrong = next(
411
+ (o for o in opts if ans and norm(o) != norm(ans)),
412
+ None,
413
+ )
414
+ make_true = (i % 2 == 0 or not wrong)
415
+
416
+ if make_true:
417
+ statement = f"\"{ans}\" ปรากฏเป็นคำสำคัญในสไลด์นี้"
418
+ tf_ans = "True"
419
+ expl = ""
420
+ else:
421
+ statement = f"\"{wrong}\" ปรากฏเป็นคำสำคัญในสไลด์นี้"
422
+ tf_ans = "False"
423
+ expl = f"คำตอบที่ถูกคือ {ans}"
424
+
425
+ out.append({
426
+ "type": "T/F",
427
+ "question": statement,
428
+ "options": ["True", "False"],
429
+ "answer": tf_ans,
430
+ "explanation": expl,
431
+ "meta": q.get("meta", {}),
432
+ })
433
+
434
+ # ---- MCQ ----
435
+ else:
436
+ out.append({
437
+ "type": "MCQ",
438
+ "question": stem,
439
+ "options": opts,
440
+ "answer": ans,
441
+ "explanation": "",
442
+ "meta": q.get("meta", {}),
443
+ })
444
+
445
+ return out
446
+
447
 
448
  # ---------------------------
449
  # Public API (force-complete)
450
  # ---------------------------
451
+ def generate_quiz(
452
+ slides: List[str],
453
+ n_questions=12,
454
+ level="medium",
455
+ seed=42,
456
+ q_types: Optional[List[str]] = None,
457
+ ) -> Dict:
458
  set_all_seeds(seed)
459
  slides = [s for s in slides if s and s.strip()]
460
  if not slides:
 
508
 
509
  qc_set(final)
510
 
511
+ # apply question types (MCQ / CLOZE / T/F)
512
+ typed_questions = _apply_qtypes(final[:n_questions], q_types, seed)
513
+
514
  total_raw = sum(len(x) for x in draft_by_slide)
515
  return {
516
+ "questions": typed_questions,
517
  "qc_report": {
518
  "slides": len(slides),
519
  "draft_generated": total_raw,
520
+ "selected": len(typed_questions),
521
  "level": clamp(level),
522
  "seed": seed,
523
  "forced_fill_used": len(final) < n_questions
524
  }
525
  }
526
+