Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,33 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from textblob import TextBlob
|
3 |
-
|
4 |
-
def sentiment_analysis(text: str) -> dict:
|
5 |
-
"""
|
6 |
-
Analyze the sentiment of a given text.
|
7 |
-
|
8 |
-
Args:
|
9 |
-
text (str): The input text to analyze.
|
10 |
-
|
11 |
-
Returns:
|
12 |
-
dict: A dictionary containing the sentiment polarity, subjectivity scores and assessment.
|
13 |
-
"""
|
14 |
-
blob = TextBlob(text)
|
15 |
-
sentiment = blob.sentiment
|
16 |
-
return {
|
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 |
-
# Gradio Interface
|
23 |
-
|
24 |
-
fn=sentiment_analysis,
|
25 |
-
inputs=gr.Textbox(placeholder="Enter text to analyze..."),
|
26 |
-
outputs=gr.JSON(),
|
27 |
-
title="Text Sentiment Analysis",
|
28 |
-
description="Analyze the sentiment of a given text using TextBlob.",
|
29 |
-
)
|
30 |
-
|
31 |
-
# Launch the interface and MCP server
|
32 |
-
if __name__ == "__main__":
|
33 |
-
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from textblob import TextBlob
|
3 |
+
|
4 |
+
def sentiment_analysis(text: str) -> dict:
|
5 |
+
"""
|
6 |
+
Analyze the sentiment of a given text.
|
7 |
+
|
8 |
+
Args:
|
9 |
+
text (str): The input text to analyze.
|
10 |
+
|
11 |
+
Returns:
|
12 |
+
dict: A dictionary containing the sentiment polarity, subjectivity scores and assessment.
|
13 |
+
"""
|
14 |
+
blob = TextBlob(text)
|
15 |
+
sentiment = blob.sentiment
|
16 |
+
return {
|
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 |
+
# Gradio Interface
|
23 |
+
demo = gr.Interface(
|
24 |
+
fn=sentiment_analysis,
|
25 |
+
inputs=gr.Textbox(placeholder="Enter text to analyze..."),
|
26 |
+
outputs=gr.JSON(),
|
27 |
+
title="Text Sentiment Analysis",
|
28 |
+
description="Analyze the sentiment of a given text using TextBlob.",
|
29 |
+
)
|
30 |
+
|
31 |
+
# Launch the interface and MCP server
|
32 |
+
if __name__ == "__main__":
|
33 |
+
demo.launch(mcp_server=True)
|