import gradio as gr from transformers import pipeline import torch # Load the sentiment analysis model sentiment_analyzer = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment") def analyze_sentiment(input_text): # Analyze the sentiment of the input text result = sentiment_analyzer(input_text) sentiment_score = result[0]['label'] return sentiment_score # Predefined examples example_inputs = [ "I love this product! It's amazing!", "This was the worst experience I've ever had.", "The movie was okay, not great but not bad either.", "Absolutely fantastic! I would recommend it to everyone." ] # Create the Gradio interface interface = gr.Interface( fn=analyze_sentiment, inputs=gr.Textbox(label="Enter Text", placeholder="Type or paste your sentence or paragraph here...", lines=5), outputs=gr.Textbox(label="Predicted Sentiment (1 to 5 stars)"), examples=example_inputs, title="Sentiment Analysis App", description="Enter a sentence or paragraph and click the button to analyze the sentiment. The output will show a score from 1 to 5 stars." ) # Launch the app if __name__ == "__main__": interface.launch()