File size: 2,867 Bytes
76acfc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import torch
import gradio as gr
import spaces

from transformers import (
    AutoTokenizer,
    AutoModelForMultipleChoice
)


# -------------------------
# Model
# -------------------------

MODEL_PATH = "./best_model_fold_1"


# -------------------------
# Device
# -------------------------

device = torch.device(
    "cuda" if torch.cuda.is_available() else "cpu"
)

print("Device:", device)


# -------------------------
# Load tokenizer
# -------------------------

tokenizer = AutoTokenizer.from_pretrained(
    MODEL_PATH
)

print("Tokenizer loaded successfully!")


# -------------------------
# Load model
# -------------------------

model = AutoModelForMultipleChoice.from_pretrained(
    MODEL_PATH
)

model.to(device)
model.eval()

print("Model loaded successfully!")


# -------------------------
# Prediction
# -------------------------
@spaces.GPU(duration=30)
def predict(question, A, B, C, D, E):

    choices = [A, B, C, D, E]

    encoding = tokenizer(
        [question] * 5,
        choices,
        max_length=256,
        truncation=True,
        padding="max_length",
        return_tensors="pt"
    )

    input_ids = encoding["input_ids"].unsqueeze(0).to(device)

    attention_mask = encoding[
        "attention_mask"
    ].unsqueeze(0).to(device)

    with torch.no_grad():

        outputs = model(
            input_ids=input_ids,
            attention_mask=attention_mask
        )

    logits = outputs.logits

    probabilities = torch.softmax(
        logits,
        dim=1
    )[0]

    letters = ["A", "B", "C", "D", "E"]

    prediction = torch.argmax(
        probabilities
    ).item()

    answer = letters[prediction]

    result = (
        f"Predicted Answer: {answer}\n\n"
        "Scores\n\n"
    )

    for letter, score in zip(
        letters,
        probabilities
    ):
        result += (
            f"{letter}: "
            f"{score.item():.4f}\n"
        )

    return result


# -------------------------
# Gradio
# -------------------------

demo = gr.Interface(

    fn=predict,

    inputs=[

        gr.Textbox(
            lines=4,
            label="Question"
        ),

        gr.Textbox(
            label="Option A"
        ),

        gr.Textbox(
            label="Option B"
        ),

        gr.Textbox(
            label="Option C"
        ),

        gr.Textbox(
            label="Option D"
        ),

        gr.Textbox(
            label="Option E"
        ),
    ],

    outputs=gr.Textbox(
        label="Prediction"
    ),

    title="DeBERTa MCQ Solver",

    description=(
        "Enter a question and five options."
    ),

    examples=[
        [
            "Which planet is known as the Red Planet?",
            "Earth",
            "Mars",
            "Venus",
            "Jupiter",
            "Saturn"
        ]
    ]
)


if __name__ == "__main__":
    demo.launch()