""" finetune/train_modal.py — QLoRA fine-tune of MiniCPM4-8B for PaperProf. Task: exam-question generation. The training data is SQuAD passages and questions reformatted into the EXACT production prompt used by core/questioner.py, so the model learns PaperProf's task, not generic QA. Run: modal run finetune/train_modal.py Output: Merged bf16 model pushed to hf.co/build-small-hackathon/MiniCPM4-8B-PaperProf """ import modal APP_NAME = "paperprof-finetune-41" BASE_MODEL = "openbmb/MiniCPM4.1-8B" HUB_REPO = "build-small-hackathon/MiniCPM4.1-8B-PaperProf" N_SAMPLES = 3000 MAX_LEN = 1024 app = modal.App(APP_NAME) image = ( modal.Image.debian_slim(python_version="3.12") .pip_install( "torch==2.6.0", "transformers==4.57.1", "datasets==3.2.0", "peft==0.14.0", "bitsandbytes==0.46.0", "accelerate>=1.8.0", "huggingface_hub", "sentencepiece", ) ) # Mirror of core/questioner.py _PROMPT_TEMPLATE (Normal difficulty) PROMPT_TEMPLATE = """\ You are a university professor creating exam questions. Given the following excerpt from a course, write ONE focused question. Difficulty — Ask for conceptual understanding (Explain X. Why does X happen?). Rules: - ONE question only, on ONE concept - Maximum 25 words - No sub-questions, no "and", no compound questions - IMPORTANT: Always write the question in English, even if the source text is in another language - Output only the question, nothing else Excerpt: {chunk} Question:""" # Mirror of core/questioner.py _MCQ_TEMPLATE MCQ_TEMPLATE = """\ You are a university professor creating a multiple choice exam question. Given the following excerpt, write ONE multiple choice question. Rules: - One clear question, maximum 25 words - Exactly 4 options (A, B, C, D), only ONE is correct - All wrong options must be plausible — no obviously wrong answers - IMPORTANT: Always write everything in English, even if the source text is in another language - For each option, write a 1-sentence explanation of why it is correct or incorrect Output format (use EXACTLY these labels, one per line, nothing else): QUESTION: A)