|
import pandas as pd |
|
import numpy as np |
|
import gradio as gr |
|
import torch |
|
from transformers import AutoModelForMultipleChoice, AutoTokenizer |
|
from huggingface_hub import hf_hub_url, Repository |
|
model_id="/kaggle/input/deberta-v3-large-hf-weights" |
|
|
|
|
|
model = AutoModelForMultipleChoice.from_pretrained(model_id) |
|
tokenizer = AutoTokenizer.from_pretrained(model_id) |
|
|
|
|
|
def preprocess(sample): |
|
first_sentences = [sample["prompt"]] * 5 |
|
second_sentences = [sample[option] for option in "ABCDE"] |
|
tokenized_sentences = tokenizer(first_sentences, second_sentences, truncation=True, padding=True, return_tensors="pt") |
|
sample["input_ids"] = tokenized_sentences["input_ids"] |
|
sample["attention_mask"] = tokenized_sentences["attention_mask"] |
|
return sample |
|
|
|
|
|
def predict(data): |
|
inputs = torch.stack(data["input_ids"]) |
|
masks = torch.stack(data["attention_mask"]) |
|
with torch.no_grad(): |
|
logits = model(inputs, attention_mask=masks).logits |
|
predictions_as_ids = torch.argsort(-logits, dim=1) |
|
answers = np.array(list("ABCDE"))[predictions_as_ids.tolist()] |
|
return ["".join(i) for i in answers[:, :3]] |
|
|
|
|
|
iface = gr.Interface( |
|
fn=predict, |
|
inputs=gr.Interface.DataType.json, |
|
outputs=gr.outputs.Label(num_top_classes=3), |
|
live=True, |
|
examples=[ |
|
{"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"} |
|
], |
|
title="LLM Science Exam Demo", |
|
description="Enter the prompt and options (A to E) below and get predictions.", |
|
) |
|
|
|
|
|
iface.launch() |
|
import pandas as pd |
|
import numpy as np |
|
import gradio as gr |
|
import torch |
|
from transformers import AutoModelForMultipleChoice, AutoTokenizer |
|
from huggingface_hub import hf_hub_url, Repository |
|
|
|
|
|
model_path = "my_model" |
|
model = AutoModelForMultipleChoice.from_pretrained(model_path) |
|
tokenizer = AutoTokenizer.from_pretrained(model_path) |
|
|
|
|
|
def preprocess(sample): |
|
first_sentences = [sample["prompt"]] * 5 |
|
second_sentences = [sample[option] for option in "ABCDE"] |
|
tokenized_sentences = tokenizer(first_sentences, second_sentences, truncation=True, padding=True, return_tensors="pt") |
|
sample["input_ids"] = tokenized_sentences["input_ids"] |
|
sample["attention_mask"] = tokenized_sentences["attention_mask"] |
|
return sample |
|
|
|
|
|
def predict(data): |
|
inputs = torch.stack(data["input_ids"]) |
|
masks = torch.stack(data["attention_mask"]) |
|
with torch.no_grad(): |
|
logits = model(inputs, attention_mask=masks).logits |
|
predictions_as_ids = torch.argsort(-logits, dim=1) |
|
answers = np.array(list("ABCDE"))[predictions_as_ids.tolist()] |
|
return ["".join(i) for i in answers[:, :3]] |
|
|
|
|
|
iface = gr.Interface( |
|
fn=predict, |
|
inputs=gr.Interface.DataType.json, |
|
outputs=gr.outputs.Label(num_top_classes=3), |
|
live=True, |
|
examples=[ |
|
{"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"} |
|
], |
|
title="LLM Science Exam Demo", |
|
description="Enter the prompt and options (A to E) below and get predictions.", |
|
) |
|
|
|
|
|
iface.launch(share=True) |
|
|