Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Load the pre-trained sentiment analysis model
|
5 |
+
sentiment_model = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
|
6 |
+
|
7 |
+
# Function to analyze sentiment
|
8 |
+
def analyze_sentiment(text):
|
9 |
+
# Get sentiment predictions
|
10 |
+
result = sentiment_model(text)
|
11 |
+
label = result[0]['label']
|
12 |
+
score = result[0]['score']
|
13 |
+
|
14 |
+
# Format the sentiment into human-friendly text
|
15 |
+
label_mapping = {
|
16 |
+
'1 star': 'Very Negative',
|
17 |
+
'2 stars': 'Negative',
|
18 |
+
'3 stars': 'Neutral',
|
19 |
+
'4 stars': 'Positive',
|
20 |
+
'5 stars': 'Very Positive'
|
21 |
+
}
|
22 |
+
sentiment_result = f"**Sentiment**: {label_mapping[label]}\n**Confidence**: {score:.2f}"
|
23 |
+
return sentiment_result
|
24 |
+
|
25 |
+
# Custom CSS for appealing colors on dark and light themes
|
26 |
+
css = """
|
27 |
+
body {background-color: #f4f4f4; font-family: 'Arial'; color: #333;}
|
28 |
+
input, textarea {border-radius: 10px; border: 2px solid #999; padding: 10px; background-color: #fff; color: #333;}
|
29 |
+
button {background-color: #3498db; color: white; border: none; padding: 10px 20px; border-radius: 10px; cursor: pointer;}
|
30 |
+
button:hover {background-color: #2980b9;}
|
31 |
+
.output-text {font-size: 18px; color: #333;}
|
32 |
+
footer {display: none !important;}
|
33 |
+
/* Dark theme */
|
34 |
+
@media (prefers-color-scheme: dark) {
|
35 |
+
body {background-color: #2c3e50; color: #ecf0f1;}
|
36 |
+
input, textarea {background-color: #34495e; color: #ecf0f1; border: 2px solid #999;}
|
37 |
+
button {background-color: #2980b9;}
|
38 |
+
button:hover {background-color: #1abc9c;}
|
39 |
+
.output-text {color: #ecf0f1;}
|
40 |
+
}
|
41 |
+
"""
|
42 |
+
|
43 |
+
# Create Gradio interface with enhanced UI and updated description
|
44 |
+
interface = gr.Interface(
|
45 |
+
fn=analyze_sentiment,
|
46 |
+
inputs=gr.Textbox(
|
47 |
+
lines=5,
|
48 |
+
placeholder="Enter a marketplace review or sentence here...",
|
49 |
+
label="Input Review",
|
50 |
+
),
|
51 |
+
outputs=gr.Markdown(),
|
52 |
+
title="Sentiment Reveal",
|
53 |
+
description=(
|
54 |
+
"Analyze the sentiment of product reviews in English, Dutch, German, French, Italian, and Spanish. Focused Sentiment Analysis for eCommerce."
|
55 |
+
),
|
56 |
+
examples=[["This product is amazing! I highly recommend it."],
|
57 |
+
["I'm very disappointed with this purchase."],
|
58 |
+
["The product was okay, not great but not terrible."]],
|
59 |
+
allow_flagging="never",
|
60 |
+
css=css,
|
61 |
+
)
|
62 |
+
|
63 |
+
# Launch the app with sharing enabled
|
64 |
+
interface.launch(share=True)
|