| """Exam topic prediction.""" |
| from __future__ import annotations |
|
|
| from typing import Tuple, List, Dict, Any |
|
|
| from src.config import EXAM_SYSTEM_PROMPT |
| from src.model_loader import get_model_and_tokenizer |
| from src.generation import chat_generate |
|
|
|
|
| def predict_exam_topics(text: str) -> Tuple[List[Dict[str, Any]], str]: |
| """Predict likely exam questions from Japanese text.""" |
| if not text or not text.strip(): |
| return [], "" |
|
|
| model, tokenizer = get_model_and_tokenizer() |
| messages = [ |
| {"role": "system", "content": EXAM_SYSTEM_PROMPT}, |
| {"role": "user", "content": text.strip()}, |
| ] |
| raw = chat_generate(model, tokenizer, messages, max_new_tokens=1024, do_sample=False) |
| items = [] |
| blocks = [b.strip() for b in raw.strip().split("\n\n") if b.strip()] |
| for block in blocks[:10]: |
| topic = "" |
| question = "" |
| sample_answer = "" |
| for line in block.splitlines(): |
| line = line.strip() |
| if line.lower().startswith("topic:"): |
| topic = line.split(":", 1)[1].strip() |
| elif line.lower().startswith("question:"): |
| question = line.split(":", 1)[1].strip() |
| elif line.lower().startswith("answer:"): |
| sample_answer = line.split(":", 1)[1].strip() |
| if topic or question: |
| items.append({"topic": topic, "question": question, "sample_answer": sample_answer}) |
| return items, "" |
|
|