Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,12 @@
|
|
| 1 |
-
# ============================================
|
| 2 |
-
# RULE-BASED SENTIMENT ANALYSIS
|
| 3 |
-
# Python + Gradio
|
| 4 |
-
# ============================================
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
# Import libraries
|
| 9 |
import re
|
| 10 |
import gradio as gr
|
| 11 |
|
| 12 |
|
| 13 |
-
# ============================================
|
| 14 |
-
# SENTIMENT
|
| 15 |
-
# ============================================
|
| 16 |
|
| 17 |
-
|
| 18 |
"good",
|
| 19 |
"great",
|
| 20 |
"excellent",
|
|
@@ -41,7 +33,7 @@ positive_words = {
|
|
| 41 |
}
|
| 42 |
|
| 43 |
|
| 44 |
-
|
| 45 |
"bad",
|
| 46 |
"terrible",
|
| 47 |
"awful",
|
|
@@ -70,80 +62,89 @@ negative_words = {
|
|
| 70 |
}
|
| 71 |
|
| 72 |
|
| 73 |
-
# ============================================
|
| 74 |
# SENTIMENT ANALYSIS FUNCTION
|
| 75 |
-
# ============================================
|
| 76 |
|
| 77 |
def sentiment_analyzer(text):
|
| 78 |
|
| 79 |
-
# Check empty input
|
| 80 |
if not text or not text.strip():
|
| 81 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
# Convert to lowercase
|
| 84 |
-
|
| 85 |
|
| 86 |
# Extract words
|
| 87 |
-
words = re.findall(r
|
| 88 |
|
| 89 |
# Find positive words
|
| 90 |
positive_matches = [
|
| 91 |
word for word in words
|
| 92 |
-
if word in
|
| 93 |
]
|
| 94 |
|
| 95 |
# Find negative words
|
| 96 |
negative_matches = [
|
| 97 |
word for word in words
|
| 98 |
-
if word in
|
| 99 |
]
|
| 100 |
|
| 101 |
-
# Count
|
| 102 |
positive_count = len(positive_matches)
|
| 103 |
negative_count = len(negative_matches)
|
| 104 |
|
| 105 |
-
# ========================================
|
| 106 |
# SENTIMENT DECISION RULE
|
| 107 |
-
# ========================================
|
| 108 |
|
| 109 |
if positive_count > negative_count:
|
| 110 |
-
|
| 111 |
sentiment = "π Positive"
|
| 112 |
|
| 113 |
elif negative_count > positive_count:
|
| 114 |
-
|
| 115 |
sentiment = "π Negative"
|
| 116 |
|
| 117 |
else:
|
| 118 |
-
|
| 119 |
sentiment = "π Neutral"
|
| 120 |
|
| 121 |
-
|
| 122 |
-
# ========================================
|
| 123 |
# CREATE RESULT
|
| 124 |
-
# ========================================
|
| 125 |
|
| 126 |
-
result = f"""
|
| 127 |
-
Sentiment: {sentiment}
|
| 128 |
|
| 129 |
Positive words detected: {positive_count}
|
| 130 |
Negative words detected: {negative_count}
|
| 131 |
|
| 132 |
Positive matches:
|
| 133 |
-
{
|
| 134 |
|
| 135 |
Negative matches:
|
| 136 |
-
{
|
| 137 |
"""
|
| 138 |
|
| 139 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
|
| 142 |
-
# ============================================
|
| 143 |
-
# GRADIO
|
| 144 |
-
# ============================================
|
| 145 |
|
| 146 |
-
with gr.Blocks(
|
|
|
|
|
|
|
| 147 |
|
| 148 |
gr.Markdown(
|
| 149 |
"""
|
|
@@ -161,52 +162,73 @@ with gr.Blocks() as app:
|
|
| 161 |
π **Neutral**
|
| 162 |
|
| 163 |
---
|
| 164 |
-
|
| 165 |
-
**
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
| 167 |
"""
|
| 168 |
)
|
| 169 |
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
-
# Text input
|
| 172 |
text_input = gr.Textbox(
|
| 173 |
label="Enter your sentence",
|
| 174 |
placeholder="Example: I really love this application!",
|
| 175 |
lines=4
|
| 176 |
)
|
| 177 |
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
-
# Analyze button
|
| 180 |
analyze_button = gr.Button(
|
| 181 |
"Analyze Sentiment",
|
| 182 |
variant="primary"
|
| 183 |
)
|
| 184 |
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
-
# Output
|
| 187 |
output = gr.Textbox(
|
| 188 |
label="Analysis Result",
|
| 189 |
-
lines=10
|
|
|
|
| 190 |
)
|
| 191 |
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
-
# Button action
|
| 194 |
analyze_button.click(
|
| 195 |
fn=sentiment_analyzer,
|
| 196 |
inputs=text_input,
|
| 197 |
outputs=output
|
| 198 |
)
|
| 199 |
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
|
|
|
|
|
|
| 204 |
value="Clear"
|
| 205 |
)
|
| 206 |
|
| 207 |
|
| 208 |
-
# ============================================
|
| 209 |
-
# LAUNCH
|
| 210 |
-
# ============================================
|
|
|
|
| 211 |
if __name__ == "__main__":
|
| 212 |
-
app.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
|
| 5 |
+
# ============================================================
|
| 6 |
+
# RULE-BASED SENTIMENT WORDS
|
| 7 |
+
# ============================================================
|
| 8 |
|
| 9 |
+
POSITIVE_WORDS = {
|
| 10 |
"good",
|
| 11 |
"great",
|
| 12 |
"excellent",
|
|
|
|
| 33 |
}
|
| 34 |
|
| 35 |
|
| 36 |
+
NEGATIVE_WORDS = {
|
| 37 |
"bad",
|
| 38 |
"terrible",
|
| 39 |
"awful",
|
|
|
|
| 62 |
}
|
| 63 |
|
| 64 |
|
| 65 |
+
# ============================================================
|
| 66 |
# SENTIMENT ANALYSIS FUNCTION
|
| 67 |
+
# ============================================================
|
| 68 |
|
| 69 |
def sentiment_analyzer(text):
|
| 70 |
|
| 71 |
+
# Check for empty input
|
| 72 |
if not text or not text.strip():
|
| 73 |
+
return (
|
| 74 |
+
"β οΈ Please enter a sentence.",
|
| 75 |
+
0,
|
| 76 |
+
0,
|
| 77 |
+
"None",
|
| 78 |
+
"None"
|
| 79 |
+
)
|
| 80 |
|
| 81 |
+
# Convert sentence to lowercase
|
| 82 |
+
text = text.lower()
|
| 83 |
|
| 84 |
# Extract words
|
| 85 |
+
words = re.findall(r"\b\w+\b", text)
|
| 86 |
|
| 87 |
# Find positive words
|
| 88 |
positive_matches = [
|
| 89 |
word for word in words
|
| 90 |
+
if word in POSITIVE_WORDS
|
| 91 |
]
|
| 92 |
|
| 93 |
# Find negative words
|
| 94 |
negative_matches = [
|
| 95 |
word for word in words
|
| 96 |
+
if word in NEGATIVE_WORDS
|
| 97 |
]
|
| 98 |
|
| 99 |
+
# Count positive and negative words
|
| 100 |
positive_count = len(positive_matches)
|
| 101 |
negative_count = len(negative_matches)
|
| 102 |
|
| 103 |
+
# ========================================================
|
| 104 |
# SENTIMENT DECISION RULE
|
| 105 |
+
# ========================================================
|
| 106 |
|
| 107 |
if positive_count > negative_count:
|
|
|
|
| 108 |
sentiment = "π Positive"
|
| 109 |
|
| 110 |
elif negative_count > positive_count:
|
|
|
|
| 111 |
sentiment = "π Negative"
|
| 112 |
|
| 113 |
else:
|
|
|
|
| 114 |
sentiment = "π Neutral"
|
| 115 |
|
| 116 |
+
# ========================================================
|
|
|
|
| 117 |
# CREATE RESULT
|
| 118 |
+
# ========================================================
|
| 119 |
|
| 120 |
+
result = f"""Sentiment: {sentiment}
|
|
|
|
| 121 |
|
| 122 |
Positive words detected: {positive_count}
|
| 123 |
Negative words detected: {negative_count}
|
| 124 |
|
| 125 |
Positive matches:
|
| 126 |
+
{", ".join(positive_matches) if positive_matches else "None"}
|
| 127 |
|
| 128 |
Negative matches:
|
| 129 |
+
{", ".join(negative_matches) if negative_matches else "None"}
|
| 130 |
"""
|
| 131 |
|
| 132 |
+
return (
|
| 133 |
+
result,
|
| 134 |
+
positive_count,
|
| 135 |
+
negative_count,
|
| 136 |
+
", ".join(positive_matches) if positive_matches else "None",
|
| 137 |
+
", ".join(negative_matches) if negative_matches else "None"
|
| 138 |
+
)
|
| 139 |
|
| 140 |
|
| 141 |
+
# ============================================================
|
| 142 |
+
# GRADIO INTERFACE
|
| 143 |
+
# ============================================================
|
| 144 |
|
| 145 |
+
with gr.Blocks(
|
| 146 |
+
title="Rule-Based Sentiment Analysis"
|
| 147 |
+
) as app:
|
| 148 |
|
| 149 |
gr.Markdown(
|
| 150 |
"""
|
|
|
|
| 162 |
π **Neutral**
|
| 163 |
|
| 164 |
---
|
| 165 |
+
|
| 166 |
+
**How it works:**
|
| 167 |
+
The application checks the words in your sentence
|
| 168 |
+
against predefined positive and negative word lists.
|
| 169 |
+
|
| 170 |
+
**No AI model β’ No API β’ No Machine Learning**
|
| 171 |
"""
|
| 172 |
)
|
| 173 |
|
| 174 |
+
# ========================================================
|
| 175 |
+
# INPUT
|
| 176 |
+
# ========================================================
|
| 177 |
|
|
|
|
| 178 |
text_input = gr.Textbox(
|
| 179 |
label="Enter your sentence",
|
| 180 |
placeholder="Example: I really love this application!",
|
| 181 |
lines=4
|
| 182 |
)
|
| 183 |
|
| 184 |
+
# ========================================================
|
| 185 |
+
# ANALYZE BUTTON
|
| 186 |
+
# ========================================================
|
| 187 |
|
|
|
|
| 188 |
analyze_button = gr.Button(
|
| 189 |
"Analyze Sentiment",
|
| 190 |
variant="primary"
|
| 191 |
)
|
| 192 |
|
| 193 |
+
# ========================================================
|
| 194 |
+
# MAIN OUTPUT
|
| 195 |
+
# ========================================================
|
| 196 |
|
|
|
|
| 197 |
output = gr.Textbox(
|
| 198 |
label="Analysis Result",
|
| 199 |
+
lines=10,
|
| 200 |
+
interactive=False
|
| 201 |
)
|
| 202 |
|
| 203 |
+
# ========================================================
|
| 204 |
+
# BUTTON EVENT
|
| 205 |
+
# ========================================================
|
| 206 |
|
|
|
|
| 207 |
analyze_button.click(
|
| 208 |
fn=sentiment_analyzer,
|
| 209 |
inputs=text_input,
|
| 210 |
outputs=output
|
| 211 |
)
|
| 212 |
|
| 213 |
+
# ========================================================
|
| 214 |
+
# CLEAR BUTTON
|
| 215 |
+
# ========================================================
|
| 216 |
|
| 217 |
+
gr.ClearButton(
|
| 218 |
+
components=[
|
| 219 |
+
text_input,
|
| 220 |
+
output
|
| 221 |
+
],
|
| 222 |
value="Clear"
|
| 223 |
)
|
| 224 |
|
| 225 |
|
| 226 |
+
# ============================================================
|
| 227 |
+
# LAUNCH
|
| 228 |
+
# ============================================================
|
| 229 |
+
|
| 230 |
if __name__ == "__main__":
|
| 231 |
+
app.launch(
|
| 232 |
+
server_name="0.0.0.0",
|
| 233 |
+
server_port=7860
|
| 234 |
+
)
|