Spaces:
Running
Running
import gradio as gr | |
from textblob import TextBlob | |
def sentiment_analysis(text: str) -> dict: # Type hints (str and dict) help define the input/output schema | |
# The docstring is crucial as it helps Gradio generate the MCP tool schema | |
# This helps Gradio generate accurate MCP tool schemas | |
""" | |
Analyze the sentiment of the give text.txt | |
Args: | |
text (str): The text to analyze | |
Returns: | |
dict: A dictionary contains polarity, subjectivity, and assessment | |
""" | |
blob = TextBlob(text) | |
sentiment = blob.sentiment | |
return { | |
"polarity": round(sentiment.polarity, 2), | |
"subjectivity": round(sentiment.subjectivity, 2), | |
"assessment": "positivs" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral" # if elif else | |
} | |
# gradio interface | |
demo = gr.Interface( # gr.interafce creaets both the web UI and MCP server | |
fn=sentiment_analysis, # function as mcp tool automatically | |
inputs=gr.Textbox(placeholder="Enter a text to analyze"), # define the tools schema | |
outputs=gr.JSON(), # defin the tools schema, JSON output component ensure proper serialization. | |
title="Sentiment Analysis", | |
description="Text sentiemnt with Text Blob" | |
) | |
if __name__ == "__main__": | |
demo.launch(mcp_server=True) # enables the mcp server |