File size: 2,240 Bytes
59fa052
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
from unsloth import FastLanguageModel
import numpy as np

# Load fine-tuned model
model_name = "sue888888888888/essay_grader"
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype=torch.float16)
tokenizer = AutoTokenizer.from_pretrained(model_name)
FastLanguageModel.for_inference(model)

# Prompt template
prompt_template = """Below is an instruction that describes how to grade an essay, paired with an input that provides the grading schema. Write a response that grades essays based on the mark schema provided.

### Instruction:
{instruction}

### Input:
{input_text}

### Response:
"""

def grade_essay(question, reference, student, mark1, mark2, mark3, mark4):
    mark_scheme = {
        "1": mark1,
        "2": mark2,
        "3": mark3,
        "4": mark4
    }

    instruction = "Grade this essay based on the following mark scheme:\n" + "\n".join([f"Criterion {k}: {v}" for k, v in mark_scheme.items()])
    input_text = f"Question: {question}\nReference Answer: {reference}\nStudent Answer: {student}"
    full_prompt = prompt_template.format(instruction=instruction, input_text=input_text)

    inputs = tokenizer([full_prompt], return_tensors="pt").to("cuda")

    with torch.no_grad():
        outputs = model.generate(
            **inputs,
            max_new_tokens=50,
            temperature=0.3,
            do_sample=True,
            top_p=0.9,
            pad_token_id=tokenizer.eos_token_id
        )

    decoded = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
    response = decoded.split("### Response:")[-1].strip()

    return response

# UI
demo = gr.Interface(
    fn=grade_essay,
    inputs=[
        gr.Textbox(label="Question"),
        gr.Textbox(label="Reference Answer"),
        gr.Textbox(label="Student Answer"),
        gr.Textbox(label="Marking Criterion 1"),
        gr.Textbox(label="Marking Criterion 2"),
        gr.Textbox(label="Marking Criterion 3"),
        gr.Textbox(label="Marking Criterion 4"),
    ],
    outputs=gr.Textbox(label="Model Response (Score or Explanation)"),
    title="📝 Essay Grader (Mistral + Unsloth)"
)

demo.launch()