Spaces:
Running
Running
Experimenting with unicorns
Browse files
app.py
CHANGED
@@ -1,6 +1,12 @@
|
|
1 |
import os
|
2 |
os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # Disable GPU and enforce CPU execution
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
import gradio as gr
|
5 |
from transformers import (
|
6 |
DistilBertTokenizerFast,
|
@@ -196,33 +202,74 @@ with gr.Blocks(
|
|
196 |
text-align: center;
|
197 |
font-size: 2.5rem;
|
198 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
footer {
|
200 |
text-align: center;
|
201 |
margin-top: 20px;
|
202 |
font-size: 14px;
|
203 |
color: gray;
|
204 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
"""
|
206 |
) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
207 |
gr.Markdown("# Sentiment Analysis Demo")
|
208 |
gr.Markdown(
|
209 |
"""
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
- **Deep Learning**: GRU, LSTM, and BiLSTM models.
|
214 |
-
- **Transformers**: DistilBERT, TinyBERT, BERT Multilingual, and RoBERTa.
|
215 |
-
|
216 |
-
### Features:
|
217 |
-
- Compare predictions across different models.
|
218 |
-
- See which model predicts the highest and lowest scores.
|
219 |
-
- Get the average sentiment score across all models.
|
220 |
-
- Easily test with your own input or select from suggested reviews.
|
221 |
-
|
222 |
-
Use this app to explore how different models interpret sentiment and compare their outputs!
|
223 |
"""
|
224 |
)
|
225 |
|
|
|
226 |
with gr.Row():
|
227 |
with gr.Column():
|
228 |
text_input = gr.Textbox(
|
@@ -251,7 +298,7 @@ with gr.Blocks(
|
|
251 |
inputs=[sample_dropdown],
|
252 |
outputs=[text_input]
|
253 |
)
|
254 |
-
analyze_button = gr.Button("Analyze Sentiment")
|
255 |
|
256 |
with gr.Row():
|
257 |
with gr.Column():
|
@@ -283,13 +330,59 @@ with gr.Blocks(
|
|
283 |
This demo was built as a part of the NLP course at the University of Zagreb.
|
284 |
Check out our GitHub repository:
|
285 |
<a href="https://github.com/FFZG-NLP-2024/TripAdvisor-Sentiment/" target="_blank">TripAdvisor Sentiment Analysis</a>
|
286 |
-
|
287 |
-
<a href="https://huggingface.co/collections/nhull/nlp-zg-6794604b85fd4216e6470d38" target="_blank">NLP Zagreb HuggingFace Collection</a
|
288 |
</footer>
|
289 |
"""
|
290 |
)
|
291 |
-
|
292 |
def process_input_and_analyze(text_input):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
293 |
results, statistics = analyze_sentiment_and_statistics(text_input)
|
294 |
if "Message" in statistics:
|
295 |
return (
|
|
|
1 |
import os
|
2 |
os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # Disable GPU and enforce CPU execution
|
3 |
|
4 |
+
from PIL import Image
|
5 |
+
from huggingface_hub import hf_hub_download
|
6 |
+
|
7 |
+
# Load a fun unicorn image
|
8 |
+
unicorn_image_path = "unicorn.png"
|
9 |
+
|
10 |
import gradio as gr
|
11 |
from transformers import (
|
12 |
DistilBertTokenizerFast,
|
|
|
202 |
text-align: center;
|
203 |
font-size: 2.5rem;
|
204 |
}
|
205 |
+
.unicorn-image {
|
206 |
+
display: block;
|
207 |
+
margin: auto;
|
208 |
+
width: 300px; /* Larger size */
|
209 |
+
height: auto;
|
210 |
+
border-radius: 20px;
|
211 |
+
margin-bottom: 20px;
|
212 |
+
animation: magical-float 5s ease-in-out infinite; /* Gentle floating animation */
|
213 |
+
}
|
214 |
+
|
215 |
+
@keyframes magical-float {
|
216 |
+
0% {
|
217 |
+
transform: translate(0, 0) rotate(0deg); /* Start position */
|
218 |
+
}
|
219 |
+
25% {
|
220 |
+
transform: translate(10px, -10px) rotate(3deg); /* Slightly up and right, tilted */
|
221 |
+
}
|
222 |
+
50% {
|
223 |
+
transform: translate(0, -20px) rotate(0deg); /* Higher point, back to straight */
|
224 |
+
}
|
225 |
+
75% {
|
226 |
+
transform: translate(-10px, -10px) rotate(-3deg); /* Slightly up and left, tilted */
|
227 |
+
}
|
228 |
+
100% {
|
229 |
+
transform: translate(0, 0) rotate(0deg); /* Return to start position */
|
230 |
+
}
|
231 |
+
}
|
232 |
+
|
233 |
footer {
|
234 |
text-align: center;
|
235 |
margin-top: 20px;
|
236 |
font-size: 14px;
|
237 |
color: gray;
|
238 |
}
|
239 |
+
.custom-analyze-button {
|
240 |
+
background-color: #e8a4c9;
|
241 |
+
color: white;
|
242 |
+
font-size: 1rem;
|
243 |
+
padding: 10px 20px;
|
244 |
+
border-radius: 10px;
|
245 |
+
border: none;
|
246 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
247 |
+
transition: transform 0.2s, background-color 0.2s;
|
248 |
+
}
|
249 |
+
.custom-analyze-button:hover {
|
250 |
+
background-color: #d693b8;
|
251 |
+
transform: scale(1.05);
|
252 |
+
}
|
253 |
"""
|
254 |
) as demo:
|
255 |
+
# Add the unicorn image at the start
|
256 |
+
gr.Image(
|
257 |
+
value=unicorn_image_path, # File path or URL
|
258 |
+
type="filepath", # Correct type for file paths
|
259 |
+
elem_classes=["unicorn-image"]
|
260 |
+
)
|
261 |
+
|
262 |
+
|
263 |
gr.Markdown("# Sentiment Analysis Demo")
|
264 |
gr.Markdown(
|
265 |
"""
|
266 |
+
Welcome! A magical unicorn 🦄 will guide you through this sentiment analysis journey! 🎉
|
267 |
+
This app lets you explore how different models interpret sentiment and compare their predictions.
|
268 |
+
**Enjoy the magic!**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
269 |
"""
|
270 |
)
|
271 |
|
272 |
+
|
273 |
with gr.Row():
|
274 |
with gr.Column():
|
275 |
text_input = gr.Textbox(
|
|
|
298 |
inputs=[sample_dropdown],
|
299 |
outputs=[text_input]
|
300 |
)
|
301 |
+
analyze_button = gr.Button("Analyze Sentiment", elem_classes=["custom-analyze-button"])
|
302 |
|
303 |
with gr.Row():
|
304 |
with gr.Column():
|
|
|
330 |
This demo was built as a part of the NLP course at the University of Zagreb.
|
331 |
Check out our GitHub repository:
|
332 |
<a href="https://github.com/FFZG-NLP-2024/TripAdvisor-Sentiment/" target="_blank">TripAdvisor Sentiment Analysis</a>
|
333 |
+
or explore our HuggingFace collection:
|
334 |
+
<a href="https://huggingface.co/collections/nhull/nlp-zg-6794604b85fd4216e6470d38" target="_blank">NLP Zagreb HuggingFace Collection</a>.
|
335 |
</footer>
|
336 |
"""
|
337 |
)
|
|
|
338 |
def process_input_and_analyze(text_input):
|
339 |
+
# Check for empty input
|
340 |
+
if not text_input.strip():
|
341 |
+
funny_message = "Are you sure you wrote something? Try again! 🧐"
|
342 |
+
return (
|
343 |
+
funny_message, # Logistic Regression
|
344 |
+
funny_message, # GRU
|
345 |
+
funny_message, # LSTM
|
346 |
+
funny_message, # BiLSTM
|
347 |
+
funny_message, # DistilBERT
|
348 |
+
funny_message, # BERT Multilingual
|
349 |
+
funny_message, # TinyBERT
|
350 |
+
funny_message, # RoBERTa
|
351 |
+
"No statistics to display, as nothing was input. 🤷♀️"
|
352 |
+
)
|
353 |
+
|
354 |
+
# Check for one letter/number input
|
355 |
+
if len(text_input.strip()) == 1 or text_input.strip().isdigit():
|
356 |
+
funny_message = "Why not write something that makes sense? 🤔"
|
357 |
+
return (
|
358 |
+
funny_message, # Logistic Regression
|
359 |
+
funny_message, # GRU
|
360 |
+
funny_message, # LSTM
|
361 |
+
funny_message, # BiLSTM
|
362 |
+
funny_message, # DistilBERT
|
363 |
+
funny_message, # BERT Multilingual
|
364 |
+
funny_message, # TinyBERT
|
365 |
+
funny_message, # RoBERTa
|
366 |
+
"No statistics to display for one letter or number. 😅"
|
367 |
+
)
|
368 |
+
|
369 |
+
# Check if the review is shorter than 5 words
|
370 |
+
if len(text_input.split()) < 5:
|
371 |
+
results, statistics = analyze_sentiment_and_statistics(text_input)
|
372 |
+
short_message = "Maybe try with some longer text next time. 😉"
|
373 |
+
return (
|
374 |
+
f"{results['Logistic Regression']} - {short_message}",
|
375 |
+
f"{results['GRU Model']} - {short_message}",
|
376 |
+
f"{results['LSTM Model']} - {short_message}",
|
377 |
+
f"{results['BiLSTM Model']} - {short_message}",
|
378 |
+
f"{results['DistilBERT']} - {short_message}",
|
379 |
+
f"{results['BERT Multilingual (NLP Town)']} - {short_message}",
|
380 |
+
f"{results['TinyBERT']} - {short_message}",
|
381 |
+
f"{results['RoBERTa']} - {short_message}",
|
382 |
+
f"Statistics:\n{statistics['Lowest Score']}\n{statistics['Highest Score']}\nAverage Score: {statistics['Average Score']}\n{short_message}"
|
383 |
+
)
|
384 |
+
|
385 |
+
# Proceed with normal sentiment analysis if none of the above conditions apply
|
386 |
results, statistics = analyze_sentiment_and_statistics(text_input)
|
387 |
if "Message" in statistics:
|
388 |
return (
|