File size: 3,316 Bytes
8bb99c4
d0ce3d8
8bb99c4
0339f03
d2e8e3d
 
 
 
 
 
 
ff2f8a3
 
 
 
 
8bb99c4
 
d0ce3d8
3af69cf
 
b502aea
 
3af69cf
b502aea
 
 
3af69cf
 
 
 
 
b502aea
3af69cf
 
 
 
d0ce3d8
 
 
 
 
 
 
87ab8f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1148bce
 
 
 
87ab8f9
d0ce3d8
4adaee1
 
 
 
 
 
 
 
 
 
87ab8f9
 
 
 
 
 
 
 
 
1148bce
87ab8f9
 
 
 
 
 
07e4234
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import gradio as gr
import requests

def greet(name: str) -> str:
    """
    Greets the user with a personalized hello message.
    Args:
        name (str): The name of the user to greet.
    Returns:
        str: A greeting message addressed to the specified user.
    """
    if not name:
        return "Hello stranger!!"
    name = name.strip()
    if len(name) > 50:
        return "Name is too long!"
    return "Hello " + name + "!!"

def get_dad_joke():
    """
    Fetches a random dad joke from the icanhazdadjoke.com API.

    The function makes an HTTP GET request to the <https://icanhazdadjoke.com/> API
    with appropriate headers to receive JSON response.

    Args:
        None
    Returns:
        str: A random dad joke if successful, otherwise returns an error message.
            - Success: Returns the joke text
            - Failure: Returns "Failed to retrieve a joke." if the API request fails
            - No joke: Returns "No joke found." if the API response doesn't contain a joke

    Requires:
        requests: The requests library must be imported to make HTTP requests
    """

    headers = {"Accept": "application/json"}
    response = requests.get("https://icanhazdadjoke.com/", headers=headers)
    if response.status_code == 200:
        data = response.json()
        return data.get("joke", "No joke found.")
    else:
        return "Failed to retrieve a joke."

def calculate(n1, op, n2):
    """
    Performs basic arithmetic operations between two numbers.
    This function takes two numbers and an operator as input and returns the result
    of the arithmetic operation. Supported operations are addition (+), subtraction (-),
    multiplication (*), and division (/). For division, checks for division by zero.
    Args:
        n1 (float): First number
        op (str): Operator ('+', '-', '*', or '/')
        n2 (float): Second number
    Returns:
        float: Result of the arithmetic operation if valid
        str: "Error" if invalid operation (like division by zero)
    Examples:
        >>> calculate(10, "+", 5)
        15
        >>> calculate(10, "/", 0)
        "Error"
    """

    if op == "+": return str(n1 + n2)
    if op == "-": return str(n1 - n2)
    if op == "*": return str(n1 * n2)
    if op == "/" and n2 != 0: return str(n1 / n2)
    return "Error"
    
# demo = gr.Interface(fn=greet, inputs="text", outputs="text")
with gr.Blocks() as demo:
    name = gr.Textbox(label="Type your name", placeholder="Enter your name here")
    greet_btn = gr.Button("Greet")
    output = gr.Textbox(label="Message")
    greet_btn.click(fn=greet, inputs=name, outputs=output, api_name="greet")
    joke_btn = gr.Button("Get Dad Joke")
    joke_output = gr.Textbox(label="Dad Joke")
    joke_btn.click(fn=get_dad_joke, outputs=joke_output, api_name="get_dad_joke")

    with gr.Row():
        num1 = gr.Number(label="First Number")
        operation = gr.Dropdown(
            choices=["+", "-", "*", "/"],
            label="Operation"
        )
        num2 = gr.Number(label="Second Number")
    
    calc_btn = gr.Button("Calculate")
    calc_output = gr.Text(label="Result")
    calc_btn.click(
        fn=calculate,
        inputs=[num1, operation, num2],
        outputs=calc_output,
        api_name="calculate")

demo.launch(mcp_server=True)