yaffo commited on
Commit
59fa052
·
verified ·
1 Parent(s): fd716b7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
+ from unsloth import FastLanguageModel
5
+ import numpy as np
6
+
7
+ # Load fine-tuned model
8
+ model_name = "sue888888888888/essay_grader"
9
+ model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype=torch.float16)
10
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
11
+ FastLanguageModel.for_inference(model)
12
+
13
+ # Prompt template
14
+ 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.
15
+
16
+ ### Instruction:
17
+ {instruction}
18
+
19
+ ### Input:
20
+ {input_text}
21
+
22
+ ### Response:
23
+ """
24
+
25
+ def grade_essay(question, reference, student, mark1, mark2, mark3, mark4):
26
+ mark_scheme = {
27
+ "1": mark1,
28
+ "2": mark2,
29
+ "3": mark3,
30
+ "4": mark4
31
+ }
32
+
33
+ instruction = "Grade this essay based on the following mark scheme:\n" + "\n".join([f"Criterion {k}: {v}" for k, v in mark_scheme.items()])
34
+ input_text = f"Question: {question}\nReference Answer: {reference}\nStudent Answer: {student}"
35
+ full_prompt = prompt_template.format(instruction=instruction, input_text=input_text)
36
+
37
+ inputs = tokenizer([full_prompt], return_tensors="pt").to("cuda")
38
+
39
+ with torch.no_grad():
40
+ outputs = model.generate(
41
+ **inputs,
42
+ max_new_tokens=50,
43
+ temperature=0.3,
44
+ do_sample=True,
45
+ top_p=0.9,
46
+ pad_token_id=tokenizer.eos_token_id
47
+ )
48
+
49
+ decoded = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
50
+ response = decoded.split("### Response:")[-1].strip()
51
+
52
+ return response
53
+
54
+ # UI
55
+ demo = gr.Interface(
56
+ fn=grade_essay,
57
+ inputs=[
58
+ gr.Textbox(label="Question"),
59
+ gr.Textbox(label="Reference Answer"),
60
+ gr.Textbox(label="Student Answer"),
61
+ gr.Textbox(label="Marking Criterion 1"),
62
+ gr.Textbox(label="Marking Criterion 2"),
63
+ gr.Textbox(label="Marking Criterion 3"),
64
+ gr.Textbox(label="Marking Criterion 4"),
65
+ ],
66
+ outputs=gr.Textbox(label="Model Response (Score or Explanation)"),
67
+ title="📝 Essay Grader (Mistral + Unsloth)"
68
+ )
69
+
70
+ demo.launch()