File size: 1,651 Bytes
4ffa2ae f059988 4ffa2ae f059988 4ffa2ae f059988 4ffa2ae f059988 4ffa2ae 184d753 4ffa2ae f059988 4ffa2ae f059988 4ffa2ae 184d753 |
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 38 39 40 41 42 43 44 45 46 47 48 |
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()
|