import gradio as gr from transformers import pipeline # Load sentiment analysis model from Hugging Face sentiment_analyzer = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment") # Function to analyze sentiment and convert it to star rating (1-5) def analyze_sentiment(text): result = sentiment_analyzer(text)[0] sentiment_score = result['label'] # Convert sentiment score to numeric star rating (1-5 stars) if sentiment_score == '1 star': return 1 elif sentiment_score == '2 stars': return 2 elif sentiment_score == '3 stars': return 3 elif sentiment_score == '4 stars': return 4 else: return 5 # Define example sentences for easy testing examples = [ ["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."] ] # Build the Gradio interface iface = gr.Interface( fn=analyze_sentiment, # Function to call for sentiment analysis inputs=[ gr.Textbox(label="Enter Text", placeholder="Type or paste a sentence or paragraph here...", lines=5), gr.Button("Analyze Sentiment") # Button to trigger analysis ], outputs=gr.Textbox(label="Sentiment Rating (1 to 5 stars)"), # Display sentiment rating live=False, # Disable live preview while typing examples=examples, # Predefined examples description="Sentiment analysis using BERT-based model for multilingual sentiment prediction." ) # Launch the Gradio interface iface.launch()