Sentiment / app.py
keerthii05's picture
Update app.py
444a3e7 verified
Raw History Blame Contribute Delete
1.21 kB
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()