File size: 1,213 Bytes
a126335 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
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)
|