Spaces:
Runtime error
Runtime error
File size: 1,030 Bytes
009dbb2 bfa9948 009dbb2 bfa9948 009dbb2 bfa9948 009dbb2 |
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 |
import gradio as gr
import torch
from transformers import AutoTokenizer
from transformers import AutoModelForSequenceClassification
# Specify the path of the model
model_ckpt = Path("./distilbert-base-uncased-finetuned-emotion")
# Load the fine-tuned tokenizer and model
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
tokenizer = AutoTokenizer.from_pretrained(model_ckpt)
model = AutoModelForSequenceClassification.from_pretrained(model_ckpt).to(device)
class_names = ['sadness', 'joy', 'love', 'anger', 'fear', 'surprise']
def inference(text: str) -> str:
inputs = tokenizer(text, return_tensors="pt")
inputs = {k:v.to(device) for k,v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1).tolist()[0]
max_vale = max(predictions)
idx = predictions.index(max_vale)
return model.config.id2label[idx]
iface = gr.Interface(fn=inference, inputs="text", outputs="text")
iface.launch()
|