Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import nltk
|
|
|
|
| 3 |
from nltk.sentiment.vader import SentimentIntensityAnalyzer
|
| 4 |
|
| 5 |
nltk.download("vader_lexicon")
|
|
@@ -8,14 +9,27 @@ sid = SentimentIntensityAnalyzer()
|
|
| 8 |
def sentiment_analysis(text):
|
| 9 |
scores = sid.polarity_scores(text)
|
| 10 |
del scores["compound"]
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
demo = gr.Interface(
|
| 14 |
fn=sentiment_analysis,
|
| 15 |
inputs=gr.Textbox(lines=5, placeholder="Enter a positive or negative sentence here..."),
|
| 16 |
-
outputs=
|
| 17 |
title="Sentiment Analysis",
|
| 18 |
-
description="Enter a sentence to analyze its sentiment. The model will return the sentiment scores for positive, negative, and neutral tones.",
|
| 19 |
examples=[["This is wonderful!"], ["I hate this!"], ["It's okay, not bad."]]
|
| 20 |
)
|
| 21 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import nltk
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
from nltk.sentiment.vader import SentimentIntensityAnalyzer
|
| 5 |
|
| 6 |
nltk.download("vader_lexicon")
|
|
|
|
| 9 |
def sentiment_analysis(text):
|
| 10 |
scores = sid.polarity_scores(text)
|
| 11 |
del scores["compound"]
|
| 12 |
+
|
| 13 |
+
labels = list(scores.keys())
|
| 14 |
+
values = list(scores.values())
|
| 15 |
+
colors = ['red' if label == 'neg' else 'green' if label == 'pos' else 'white' for label in labels]
|
| 16 |
+
|
| 17 |
+
plt.figure(figsize=(5, 3))
|
| 18 |
+
plt.bar(labels, values, color=colors)
|
| 19 |
+
plt.ylim(0, 1)
|
| 20 |
+
plt.title("Sentiment Analysis")
|
| 21 |
+
plt.xlabel("Sentiment")
|
| 22 |
+
plt.ylabel("Score")
|
| 23 |
+
|
| 24 |
+
plt.savefig('sentiment_chart.png')
|
| 25 |
+
return gr.Image('sentiment_chart.png')
|
| 26 |
|
| 27 |
demo = gr.Interface(
|
| 28 |
fn=sentiment_analysis,
|
| 29 |
inputs=gr.Textbox(lines=5, placeholder="Enter a positive or negative sentence here..."),
|
| 30 |
+
outputs="image",
|
| 31 |
title="Sentiment Analysis",
|
| 32 |
+
description="Enter a sentence to analyze its sentiment. The model will return the sentiment scores for positive, negative, and neutral tones as a bar chart.",
|
| 33 |
examples=[["This is wonderful!"], ["I hate this!"], ["It's okay, not bad."]]
|
| 34 |
)
|
| 35 |
|