File size: 664 Bytes
167d3d4
36ea7dc
167d3d4
36ea7dc
 
 
167d3d4
36ea7dc
 
 
 
 
167d3d4
36ea7dc
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import gradio as gr
from transformers import AutoModelForSequenceClassification, AutoTokenizer

model_name = "distilbert-base-uncased-finetuned-sst-2-english"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

def predict_sentiment(text):
    inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512)
    outputs = model(**inputs)
    probs = outputs.logits.softmax(dim=1).detach().numpy()[0]
    return {"Negative": float(probs[0]), "Positive": float(probs[1])}

iface = gr.Interface(fn=predict_sentiment, inputs="text", outputs="label")
iface.launch()