Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,235 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
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 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
if
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from datetime import datetime
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import io
|
6 |
+
import base64
|
7 |
+
from langdetect import detect
|
8 |
+
import qrcode
|
9 |
+
from wordcloud import WordCloud
|
10 |
+
import nltk
|
11 |
+
from nltk.tokenize import sent_tokenize
|
12 |
+
from better_profanity import profanity
|
13 |
+
import tempfile
|
14 |
+
import os
|
15 |
+
|
16 |
+
# Download NLTK data
|
17 |
+
nltk.download('punkt', quiet=True)
|
18 |
+
|
19 |
+
# Model options
|
20 |
+
models = {
|
21 |
+
"DistilBERT": "distilbert-base-uncased-finetuned-sst-2-english",
|
22 |
+
"Twitter RoBERTa": "cardiffnlp/twitter-roberta-base-sentiment-latest"
|
23 |
+
}
|
24 |
+
analyzer = None
|
25 |
+
history = []
|
26 |
+
sentiment_scores = []
|
27 |
+
feedback_log = []
|
28 |
+
|
29 |
+
# Load the selected or custom model
|
30 |
+
def load_model(model_name, custom_model_path=None):
|
31 |
+
global analyzer
|
32 |
+
if custom_model_path:
|
33 |
+
analyzer = pipeline("sentiment-analysis", model=custom_model_path)
|
34 |
+
return f"Loaded custom model from {custom_model_path}"
|
35 |
+
analyzer = pipeline("sentiment-analysis", model=models[model_name])
|
36 |
+
return f"Loaded {model_name} model."
|
37 |
+
|
38 |
+
# Highlight words
|
39 |
+
def highlight_words(text, sentiment, pos_words, neg_words):
|
40 |
+
words = text.split()
|
41 |
+
highlighted = []
|
42 |
+
pos_list = pos_words.split(",") if pos_words else ["love", "great", "happy", "awesome", "good"]
|
43 |
+
neg_list = neg_words.split(",") if neg_words else ["hate", "bad", "terrible", "awful", "sad"]
|
44 |
+
for word in words:
|
45 |
+
if sentiment == "POSITIVE" and word.lower() in [w.strip().lower() for w in pos_list]:
|
46 |
+
highlighted.append(f"**{word}**")
|
47 |
+
elif sentiment == "NEGATIVE" and word.lower() in [w.strip().lower() for w in neg_list]:
|
48 |
+
highlighted.append(f"**{word}**")
|
49 |
+
else:
|
50 |
+
highlighted.append(word)
|
51 |
+
return " ".join(highlighted)
|
52 |
+
|
53 |
+
# Sentiment analysis with context
|
54 |
+
def analyze_sentiment(text, model_name, pos_words, neg_words, intensity, custom_model_path=None, source="manual", compare_text=None):
|
55 |
+
if not text or not text.strip():
|
56 |
+
return "Error: Please enter some text.", "", "", None, None, "", None, ""
|
57 |
+
try:
|
58 |
+
if analyzer is None:
|
59 |
+
load_model(model_name, custom_model_path)
|
60 |
+
|
61 |
+
# Language and profanity check
|
62 |
+
lang = detect(text)
|
63 |
+
lang_note = " (Warning: Text may not be in English)" if lang != "en" else ""
|
64 |
+
profanity_note = " (Warning: Inappropriate language detected)" if profanity.contains_profanity(text) else ""
|
65 |
+
|
66 |
+
# Contextual analysis
|
67 |
+
sentences = sent_tokenize(text)
|
68 |
+
results = []
|
69 |
+
for sent in sentences:
|
70 |
+
result = analyzer(sent)[0]
|
71 |
+
label, score = result['label'], result['score']
|
72 |
+
if score < intensity:
|
73 |
+
label = "NEUTRAL"
|
74 |
+
results.append(f"{sent} -> {label} ({score:.2f})")
|
75 |
+
combined_result = analyzer(text)[0]
|
76 |
+
label, score = combined_result['label'], combined_result['score']
|
77 |
+
if score < intensity:
|
78 |
+
label, emoji = "NEUTRAL", "π"
|
79 |
+
else:
|
80 |
+
emoji = "π" if "POSITIVE" in label.upper() else "π" if "NEGATIVE" in label.upper() else "π"
|
81 |
+
confidence_note = " (Low confidence)" if score < 0.7 else ""
|
82 |
+
sentiment_result = f"Overall: {label} {emoji} (Confidence: {score:.2f}{confidence_note}{lang_note}{profanity_note})\n" + "\n".join(results)
|
83 |
+
highlighted_text = highlight_words(text, label, pos_words, neg_words)
|
84 |
+
|
85 |
+
# History and scores
|
86 |
+
timestamp = datetime.now().strftime('%H:%M:%S')
|
87 |
+
history.append(f"[{timestamp}] {source}: {text} -> {sentiment_result.splitlines()[0]}")
|
88 |
+
sentiment_scores.append((timestamp, 1 if "POSITIVE" in label.upper() else -1 if "NEGATIVE" in label.upper() else 0))
|
89 |
+
history_str = "\n".join([h for h in history[-5:]])
|
90 |
+
|
91 |
+
# Visuals
|
92 |
+
trend_img = generate_timeline()
|
93 |
+
wordcloud_img = generate_wordcloud(text)
|
94 |
+
qr_img = generate_qr(f"https://example.com/share?text={text}&result={sentiment_result.splitlines()[0].replace(' ', '+')}")
|
95 |
+
|
96 |
+
# Comparative analysis
|
97 |
+
compare_result = ""
|
98 |
+
if compare_text:
|
99 |
+
comp_result = analyzer(compare_text)[0]
|
100 |
+
comp_label, comp_score = comp_result['label'], comp_result['score']
|
101 |
+
comp_emoji = "π" if "POSITIVE" in comp_label.upper() else "π" if "NEGATIVE" in comp_label.upper() else "π"
|
102 |
+
compare_result = f"Comparison: {comp_label} {comp_emoji} (Confidence: {comp_score:.2f})"
|
103 |
+
|
104 |
+
return sentiment_result, highlighted_text, history_str, trend_img, wordcloud_img, qr_img, compare_result, ""
|
105 |
+
except Exception as e:
|
106 |
+
return f"Error: {str(e)}", "", "", None, None, "", "", ""
|
107 |
+
|
108 |
+
# Fetch X post (simulated)
|
109 |
+
def fetch_x_post(x_url, model_name, pos_words, neg_words, intensity, custom_model_path):
|
110 |
+
sample_text = "Sample X post from " + x_url
|
111 |
+
return analyze_sentiment(sample_text, model_name, pos_words, neg_words, intensity, custom_model_path, source="X post")
|
112 |
+
|
113 |
+
# Generate timeline
|
114 |
+
def generate_timeline():
|
115 |
+
if not sentiment_scores:
|
116 |
+
return None
|
117 |
+
times, scores = zip(*sentiment_scores[-10:])
|
118 |
+
plt.figure(figsize=(6, 3))
|
119 |
+
plt.plot(times, scores, marker='o', linestyle='-', color='b')
|
120 |
+
plt.title("Sentiment Timeline")
|
121 |
+
plt.xlabel("Time")
|
122 |
+
plt.ylabel("Sentiment")
|
123 |
+
plt.ylim(-1.5, 1.5)
|
124 |
+
plt.xticks(rotation=45)
|
125 |
+
plt.tight_layout()
|
126 |
+
buf = io.BytesIO()
|
127 |
+
plt.savefig(buf, format="png")
|
128 |
+
buf.seek(0)
|
129 |
+
img_str = "data:image/png;base64," + base64.b64encode(buf.getvalue()).decode()
|
130 |
+
plt.close()
|
131 |
+
return img_str
|
132 |
+
|
133 |
+
# Generate word cloud
|
134 |
+
def generate_wordcloud(text):
|
135 |
+
wordcloud = WordCloud(width=400, height=200, background_color="white").generate(text)
|
136 |
+
buf = io.BytesIO()
|
137 |
+
wordcloud.to_image().save(buf, format="PNG")
|
138 |
+
buf.seek(0)
|
139 |
+
img_str = "data:image/png;base64," + base64.b64encode(buf.getvalue()).decode()
|
140 |
+
return img_str
|
141 |
+
|
142 |
+
# Generate QR code
|
143 |
+
def generate_qr(url):
|
144 |
+
qr = qrcode.QRCode(version=1, box_size=10, border=4)
|
145 |
+
qr.add_data(url)
|
146 |
+
qr.make(fit=True)
|
147 |
+
img = qr.make_image(fill="black", back_color="white")
|
148 |
+
buf = io.BytesIO()
|
149 |
+
img.save(buf, format="PNG")
|
150 |
+
buf.seek(0)
|
151 |
+
img_str = "data:image/png;base64," + base64.b64encode(buf.getvalue()).decode()
|
152 |
+
return img_str
|
153 |
+
|
154 |
+
# Export history with proper file handling
|
155 |
+
def export_history():
|
156 |
+
if not history:
|
157 |
+
return None
|
158 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="w") as temp_file:
|
159 |
+
temp_file.write("\n".join(history))
|
160 |
+
temp_path = temp_file.name
|
161 |
+
return temp_path
|
162 |
+
|
163 |
+
# Log feedback
|
164 |
+
def log_feedback(rating):
|
165 |
+
feedback_log.append(f"[{datetime.now().strftime('%H:%M:%S')}] Rating: {rating}/5")
|
166 |
+
return f"Feedback received! ({len(feedback_log)} total)"
|
167 |
+
|
168 |
+
# Theme toggle function
|
169 |
+
def toggle_theme(light_mode):
|
170 |
+
return "Theme switched to " + ("Light" if light_mode else "Dark") + ". Please refresh the page to apply."
|
171 |
+
|
172 |
+
# Gradio interface
|
173 |
+
with gr.Blocks(theme=gr.themes.Monochrome()) as interface:
|
174 |
+
gr.Markdown("# Sentiment Analysis App")
|
175 |
+
gr.Markdown("Next-level sentiment analysis with context, comparison, and more!")
|
176 |
+
|
177 |
+
with gr.Row():
|
178 |
+
with gr.Column(scale=2):
|
179 |
+
model_dropdown = gr.Dropdown(choices=list(models.keys()), label="Select Model", value="DistilBERT")
|
180 |
+
custom_model = gr.File(label="Upload Custom Model (optional)", file_types=[".bin", ".pt"])
|
181 |
+
text_input = gr.Textbox(label="Enter text or X URL", placeholder="Type text or paste an X URL...")
|
182 |
+
compare_input = gr.Textbox(label="Compare with (optional)", placeholder="Enter second text...")
|
183 |
+
audio_input = gr.Audio(label="Or Speak Your Text", type="filepath")
|
184 |
+
pos_words = gr.Textbox(label="Custom Positive Words", placeholder="love, great")
|
185 |
+
neg_words = gr.Textbox(label="Custom Negative Words", placeholder="hate, bad")
|
186 |
+
intensity_slider = gr.Slider(0.5, 1.0, value=0.7, label="Sentiment Intensity Threshold")
|
187 |
+
x_button = gr.Button("Analyze X Post")
|
188 |
+
with gr.Column(scale=3):
|
189 |
+
sentiment_output = gr.Textbox(label="Sentiment Result (Contextual)")
|
190 |
+
highlighted_output = gr.Textbox(label="Highlighted Text")
|
191 |
+
history_output = gr.Textbox(label="Analysis History (Last 5)", lines=5)
|
192 |
+
trend_output = gr.Image(label="Sentiment Timeline")
|
193 |
+
wordcloud_output = gr.Image(label="Word Cloud")
|
194 |
+
qr_output = gr.Image(label="Shareable QR Code")
|
195 |
+
compare_output = gr.Textbox(label="Comparative Analysis")
|
196 |
+
|
197 |
+
with gr.Row():
|
198 |
+
export_button = gr.Button("Export History")
|
199 |
+
export_file = gr.File(label="Download History")
|
200 |
+
theme_toggle = gr.Checkbox(label="Light Mode", value=False)
|
201 |
+
theme_status = gr.Textbox(label="Theme Status", value="Dark (default)")
|
202 |
+
feedback_slider = gr.Slider(1, 5, step=1, label="Rate this analysis (1-5)")
|
203 |
+
feedback_output = gr.Textbox(label="Feedback Status")
|
204 |
+
|
205 |
+
gr.Examples(
|
206 |
+
examples=["I love this app! Itβs great.", "This is awful and sad.", "https://x.com/sample/post"],
|
207 |
+
inputs=[text_input]
|
208 |
+
)
|
209 |
+
|
210 |
+
# Event handlers
|
211 |
+
def audio_to_text(audio_file, model_name, pos_words, neg_words, intensity, custom_model_path):
|
212 |
+
text = "Simulated speech: I feel great today" if audio_file else ""
|
213 |
+
return analyze_sentiment(text, model_name, pos_words, neg_words, intensity, custom_model_path, source="audio")
|
214 |
+
|
215 |
+
text_input.change(
|
216 |
+
fn=analyze_sentiment,
|
217 |
+
inputs=[text_input, model_dropdown, pos_words, neg_words, intensity_slider, custom_model],
|
218 |
+
outputs=[sentiment_output, highlighted_output, history_output, trend_output, wordcloud_output, qr_output, compare_output, feedback_output]
|
219 |
+
)
|
220 |
+
x_button.click(
|
221 |
+
fn=fetch_x_post,
|
222 |
+
inputs=[text_input, model_dropdown, pos_words, neg_words, intensity_slider, custom_model],
|
223 |
+
outputs=[sentiment_output, highlighted_output, history_output, trend_output, wordcloud_output, qr_output, compare_output, feedback_output]
|
224 |
+
)
|
225 |
+
audio_input.change(
|
226 |
+
fn=audio_to_text,
|
227 |
+
inputs=[audio_input, model_dropdown, pos_words, neg_words, intensity_slider, custom_model],
|
228 |
+
outputs=[sentiment_output, highlighted_output, history_output, trend_output, wordcloud_output, qr_output, compare_output, feedback_output]
|
229 |
+
)
|
230 |
+
export_button.click(fn=export_history, inputs=None, outputs=export_file)
|
231 |
+
theme_toggle.change(fn=toggle_theme, inputs=theme_toggle, outputs=theme_status)
|
232 |
+
feedback_slider.change(fn=log_feedback, inputs=feedback_slider, outputs=feedback_output)
|
233 |
+
|
234 |
+
# Launch the app
|
235 |
+
interface.launch()
|