|
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 |
|
|
|
|
|
polarity = round(sentiment.polarity, 2) |
|
subjectivity = round(sentiment.subjectivity, 2) |
|
|
|
|
|
if polarity > 0: |
|
assessment = "positive" |
|
elif polarity < 0: |
|
assessment = "negative" |
|
else: |
|
assessment = "neutral" |
|
|
|
return { |
|
"polarity": polarity, |
|
"subjectivity": subjectivity, |
|
"assessment": assessment |
|
} |
|
|
|
|
|
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." |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch(mcp_server=True) |
|
|