mcp-sentiment / app.py
Saurabh Zinjad
Add addition functionality and integrate into Gradio interface
1db7322
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 add_two_number(a: int, b: int) -> int:
"""
Add two numbers.
Args:
a (int): First number
b (int): Second number
Returns:
int: The sum of the two numbers
"""
return a + b
# Create the Gradio interface
demo = gr.Interface(
fn=sentiment_analysis,
inputs=gr.Textbox(placeholder="Enter text to analyze..."),
outputs=gr.JSON(),
title="Text Sentiment Analysis",
description="Analyze the sentiment of text using TextBlob",
)
demo2 = gr.Interface(
fn=add_two_number,
inputs=[gr.Number(), gr.Number()],
outputs=gr.Number(),
title="Add Two Numbers",
description="Add two numbers together",
)
app = gr.TabbedInterface(
interface_list=[demo, demo2], tab_names=["Function 1", "Function 2"]
)
# Launch the interface and MCP server
if __name__ == "__main__":
app.launch(mcp_server=True)