File size: 1,210 Bytes
ce4d526
 
 
 
444a3e7
 
 
 
ce4d526
 
 
 
 
 
 
 
444a3e7
ce4d526
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
444a3e7
 
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
33
34
35
36
37
import gradio as gr
from transformers import pipeline

# Load a pre-trained sentiment analysis pipeline
sentiment_analyzer = pipeline(
    "sentiment-analysis",
    model="cardiffnlp/twitter-roberta-base-sentiment-latest"
)

def analyze_sentiment(text_to_analyze):
    """
    Takes input text and returns the sentiment and confidence score.
    """
    if text_to_analyze:
        result = sentiment_analyzer(text_to_analyze)[0]
        sentiment = result['label']
        confidence = f"{result['score']:.4f}"  # Format confidence to 4 decimal places
        return f"Sentiment: {sentiment}", f"Confidence: {confidence}"
    else:
        return "Please enter some text.", ""

# Define the Gradio interface
iface = gr.Interface(
    fn=analyze_sentiment,
    inputs=gr.Textbox(lines=5, placeholder="Enter text to analyze sentiment..."),
    outputs=[
        gr.Textbox(label="Predicted Sentiment"),
        gr.Textbox(label="Confidence Score")
    ],
    title="Simple Sentiment Analyzer",
    description="Enter a piece of text and the app will predict its sentiment (Positive, Negative, or Neutral) along with a confidence score.",
)

# Launch the Gradio app
if __name__ == "__main__":
    iface.launch()