Spaces:
Sleeping
Sleeping
Commit
·
aad2b5b
1
Parent(s):
43ce14e
new files
Browse files- app.py +127 -0
- requirements.txt +8 -0
app.py
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# pylint: disable=import-error
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
from transformers import pipeline
|
6 |
+
|
7 |
+
# Load pre-trained sentiment analysis model
|
8 |
+
sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
9 |
+
|
10 |
+
def analyze_sentiment(text):
|
11 |
+
"""
|
12 |
+
Analyze the sentiment of input text
|
13 |
+
"""
|
14 |
+
if not text or not text.strip():
|
15 |
+
return {
|
16 |
+
"Sentiment": "N/A",
|
17 |
+
"Confidence": 0,
|
18 |
+
"Positive": 0,
|
19 |
+
"Negative": 0
|
20 |
+
}
|
21 |
+
|
22 |
+
result = sentiment_analyzer(text)[0]
|
23 |
+
sentiment = result["label"]
|
24 |
+
confidence = result["score"]
|
25 |
+
|
26 |
+
# Create result dictionary
|
27 |
+
output = {
|
28 |
+
"Sentiment": "Positive" if sentiment == "POSITIVE" else "Negative",
|
29 |
+
"Confidence": round(confidence * 100, 2)
|
30 |
+
}
|
31 |
+
|
32 |
+
# Add values for the gauge chart
|
33 |
+
output["Positive"] = confidence if sentiment == "POSITIVE" else 1 - confidence
|
34 |
+
output["Negative"] = 1 - output["Positive"]
|
35 |
+
|
36 |
+
return output
|
37 |
+
|
38 |
+
def process_text(text):
|
39 |
+
"""
|
40 |
+
Process the text and create visualizations
|
41 |
+
"""
|
42 |
+
result = analyze_sentiment(text)
|
43 |
+
|
44 |
+
# Create a visual representation of the sentiment
|
45 |
+
labels = ['Positive', 'Negative']
|
46 |
+
sizes = [result["Positive"], result["Negative"]]
|
47 |
+
colors = ['#4CAF50', '#F44336']
|
48 |
+
|
49 |
+
fig, ax = plt.subplots(figsize=(5, 3))
|
50 |
+
ax.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90)
|
51 |
+
ax.axis('equal')
|
52 |
+
plt.title('Sentiment Analysis')
|
53 |
+
|
54 |
+
return result["Sentiment"], result["Confidence"], fig
|
55 |
+
|
56 |
+
# Define examples for users to try
|
57 |
+
examples = [
|
58 |
+
["I love this product! It's absolutely fantastic and exceeded all my expectations."],
|
59 |
+
["This was a terrible experience. I will never use this service again."],
|
60 |
+
["The quality is okay, but the price is a bit high for what you get."],
|
61 |
+
["I've had better, but I've also had much worse. It's a decent option."],
|
62 |
+
["This is the best decision I've ever made. Highly recommended!"]
|
63 |
+
]
|
64 |
+
|
65 |
+
# Create Gradio interface
|
66 |
+
with gr.Blocks(title="Sentiment Analyzer", theme=gr.themes.Soft()) as demo:
|
67 |
+
gr.Markdown(
|
68 |
+
"""
|
69 |
+
# 💬 Text Sentiment Analyzer
|
70 |
+
|
71 |
+
This interactive tool analyzes the sentiment of any text, determining whether it's positive or negative.
|
72 |
+
It's particularly useful for analyzing customer feedback, social media comments, or product reviews.
|
73 |
+
|
74 |
+
Try typing or pasting text in the input area below, or select one of the examples to see how it works!
|
75 |
+
"""
|
76 |
+
)
|
77 |
+
|
78 |
+
with gr.Row():
|
79 |
+
with gr.Column(scale=3):
|
80 |
+
text_input = gr.Textbox(
|
81 |
+
label="Enter text to analyze",
|
82 |
+
placeholder="Type or paste text here...",
|
83 |
+
lines=5
|
84 |
+
)
|
85 |
+
|
86 |
+
analyze_btn = gr.Button("Analyze Sentiment", variant="primary")
|
87 |
+
|
88 |
+
with gr.Column(scale=2):
|
89 |
+
sentiment_label = gr.Label(label="Result")
|
90 |
+
confidence = gr.Number(label="Confidence Score (%)")
|
91 |
+
sentiment_gauge = gr.Plot(label="Sentiment Distribution")
|
92 |
+
|
93 |
+
# Add examples section
|
94 |
+
gr.Examples(
|
95 |
+
examples=examples,
|
96 |
+
inputs=text_input,
|
97 |
+
outputs=[sentiment_label, confidence, sentiment_gauge],
|
98 |
+
fn=process_text,
|
99 |
+
cache_examples=True
|
100 |
+
)
|
101 |
+
|
102 |
+
# Set up the click event
|
103 |
+
analyze_btn.click(
|
104 |
+
fn=process_text,
|
105 |
+
inputs=text_input,
|
106 |
+
outputs=[sentiment_label, confidence, sentiment_gauge]
|
107 |
+
)
|
108 |
+
|
109 |
+
gr.Markdown("""
|
110 |
+
### How it works
|
111 |
+
|
112 |
+
This tool uses a DistilBERT model fine-tuned for sentiment analysis. The model has been trained on a large dataset
|
113 |
+
of text with positive and negative sentiments, allowing it to recognize emotional tone in written text.
|
114 |
+
|
115 |
+
### Applications
|
116 |
+
|
117 |
+
- **Customer Service**: Monitor customer feedback in real-time
|
118 |
+
- **Market Research**: Analyze opinions about products or services
|
119 |
+
- **Social Media Monitoring**: Track sentiment about your brand across platforms
|
120 |
+
- **Content Analysis**: Evaluate the emotional impact of your content
|
121 |
+
|
122 |
+
Created by [Vinicius Guerra e Ribas](https://viniciusgribas.netlify.app/)
|
123 |
+
""")
|
124 |
+
|
125 |
+
# Launch the app
|
126 |
+
if __name__ == "__main__":
|
127 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==5.21.0
|
2 |
+
gradio_client==1.7.2
|
3 |
+
matplotlib==3.10.1
|
4 |
+
matplotlib-inline @ file:///home/conda/feedstock_root/build_artifacts/matplotlib-inline_1733416936468/work
|
5 |
+
numpy==2.1.2
|
6 |
+
torch==2.6.0+cpu
|
7 |
+
torchvision==0.21.0+cpu
|
8 |
+
transformers==4.49.0
|