skeletonbear/mcp-sentiment
0
1import json2from re import S3import gradio as gr4from textblob import TextBlob5 6def sentiment_analysis(text:str) -> str:7 """8 Analyze the sentiment of the given text.9 10 Args:11 text (str): The text to analyze12 13 Returns:14 str: A JSON string containing polarity, subjectivity, and assessment15 """16 blob = TextBlob(text)17 sentiment = blob.sentiment18 19 result = {20 "polarity": round(sentiment.polarity, 2), # -1 (negative) to 1 (positive)21 "subjectivity":round(sentiment.subjectivity, 2), # 0 (objective) to 1 (subjective)22 "assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"23 }24 25 return json.dumps(result)26 27 28# Create the Gradio interface29demo = gr.Interface(30 fn=sentiment_analysis,31 inputs=gr.Textbox(lines=3, placeholder="Enter your text here..."),32 outputs=gr.Textbox(),33 title="Text Sentiment Analysis",34 description="Analyze the sentiment of text using TextBlob",35)36 37if __name__ == "__main__":38 demo.launch(mcp_server=True)39 