File size: 1,341 Bytes
8a0df75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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