iamshukla's picture
feat: Redesign Gradio UI
3ef88e9
import gradio as gr
from transformers import pipeline
# Load the pre-trained sentiment analysis model
sentiment_model = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
# Function to analyze sentiment
def analyze_sentiment(text):
# Get sentiment predictions
result = sentiment_model(text)
label = result[0]['label']
score = result[0]['score']
# Map labels to scores for the Label component
label_mapping = {
'1 star': 'Very Negative',
'2 stars': 'Negative',
'3 stars': 'Neutral',
'4 stars': 'Positive',
'5 stars': 'Very Positive'
}
# Return a dictionary of labels and their confidences
return {label_mapping[label]: score}
# Custom CSS for appealing colors on dark and light themes
css = """
body, .gradio-container {background-color: #f4f4f4; font-family: 'Arial'; color: #333;}
.gr-input {border-radius: 10px; border: 2px solid #999; padding: 10px; background-color: #fff; color: #333;}
.gr-button {background-color: #3498db; color: white; border: none; padding: 10px 20px; border-radius: 10px; cursor: pointer;}
.gr-button:hover {background-color: #2980b9;}
.output-text {font-size: 18px; color: #333;}
footer {display: none !important;}
/* Dark theme */
@media (prefers-color-scheme: dark) {
body, .gradio-container {background-color: #2c3e50; color: #ecf0f1;}
.gr-input {background-color: #34495e; color: #ecf0f1; border: 2px solid #999;}
.gr-button {background-color: #2980b9;}
.gr-button:hover {background-color: #1abc9c;}
.output-text {color: #ecf0f1;}
}
"""
# Create Gradio interface with enhanced UI and updated description
interface = gr.Interface(
fn=analyze_sentiment,
inputs=gr.Textbox(
lines=5,
placeholder="Enter a marketplace review or sentence here...",
label="Input Review",
),
outputs=gr.Label(num_top_classes=5),
title="Sentiment Reveal",
description=(
"Analyze the sentiment of product reviews in English, Dutch, German, French, Italian, and Spanish. Focused Sentiment Analysis for eCommerce."
),
examples=[["This product is amazing! I highly recommend it."],
["I'm very disappointed with this purchase."],
["The product was okay, not great but not terrible."]],
allow_flagging="never",
css=css,
)
# Launch the app with sharing enabled
interface.launch(share=True)