File size: 2,881 Bytes
6e749b0 09f720a 6e749b0 80fea76 570653a 6e749b0 f5f26fa 6e749b0 09f720a 6e749b0 |
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
#########################################################################
# 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 /: && ls -al /")
os.system(f"echo ls -al /app/: && ls -al /app/")
os.system(f"echo ls -al /home/: && ls -al /home/")
# Create the Gradio interface, as the MCP-Server
grInterface_SentimentAnalysis__MCP_Server = gr.Interface(
fn = func_SentimentAnalysis,
inputs = gr.Textbox(placeholder="Enter text to analyze..."),
outputs = gr.JSON(),
title = "MCP-Server: Text Sentiment Analysis",
description = "Analyze the sentiment of text using TextBlob. This is an MCP-Server used by the MCP-Client https://huggingface.co/spaces/AllIllusion/MCP-Client_Qwen25-3B_Tool-SentimentAnalysis"
)
# 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
# https://allillusion-mcp-server-sentimentanalysis.hf.space/gradio_api/mcp/sse
grInterface_SentimentAnalysis__MCP_Server.launch(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/")
|