File size: 2,073 Bytes
d4a9503
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fcd68e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4a9503
 
 
 
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
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)