RamezCh's picture
Update app.py
cb57e70 verified
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 prime_factorization(n: int) -> dict:
"""
Compute the prime factorization of a number.
Args:
n (int): Positive integer
Returns:
dict: Prime factors and their exponents (with string keys for JSON compatibility)
"""
if n <= 0:
return {"error": "Please enter a positive integer."}
i = 2
factors = {}
while i * i <= n:
while n % i == 0:
factors[i] = factors.get(i, 0) + 1
n //= i
i += 1
if n > 1:
factors[n] = 1
# Convert integer keys to strings
str_factors = {str(k): v for k, v in factors.items()}
return {"factors": str_factors}
# Define Gradio interfaces for each function
sentiment_interface = 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"
)
factorization_interface = gr.Interface(
fn=prime_factorization,
inputs=gr.Number(value=1, label="Number", precision=0),
outputs=gr.JSON(),
title="Prime Factorization",
description="Compute the prime factorization of a number"
)
# Combine into a tabbed interface
demo = gr.TabbedInterface(
interface_list=[sentiment_interface, factorization_interface],
tab_names=["Sentiment Analysis", "Prime Factorization"]
)
# Launch the interface and MCP server
if __name__ == "__main__":
demo.launch(mcp_server=True)