File size: 1,629 Bytes
366bd77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import numpy as np
import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer


labels = {
    0 : "Incorrect",
    1 : "Partialy correct/Incomplete",
    2 : "correct"
}

print('currently loading model')
model = AutoModelForSequenceClassification.from_pretrained("./model")
tokenizer = AutoTokenizer.from_pretrained("./tokenizer")
print('model loaded successfully')

def grade(model_answer, student_answer):
    inputs = tokenizer(model_answer, student_answer, padding="max_length", truncation=True, return_tensors="pt")

    with torch.no_grad():
        logits = model(**inputs).logits
    
    preds = torch.nn.functional.softmax(logits, dim=1)
    preds = np.concatenate(preds.numpy()).ravel().tolist()
    print(preds)
    return {l:p for p, l in zip(preds, labels.values())}

demo = gr.Interface(
    fn=grade, 
    inputs=[
        gr.Textbox(lines=2, placeholder="Model answer here"), 
        gr.Textbox(lines=2, placeholder="Student answer here")
    ], 
    outputs="label",
    title="Grading short answer questions",
    examples=[
        [
            "A prototype is used to simulate the behavior of portions of the desired software product", 
            "a prototype is used to simulate the behavior of a portion of the desired software product"
        ],
        [
            "A variable in programming is a location in memory that can be used  to store a value", 
            "no answer"
        ],
        [
            "A computer system consists of a CPU, Memory, Input, and output devices.", 
            "a CPU only"
        ],
    ],
)

demo.launch()