File size: 854 Bytes
af4daa5
 
5ae1910
af4daa5
 
 
5ae1910
af4daa5
 
 
 
 
 
 
 
 
73d93e2
af4daa5
73d93e2
af4daa5
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification

# Load pre-trained tokenizer and model
tokenizer = AutoTokenizer.from_pretrained('distilbert-base-uncased')
model = AutoModelForSequenceClassification.from_pretrained('distilbert-base-uncased', num_labels=2)

def predict_sentiment(input_text):
    # Tokenization
    inputs = tokenizer(input_text, return_tensors='pt')
    
    # Prediction
    outputs = model(**inputs)
    probabilities = outputs[0][0].detach().numpy()
    labels = ['Negative', 'Positive']
    predicted_label = labels[probabilities.argmax()]

    return {"Text": input_text, "Sentiment": predicted_label}

iface = gr.Interface(predict_sentiment, input_type="text", output_types=["text"],
                      input_label="Enter Text", output_label="Predicted Sentiment")

iface.launch()