Spaces:
Running
Running
| # Hugging Face spaces needs an app.py file to build the space. So the name of the python file has to be app.py | |
| import json | |
| import gradio as gr | |
| from textblob import TextBlob | |
| def sentiment_analysis(text:str) -> str: | |
| """ | |
| Analyze the sentiment of a given text. | |
| Args: | |
| text (str): The text to analyze | |
| Returns: | |
| str: A JSON string containing polarity, subjectivity, and assessment | |
| """ | |
| blob = TextBlob(text) | |
| sentiment = blob.sentiment | |
| result = { | |
| "polarity": round(sentiment.polarity, 2), | |
| "subjectivity": round(sentiment.subjectivity, 2), | |
| "assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral" | |
| } | |
| return json.dumps(result) | |
| # Create the Gradio interface | |
| demo = gr.Interface( | |
| fn=sentiment_analysis, | |
| inputs=gr.Textbox(placeholder="Enter text to analyze...."), | |
| outputs=gr.Textbox(), | |
| title="Text Sentiment Analysis", | |
| description="Analyze the sentiment of text using TextBlob" | |
| ) | |
| # Launch the interface and MCP server | |
| if __name__=="__main__": | |
| demo.launch(mcp_server=True) | |
| # Let’s break down the key components: | |
| # Function Definition: | |
| # The sentiment_analysis function takes a text input and returns a dictionary | |
| # It uses TextBlob to analyze the sentiment | |
| # The docstring is crucial as it helps Gradio generate the MCP tool schema | |
| # Type hints (str and dict) help define the input/output schema | |
| # Gradio Interface:c | |
| # gr.Interface creates both the web UI and MCP server | |
| # The function is exposed as an MCP tool automatically | |
| # Input and output components define the tool’s schema | |
| # The JSON output component ensures proper serialization | |
| # MCP Server: | |
| # Setting mcp_server=True enables the MCP server | |
| # The server will be available at http://localhost:7860/gradio_api/mcp/sse | |
| # You can also enable it using the environment variable: | |
| # Copied | |
| # export GRADIO_MCP_SERVER=True | |
| # NOTE | |
| # SSE Support: | |
| # Some MCP clients don’t support SSE-based MCP Servers | |
| # In those cases, use mcp-remote: | |
| # Copied | |
| # { | |
| # "mcpServers": { | |
| # "gradio": { | |
| # "command": "npx", | |
| # "args": [ | |
| # "mcp-remote", | |
| # "http://localhost:7860/gradio_api/mcp/sse" | |
| # ] | |
| # } | |
| # } | |
| # } | |