Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import re
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
# ============================================================
|
|
@@ -63,61 +64,94 @@ NEGATIVE_WORDS = {
|
|
| 63 |
|
| 64 |
|
| 65 |
# ============================================================
|
| 66 |
-
# SENTIMENT
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
# ============================================================
|
| 68 |
|
|
|
|
| 69 |
def sentiment_analyzer(text):
|
| 70 |
|
| 71 |
-
#
|
|
|
|
|
|
|
|
|
|
| 72 |
if not text or not text.strip():
|
| 73 |
-
return
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 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
|
|
|
|
| 90 |
if word in POSITIVE_WORDS
|
| 91 |
]
|
| 92 |
|
|
|
|
|
|
|
| 93 |
# Find negative words
|
|
|
|
|
|
|
| 94 |
negative_matches = [
|
| 95 |
-
word
|
|
|
|
| 96 |
if word in NEGATIVE_WORDS
|
| 97 |
]
|
| 98 |
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
positive_count = len(positive_matches)
|
| 101 |
negative_count = len(negative_matches)
|
| 102 |
|
| 103 |
-
|
| 104 |
-
#
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
Positive words detected: {positive_count}
|
| 123 |
Negative words detected: {negative_count}
|
|
@@ -129,13 +163,7 @@ 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 |
# ============================================================
|
|
@@ -146,6 +174,10 @@ with gr.Blocks(
|
|
| 146 |
title="Rule-Based Sentiment Analysis"
|
| 147 |
) as app:
|
| 148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
gr.Markdown(
|
| 150 |
"""
|
| 151 |
# π Rule-Based Sentiment Analysis
|
|
@@ -163,7 +195,8 @@ with gr.Blocks(
|
|
| 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 |
|
|
@@ -171,9 +204,10 @@ with gr.Blocks(
|
|
| 171 |
"""
|
| 172 |
)
|
| 173 |
|
| 174 |
-
|
| 175 |
-
#
|
| 176 |
-
#
|
|
|
|
| 177 |
|
| 178 |
text_input = gr.Textbox(
|
| 179 |
label="Enter your sentence",
|
|
@@ -181,18 +215,20 @@ with gr.Blocks(
|
|
| 181 |
lines=4
|
| 182 |
)
|
| 183 |
|
| 184 |
-
|
| 185 |
-
#
|
| 186 |
-
#
|
|
|
|
| 187 |
|
| 188 |
analyze_button = gr.Button(
|
| 189 |
"Analyze Sentiment",
|
| 190 |
variant="primary"
|
| 191 |
)
|
| 192 |
|
| 193 |
-
|
| 194 |
-
#
|
| 195 |
-
#
|
|
|
|
| 196 |
|
| 197 |
output = gr.Textbox(
|
| 198 |
label="Analysis Result",
|
|
@@ -200,9 +236,10 @@ with gr.Blocks(
|
|
| 200 |
interactive=False
|
| 201 |
)
|
| 202 |
|
| 203 |
-
|
| 204 |
-
#
|
| 205 |
-
#
|
|
|
|
| 206 |
|
| 207 |
analyze_button.click(
|
| 208 |
fn=sentiment_analyzer,
|
|
@@ -210,9 +247,10 @@ with gr.Blocks(
|
|
| 210 |
outputs=output
|
| 211 |
)
|
| 212 |
|
| 213 |
-
|
| 214 |
-
#
|
| 215 |
-
#
|
|
|
|
| 216 |
|
| 217 |
gr.ClearButton(
|
| 218 |
components=[
|
|
@@ -228,7 +266,4 @@ with gr.Blocks(
|
|
| 228 |
# ============================================================
|
| 229 |
|
| 230 |
if __name__ == "__main__":
|
| 231 |
-
app.launch(
|
| 232 |
-
server_name="0.0.0.0",
|
| 233 |
-
server_port=7860
|
| 234 |
-
)
|
|
|
|
| 1 |
import re
|
| 2 |
import gradio as gr
|
| 3 |
+
import spaces
|
| 4 |
|
| 5 |
|
| 6 |
# ============================================================
|
|
|
|
| 64 |
|
| 65 |
|
| 66 |
# ============================================================
|
| 67 |
+
# SENTIMENT ANALYZER
|
| 68 |
+
# ============================================================
|
| 69 |
+
# ZeroGPU requires a @spaces.GPU function to be registered
|
| 70 |
+
# with the Gradio event system.
|
| 71 |
+
#
|
| 72 |
+
# This application does NOT actually perform GPU computation.
|
| 73 |
+
# The decorator is only required because this Space is using
|
| 74 |
+
# ZeroGPU hardware.
|
| 75 |
# ============================================================
|
| 76 |
|
| 77 |
+
@spaces.GPU(duration=1)
|
| 78 |
def sentiment_analyzer(text):
|
| 79 |
|
| 80 |
+
# --------------------------------------------------------
|
| 81 |
+
# Check empty input
|
| 82 |
+
# --------------------------------------------------------
|
| 83 |
+
|
| 84 |
if not text or not text.strip():
|
| 85 |
+
return "β οΈ Please enter a sentence."
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
# --------------------------------------------------------
|
| 89 |
+
# Convert text to lowercase
|
| 90 |
+
# --------------------------------------------------------
|
| 91 |
+
|
|
|
|
|
|
|
| 92 |
text = text.lower()
|
| 93 |
|
| 94 |
+
|
| 95 |
+
# --------------------------------------------------------
|
| 96 |
# Extract words
|
| 97 |
+
# --------------------------------------------------------
|
| 98 |
+
|
| 99 |
words = re.findall(r"\b\w+\b", text)
|
| 100 |
|
| 101 |
+
|
| 102 |
+
# --------------------------------------------------------
|
| 103 |
# Find positive words
|
| 104 |
+
# --------------------------------------------------------
|
| 105 |
+
|
| 106 |
positive_matches = [
|
| 107 |
+
word
|
| 108 |
+
for word in words
|
| 109 |
if word in POSITIVE_WORDS
|
| 110 |
]
|
| 111 |
|
| 112 |
+
|
| 113 |
+
# --------------------------------------------------------
|
| 114 |
# Find negative words
|
| 115 |
+
# --------------------------------------------------------
|
| 116 |
+
|
| 117 |
negative_matches = [
|
| 118 |
+
word
|
| 119 |
+
for word in words
|
| 120 |
if word in NEGATIVE_WORDS
|
| 121 |
]
|
| 122 |
|
| 123 |
+
|
| 124 |
+
# --------------------------------------------------------
|
| 125 |
+
# Count sentiment words
|
| 126 |
+
# --------------------------------------------------------
|
| 127 |
+
|
| 128 |
positive_count = len(positive_matches)
|
| 129 |
negative_count = len(negative_matches)
|
| 130 |
|
| 131 |
+
|
| 132 |
+
# --------------------------------------------------------
|
| 133 |
+
# Sentiment decision
|
| 134 |
+
# --------------------------------------------------------
|
| 135 |
|
| 136 |
if positive_count > negative_count:
|
| 137 |
+
|
| 138 |
sentiment = "π Positive"
|
| 139 |
|
| 140 |
elif negative_count > positive_count:
|
| 141 |
+
|
| 142 |
sentiment = "π Negative"
|
| 143 |
|
| 144 |
else:
|
| 145 |
+
|
| 146 |
sentiment = "π Neutral"
|
| 147 |
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
+
# --------------------------------------------------------
|
| 150 |
+
# Create result
|
| 151 |
+
# --------------------------------------------------------
|
| 152 |
+
|
| 153 |
+
result = f"""
|
| 154 |
+
Sentiment: {sentiment}
|
| 155 |
|
| 156 |
Positive words detected: {positive_count}
|
| 157 |
Negative words detected: {negative_count}
|
|
|
|
| 163 |
{", ".join(negative_matches) if negative_matches else "None"}
|
| 164 |
"""
|
| 165 |
|
| 166 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
|
| 169 |
# ============================================================
|
|
|
|
| 174 |
title="Rule-Based Sentiment Analysis"
|
| 175 |
) as app:
|
| 176 |
|
| 177 |
+
# --------------------------------------------------------
|
| 178 |
+
# Header
|
| 179 |
+
# --------------------------------------------------------
|
| 180 |
+
|
| 181 |
gr.Markdown(
|
| 182 |
"""
|
| 183 |
# π Rule-Based Sentiment Analysis
|
|
|
|
| 195 |
|
| 196 |
---
|
| 197 |
|
| 198 |
+
**How it works**
|
| 199 |
+
|
| 200 |
The application checks the words in your sentence
|
| 201 |
against predefined positive and negative word lists.
|
| 202 |
|
|
|
|
| 204 |
"""
|
| 205 |
)
|
| 206 |
|
| 207 |
+
|
| 208 |
+
# --------------------------------------------------------
|
| 209 |
+
# Text input
|
| 210 |
+
# --------------------------------------------------------
|
| 211 |
|
| 212 |
text_input = gr.Textbox(
|
| 213 |
label="Enter your sentence",
|
|
|
|
| 215 |
lines=4
|
| 216 |
)
|
| 217 |
|
| 218 |
+
|
| 219 |
+
# --------------------------------------------------------
|
| 220 |
+
# Analyze button
|
| 221 |
+
# --------------------------------------------------------
|
| 222 |
|
| 223 |
analyze_button = gr.Button(
|
| 224 |
"Analyze Sentiment",
|
| 225 |
variant="primary"
|
| 226 |
)
|
| 227 |
|
| 228 |
+
|
| 229 |
+
# --------------------------------------------------------
|
| 230 |
+
# Output
|
| 231 |
+
# --------------------------------------------------------
|
| 232 |
|
| 233 |
output = gr.Textbox(
|
| 234 |
label="Analysis Result",
|
|
|
|
| 236 |
interactive=False
|
| 237 |
)
|
| 238 |
|
| 239 |
+
|
| 240 |
+
# --------------------------------------------------------
|
| 241 |
+
# Button event
|
| 242 |
+
# --------------------------------------------------------
|
| 243 |
|
| 244 |
analyze_button.click(
|
| 245 |
fn=sentiment_analyzer,
|
|
|
|
| 247 |
outputs=output
|
| 248 |
)
|
| 249 |
|
| 250 |
+
|
| 251 |
+
# --------------------------------------------------------
|
| 252 |
+
# Clear button
|
| 253 |
+
# --------------------------------------------------------
|
| 254 |
|
| 255 |
gr.ClearButton(
|
| 256 |
components=[
|
|
|
|
| 266 |
# ============================================================
|
| 267 |
|
| 268 |
if __name__ == "__main__":
|
| 269 |
+
app.launch()
|
|
|
|
|
|
|
|
|