MCP-Server_SentimentAnalysis / streamlit_app.py
AllIllusion's picture
Add SentimentAnalysis for both Gradio and Docker streamlit
6e749b0
#########################################################################
# Copyright (C) #
# 2025-June Sen Li (Sen.Li.Sprout@gmail.com) #
# Permission given to modify the code only for Non-Profit Research #
# as long as you keep this declaration at the top #
#########################################################################
import gradio as gr
import textblob
import os
def func_SentimentAnalysis(str_tgtText_2_Analyze: str) -> dict:
""" Analyze the sentiment of the given text.
Args:
str_tgtText_2_Analyze (str): The text to analyze
Returns:
dict_SentimentAnalysis: { polarity [-1, 1],
subjectivity objective [0, 1] subjective
assessment ["positive", "neutral", "negative"]
}
"""
textBlob_tgtText = textblob.TextBlob(str_tgtText_2_Analyze)
sentiment_tgtText = textBlob_tgtText.sentiment
dict_SentimentAnalysis = {
"polarity": round(sentiment_tgtText.polarity, 2),
"subjectivity": round(sentiment_tgtText.subjectivity, 2),
"assessment": "positive" if sentiment_tgtText.polarity > 0 \
else "negative" if sentiment_tgtText.polarity < 0 else "neutral"
}
return dict_SentimentAnalysis
# Launch the interface and MCP server
if __name__ == "__main__":
print(f"os.getcwd() = {os.getcwd()}")
os.system(f"echo ls -al {os.getcwd()} && ls -al {os.getcwd()}")
os.system(f"echo ls -al /app/../: && ls -al /app/../")
# os.system(f"echo ls -al /.gradio/: && ls -al /.gradio/")
os.system(f"echo ls -al /app/: && ls -al /app/")
os.system(f"echo ls -al /home/: && ls -al /home/")
os.system(f"echo ls -al /app/src/: && ls -al /app/src/")
# Create the Gradio interface
grInterface_SentimentAnalysis = gr.Interface(
fn = func_SentimentAnalysis,
inputs = gr.Textbox(placeholder="Enter text to analyze..."),
outputs = gr.JSON(),
title = "Text Sentiment Analysis",
description = "Analyze the sentiment of text using TextBlob"
)
# MCP Schema: Visit http://localhost:7860/gradio_api/mcp/schema
# MCP Server:
# Setting mcp_server=True enables the MCP server, enable using the environment variable:
# export GRADIO_MCP_SERVER=True # http://localhost:7860/gradio_api/mcp/sse
grInterface_SentimentAnalysis.launch(share=True) # mcp_server=True, share=True)
os.system(f"echo ls -al /app/flagged/: && ls -al /app/flagged/")
# os.system(f"echo ls -al /.gradio/flagged/: && ls -al /.gradio/flagged/")