Spaces:
Running
Running
theo00
commited on
Commit
·
58a1e28
1
Parent(s):
3d91187
Add app file
Browse files- app.py +42 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 a given text.
|
8 |
+
Args:
|
9 |
+
text (str): The text to analyze.
|
10 |
+
Returns:
|
11 |
+
str: A JSON string containing polarity, subjectivity and assessment
|
12 |
+
"""
|
13 |
+
blob = TextBlob(text)
|
14 |
+
sentiment = blob.sentiment
|
15 |
+
|
16 |
+
result = {
|
17 |
+
"polarity": round(sentiment.polarity, 2), # -1 (negative) to 1 (positive)
|
18 |
+
"subjectivity": round(sentiment.subjectivity, 2), # 0 (objective) to 1 (subjective)
|
19 |
+
"assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"
|
20 |
+
}
|
21 |
+
|
22 |
+
return json.dumps(result)
|
23 |
+
|
24 |
+
|
25 |
+
# Create the Gradio interface
|
26 |
+
demo = gr.Interface(
|
27 |
+
fn=sentiment_analysis,
|
28 |
+
inputs=gr.Textbox(label="Enter your text to analyze..."),
|
29 |
+
outputs=gr.Textbox(), #Changed from gr.JSON() to gr.Textbox()
|
30 |
+
title="Text Sentiment Analysis",
|
31 |
+
description="Analyze the sentiment of text using TextBlob",
|
32 |
+
examples=[
|
33 |
+
["I love this product!"],
|
34 |
+
["This is the worst experience I've ever had."],
|
35 |
+
["I'm not sure about this."]
|
36 |
+
]
|
37 |
+
)
|
38 |
+
|
39 |
+
# Launch the Gradio interface and MCP server
|
40 |
+
|
41 |
+
if __name__ == "__main__":
|
42 |
+
demo.launch(mcp_server=True)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio[mcp]
|
2 |
+
textblob
|