File size: 1,040 Bytes
c7a74ff
033d543
c7a74ff
033d543
 
99aedab
033d543
 
 
 
 
 
 
 
 
 
c7a74ff
033d543
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline

# Load the sentiment analysis pipeline with DistilBERT
distilbert_pipeline = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")

def predict_sentiment(text):
    """
    Predicts the sentiment of the input text using DistilBERT.
    :param text: str, input text to analyze.
    :return: str, predicted sentiment and confidence score.
    """
    result = distilbert_pipeline(text)[0]
    label = result['label']
    score = result['score']
    return f"Sentiment: {label}, Confidence: {score:.2f}"

# Create a Gradio interface
iface = gr.Interface(fn=predict_sentiment,
                     inputs=gr.inputs.Textbox(lines=2, placeholder="Type your text here..."),
                     outputs="text",
                     title="Sentiment Analysis with DistilBERT",
                     description="This model predicts the sentiment of the input text. Enter a sentence to see if it's positive or negative.")

# Launch the interface
iface.launch()