Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,47 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
salutation = "Good morning" if is_morning else "Good evening"
|
| 5 |
-
greeting = f"{salutation} {name}. It is {temperature} degrees today"
|
| 6 |
-
celsius = (temperature - 32) * 5 / 9
|
| 7 |
-
return greeting, round(celsius, 2)
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
)
|
| 14 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
import gradio as gr
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import AutoModelForMultipleChoice, AutoTokenizer
|
| 6 |
|
| 7 |
+
model_id = "deepset/deberta-v3-large-squad2"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Load the model and tokenizer
|
| 10 |
+
model = AutoModelForMultipleChoice.from_pretrained(model_id)
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 12 |
+
|
| 13 |
+
# Define the preprocessing function
|
| 14 |
+
def preprocess(sample):
|
| 15 |
+
first_sentences = [sample["prompt"]] * 5
|
| 16 |
+
second_sentences = [sample[option] for option in "ABCDE"]
|
| 17 |
+
tokenized_sentences = tokenizer(first_sentences, second_sentences, truncation=True, padding=True, return_tensors="pt")
|
| 18 |
+
sample["input_ids"] = tokenized_sentences["input_ids"]
|
| 19 |
+
sample["attention_mask"] = tokenized_sentences["attention_mask"]
|
| 20 |
+
return sample
|
| 21 |
+
|
| 22 |
+
# Define the prediction function
|
| 23 |
+
def predict(data):
|
| 24 |
+
inputs = torch.stack(data["input_ids"])
|
| 25 |
+
masks = torch.stack(data["attention_mask"])
|
| 26 |
+
with torch.no_grad():
|
| 27 |
+
logits = model(inputs, attention_mask=masks).logits
|
| 28 |
+
predictions_as_ids = torch.argsort(-logits, dim=1)
|
| 29 |
+
answers = np.array(list("ABCDE"))[predictions_as_ids.tolist()]
|
| 30 |
+
return ["".join(i) for i in answers[:, :3]]
|
| 31 |
+
text=gr.Textbox(placeholder="paste multiple choice questions.....")
|
| 32 |
+
label=gr.Label(num_top_classes=3)
|
| 33 |
+
# Create the Gradio interface
|
| 34 |
+
iface = gr.Interface(
|
| 35 |
+
fn=predict,
|
| 36 |
+
inputs=text # Use the correct class with type="json"
|
| 37 |
+
outputs=label,
|
| 38 |
+
live=True,
|
| 39 |
+
examples=[
|
| 40 |
+
{"prompt": "This is the prompt", "A": "Option A text", "B": "Option B text", "C": "Option C text", "D": "Option D text", "E": "Option E text"}
|
| 41 |
+
],
|
| 42 |
+
title="LLM Science Exam Demo",
|
| 43 |
+
description="Enter the prompt and options (A to E) below and get predictions.",
|
| 44 |
)
|
| 45 |
+
|
| 46 |
+
# Run the interface
|
| 47 |
+
iface.launch()
|