Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,44 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
|
| 4 |
-
|
|
|
|
| 5 |
"""
|
| 6 |
-
|
| 7 |
|
| 8 |
Args:
|
| 9 |
-
|
|
|
|
| 10 |
Returns:
|
| 11 |
-
str:
|
| 12 |
"""
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
demo = gr.Interface(
|
| 26 |
-
fn=
|
| 27 |
-
inputs=gr.Textbox(label="
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
)
|
| 32 |
|
|
|
|
| 33 |
if __name__ == "__main__":
|
| 34 |
-
demo.launch(mcp_server=True)
|
|
|
|
| 1 |
+
import json
|
| 2 |
import gradio as gr
|
| 3 |
+
from textblob import TextBlob
|
| 4 |
|
| 5 |
+
|
| 6 |
+
def sentiment_analysis(text: str) -> str:
|
| 7 |
"""
|
| 8 |
+
Analyze the sentiment of the given text.
|
| 9 |
|
| 10 |
Args:
|
| 11 |
+
text(str): The text to analyze the
|
| 12 |
+
|
| 13 |
Returns:
|
| 14 |
+
str: A JSON string containing polarity, subjectivity, and assessment
|
| 15 |
"""
|
| 16 |
+
|
| 17 |
+
blob = TextBlob(text)
|
| 18 |
+
sentiment = blob.sentiment
|
| 19 |
+
|
| 20 |
+
result = {
|
| 21 |
+
"polarity": round(sentiment.polarity, 2), # -1 negative, 1 positive
|
| 22 |
+
# 0 objective, 1 subjective
|
| 23 |
+
"subjectivity": round(sentiment.subjectivity, 2),
|
| 24 |
+
"assessment": "Positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
return json.dumps(result)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# creating the Gradio interface
|
| 31 |
demo = gr.Interface(
|
| 32 |
+
fn=sentiment_analysis,
|
| 33 |
+
inputs=gr.Textbox(label="Enter text for sentiment analysis",
|
| 34 |
+
placeholder="Type here..."),
|
| 35 |
+
outputs=gr.Textbox(label="Sentiment Analysis Result",
|
| 36 |
+
placeholder="Result will be displayed here..."),
|
| 37 |
+
title="Sentiment Analysis",
|
| 38 |
+
description="This app analyzes the sentiment of the input text using TextBlob and returns the polarity, subjectivity, and assessment.",
|
| 39 |
+
theme="default"
|
| 40 |
)
|
| 41 |
|
| 42 |
+
# launching the Gradio app
|
| 43 |
if __name__ == "__main__":
|
| 44 |
+
demo.launch(mcp_server=True)
|