File size: 1,447 Bytes
00a5799
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""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, ""