Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,37 @@
|
|
| 1 |
-
import json
|
| 2 |
import gradio as gr
|
| 3 |
from textblob import TextBlob
|
| 4 |
|
| 5 |
-
def
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
Args:
|
| 10 |
-
text (str): The text to analyze
|
| 11 |
-
|
| 12 |
-
Returns:
|
| 13 |
-
str: A JSON string containing polarity, subjectivity, and assessment
|
| 14 |
-
"""
|
| 15 |
-
blob = TextBlob(text)
|
| 16 |
-
sentiment = blob.sentiment
|
| 17 |
-
|
| 18 |
-
result = {
|
| 19 |
-
"polarity": round(sentiment.polarity, 2), # -1 (negative) to 1 (positive)
|
| 20 |
-
"subjectivity": round(sentiment.subjectivity, 2), # 0 (objective) to 1 (subjective)
|
| 21 |
-
"assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"
|
| 22 |
-
}
|
| 23 |
-
|
| 24 |
-
return json.dumps(result)
|
| 25 |
-
|
| 26 |
-
# Create the Gradio interface
|
| 27 |
demo = gr.Interface(
|
| 28 |
-
fn=
|
| 29 |
-
inputs=gr.Textbox(placeholder="Enter text to analyze..."),
|
| 30 |
-
outputs=gr.Textbox(),
|
| 31 |
title="Text Sentiment Analysis",
|
| 32 |
description="Analyze the sentiment of text using TextBlob"
|
| 33 |
)
|
| 34 |
|
| 35 |
-
# Launch the interface and MCP server
|
| 36 |
if __name__ == "__main__":
|
| 37 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from textblob import TextBlob
|
| 3 |
|
| 4 |
+
def analyze_sentiment(text):
|
| 5 |
+
try:
|
| 6 |
+
if not text or not text.strip():
|
| 7 |
+
return "Please enter some text to analyze"
|
| 8 |
+
|
| 9 |
+
blob = TextBlob(text)
|
| 10 |
+
polarity = blob.sentiment.polarity
|
| 11 |
+
|
| 12 |
+
if polarity > 0.1:
|
| 13 |
+
return f"✅ Positive sentiment (score: {polarity:.2f})"
|
| 14 |
+
elif polarity < -0.1:
|
| 15 |
+
return f"❌ Negative sentiment (score: {polarity:.2f})"
|
| 16 |
+
else:
|
| 17 |
+
return f"😐 Neutral sentiment (score: {polarity:.2f})"
|
| 18 |
+
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return f"Error: {str(e)}"
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
demo = gr.Interface(
|
| 23 |
+
fn=analyze_sentiment,
|
| 24 |
+
inputs=gr.Textbox(label="text", placeholder="Enter text to analyze..."),
|
| 25 |
+
outputs=gr.Textbox(label="output"),
|
| 26 |
title="Text Sentiment Analysis",
|
| 27 |
description="Analyze the sentiment of text using TextBlob"
|
| 28 |
)
|
| 29 |
|
|
|
|
| 30 |
if __name__ == "__main__":
|
| 31 |
+
demo.launch(
|
| 32 |
+
server_name="0.0.0.0",
|
| 33 |
+
server_port=7860,
|
| 34 |
+
share=False,
|
| 35 |
+
debug=False,
|
| 36 |
+
show_error=True
|
| 37 |
+
)
|