Spaces:
Sleeping
Sleeping
Commit
·
775165a
0
Parent(s):
Initial commit
Browse files- app.py +40 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import gradio as gr
|
3 |
+
from textblob import TextBlob
|
4 |
+
|
5 |
+
def sentiment_analysis(text:str)->str:
|
6 |
+
"""
|
7 |
+
Analyze the sentiment of the given text.
|
8 |
+
|
9 |
+
Arags :
|
10 |
+
Text (str): the text to analyze
|
11 |
+
|
12 |
+
Returns:
|
13 |
+
str: A JSON string containing polarity, subjectivity, and asssesment
|
14 |
+
"""
|
15 |
+
|
16 |
+
blob = TextBlob(text)
|
17 |
+
sentiment = blob.sentiment # type: ignore
|
18 |
+
|
19 |
+
result = { # type: ignore
|
20 |
+
"polarity": round(sentiment.polarity, 2), # -1 (negative) to 1 (positive) # type: ignore
|
21 |
+
"subjectivity": round(sentiment.subjectivity, 2), # 0 (objective) to 1 (subjective) # type: ignore
|
22 |
+
"assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral" # type: ignore
|
23 |
+
}
|
24 |
+
|
25 |
+
return json.dumps(result)
|
26 |
+
|
27 |
+
|
28 |
+
|
29 |
+
demo = gr.Interface(
|
30 |
+
fn=sentiment_analysis,
|
31 |
+
inputs=gr.Textbox(placeholder="Enter text to analyze..."),
|
32 |
+
outputs=gr.Textbox(), # Changed from gr.JSON() to gr.Textbox()
|
33 |
+
title="Text Sentiment Analysis",
|
34 |
+
description="Analyze the sentiment of text using TextBlob"
|
35 |
+
)
|
36 |
+
|
37 |
+
|
38 |
+
# Launch the interface and MCP server
|
39 |
+
if __name__ == "__main__":
|
40 |
+
demo.launch(mcp_server=True)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio[mcp]
|
2 |
+
textblob
|