sentiment / app.py
Aneesha Manke
initialising
a126335
import gradio as gr
from textblob import TextBlob
def sentiment_analysis(text: str) -> dict:
"""
Analyze the sentiment of the given text.
Args:
text (str): The input text to analyze.
Returns:
dict: Sentiment metrics including polarity, subjectivity, and overall assessment.
"""
blob = TextBlob(text)
sentiment = blob.sentiment
# Round for cleaner output
polarity = round(sentiment.polarity, 2)
subjectivity = round(sentiment.subjectivity, 2)
# Classify overall sentiment
if polarity > 0:
assessment = "positive"
elif polarity < 0:
assessment = "negative"
else:
assessment = "neutral"
return {
"polarity": polarity,
"subjectivity": subjectivity,
"assessment": assessment
}
# Create the Gradio interface
demo = gr.Interface(
fn=sentiment_analysis,
inputs=gr.Textbox(lines=4, placeholder="Enter text to analyze..."),
outputs=gr.JSON(),
title="Text Sentiment Analysis",
description="Analyze the sentiment of a given text using TextBlob. MCP-compatible endpoint."
)
# Launch both web UI and MCP server
if __name__ == "__main__":
demo.launch(mcp_server=True)