File size: 2,206 Bytes
ded6bc1
 
 
 
49884e0
ded6bc1
49884e0
ded6bc1
 
 
 
 
 
49884e0
 
ded6bc1
 
 
 
 
 
 
 
 
 
 
 
4962a85
ded6bc1
 
 
49884e0
 
 
 
 
ded6bc1
 
 
 
 
 
 
 
 
 
 
 
 
49884e0
ded6bc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
import torch.nn.functional as F
from transformers import AutoTokenizer, AutoModelForMultipleChoice
import spaces  # <--- Added spaces import

# Load fine-tuned model and tokenizer from Hugging Face Hub
MODEL_ID = "udaypratap/smart-mcq-solver"

tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModelForMultipleChoice.from_pretrained(MODEL_ID)
model.eval()

# Add the @spaces.GPU decorator right here
@spaces.GPU
def predict_mcq(prompt, option_a, option_b, option_c, option_d, option_e):
    options = [option_a, option_b, option_c, option_d, option_e]
    labels = ["A", "B", "C", "D", "E"]
    
    first_sentences = [prompt] * 5
    second_sentences = options
    
    inputs = tokenizer(
        first_sentences,
        second_sentences,
        truncation=True,
        padding=True,
        max_length=128,
        return_tensors="pt"
    )
    
    # Move inputs and model to the GPU
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model.to(device)
    inputs = {k: v.to(device) for k, v in inputs.items()}
    
    input_ids = inputs["input_ids"].unsqueeze(0)
    attention_mask = inputs["attention_mask"].unsqueeze(0)
    
    with torch.no_grad():
        outputs = model(input_ids=input_ids, attention_mask=attention_mask)
        logits = outputs.logits.squeeze(0)
        probs = F.softmax(logits, dim=-1)
        
    top3_indices = torch.topk(probs, k=3).indices.tolist()
    top3_choices = [f"{labels[idx]} ({probs[idx].item():.2%})" for idx in top3_indices]
    
    return f"Top 3 Predicted Answers: {', '.join(top3_choices)}"

# Create Gradio Interface
demo = gr.Interface(
    fn=predict_mcq,
    inputs=[
        gr.Textbox(label="Question Prompt", placeholder="Enter your question here..."),
        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="Predictions"),
    title="Smart MCQ Solver",
    description="Enter a question prompt along with 5 options to get the top predicted answers."
)

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