Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import pipeline | |
| # Load the sentiment analysis model | |
| sentiment_analyzer = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment") | |
| # Function to analyze sentiment | |
| def analyze_sentiment(text): | |
| if len(text.strip()) == 0: | |
| return "Please enter some text for sentiment analysis." | |
| result = sentiment_analyzer(text)[0] | |
| # Extract numerical rating from the label (e.g., "5 stars" → "5") | |
| sentiment_label = result['label'].split()[0] # Extract only the number (1-5) | |
| confidence = round(result['score'] * 100, 2) # Convert to percentage | |
| return f"⭐ Sentiment: {sentiment_label} Stars (Confidence: {confidence}%)" | |
| # Create the Gradio interface | |
| iface = gr.Interface( | |
| fn=analyze_sentiment, | |
| inputs=gr.Textbox(lines=3, placeholder="Enter a sentence or paragraph...", label="Input Text"), | |
| outputs=gr.Textbox(label="Sentiment Analysis Result"), | |
| title="Sentiment Analysis with BERT", | |
| description="Enter a sentence or paragraph to analyze its sentiment using a pre-trained BERT model.", | |
| 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."] | |
| ], | |
| allow_flagging="never" | |
| ) | |
| # Launch the app | |
| iface.launch() | |