Spaces:
Sleeping
Sleeping
File size: 2,165 Bytes
c02dbf2 cb57e70 c02dbf2 cb57e70 c02dbf2 |
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
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) |