Spaces:
Sleeping
Sleeping
Update app.py to use rule-based sentiment analysis and remove transformers dependency
Browse files
app.py
CHANGED
|
@@ -1,23 +1,34 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
def analyze_sentiment(text):
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
iface = gr.Interface(
|
| 15 |
fn=analyze_sentiment,
|
| 16 |
-
inputs=gr.Textbox(
|
| 17 |
-
outputs=
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
)
|
| 21 |
|
| 22 |
if __name__ == "__main__":
|
| 23 |
-
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
+
# Simple rule-based sentiment analysis using sets of positive and negative words
|
| 4 |
+
positive_words = {"good", "great", "happy", "fantastic", "excellent", "love", "awesome", "amazing", "wonderful", "like", "nice"}
|
| 5 |
+
negative_words = {"bad", "terrible", "sad", "horrible", "awful", "hate", "angry", "poor", "disappointing", "worst"}
|
| 6 |
|
| 7 |
def analyze_sentiment(text):
|
| 8 |
+
words = text.lower().split()
|
| 9 |
+
pos_count = sum(word in positive_words for word in words)
|
| 10 |
+
neg_count = sum(word in negative_words for word in words)
|
| 11 |
+
if pos_count > neg_count:
|
| 12 |
+
sentiment = "Positive"
|
| 13 |
+
score = pos_count / (pos_count + neg_count) if (pos_count + neg_count) > 0 else 0
|
| 14 |
+
elif neg_count > pos_count:
|
| 15 |
+
sentiment = "Negative"
|
| 16 |
+
score = neg_count / (pos_count + neg_count) if (pos_count + neg_count) > 0 else 0
|
| 17 |
+
else:
|
| 18 |
+
sentiment = "Neutral"
|
| 19 |
+
score = 0.5 if (pos_count + neg_count) > 0 else 0.5
|
| 20 |
+
return sentiment, round(score, 2)
|
| 21 |
|
| 22 |
+
interface = gr.Interface(
|
|
|
|
| 23 |
fn=analyze_sentiment,
|
| 24 |
+
inputs=gr.Textbox(label="Enter text"),
|
| 25 |
+
outputs=[
|
| 26 |
+
gr.Textbox(label="Sentiment"),
|
| 27 |
+
gr.Number(label="Score")
|
| 28 |
+
],
|
| 29 |
+
title="Rule-based Sentiment Analysis",
|
| 30 |
+
description="A tiny AI model that performs simple sentiment analysis using a list of positive and negative words."
|
| 31 |
)
|
| 32 |
|
| 33 |
if __name__ == "__main__":
|
| 34 |
+
interface.launch()
|