mcp-sentiment / app.py
Kiran Mohan
feat: add more tools to the gradio mcp
8fcd68e
import gradio as gr
from textblob import TextBlob
def sentiment_analysis(text: str) -> dict:
"""
Analyze the sentiment of the given text.
Args:
text (str): The text to analyze
Returns:
dict: A dictionary containing polarity, subjectivity, and assessment
"""
blob = TextBlob(text)
sentiment = blob.sentiment
return {
"polarity": round(sentiment.polarity, 2), # -1 (negative) to 1 (positive)
"subjectivity": round(sentiment.subjectivity, 2), # 0 (objective) to 1 (subjective)
"assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"
}
def letter_count_in_text(letter: str, text: str) -> int:
"""
Count the number of times a specific letter appears in the given text.
Args:
letter (str): The letter to count
text (str): The text to search in
Returns:
int: The number of times the letter appears in the text
"""
return text.count(letter)
# Create the Gradio interface using Blocks
with gr.Blocks(title="Text Analysis Tools") as demo:
gr.Markdown("# Text Analysis Tools")
gr.Markdown("Analyze text sentiment and count letter occurrences")
with gr.Tab("Sentiment Analysis"):
sentiment_input = gr.Textbox(placeholder="Enter text to analyze...")
sentiment_output = gr.JSON()
sentiment_button = gr.Button("Analyze Sentiment")
sentiment_button.click(fn=sentiment_analysis, inputs=sentiment_input, outputs=sentiment_output)
with gr.Tab("Letter Counter"):
with gr.Row():
letter_input = gr.Textbox(placeholder="Enter letter to count...")
text_input = gr.Textbox(placeholder="Enter text to count letters in...")
letter_output = gr.Number()
letter_button = gr.Button("Count Letters")
letter_button.click(fn=letter_count_in_text, inputs=[letter_input, text_input], outputs=letter_output)
# Launch the interface and MCP server
if __name__ == "__main__":
demo.launch(mcp_server=True)