File size: 1,703 Bytes
7aced48
 
 
f09edcf
7aced48
f09edcf
7aced48
 
 
f09edcf
7aced48
 
f09edcf
 
 
7aced48
f09edcf
 
7aced48
f09edcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7aced48
 
 
 
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
```python
import gradio as gr

# Define the functions
def add(a, b):
    """Add two numbers"""
    return a + b

def multiply(a, b):
    """Multiply two numbers"""
    return a * b

def greet(name):
    """Greet a person by name"""
    return f"Hello, {name}!"

# Create the Gradio interface
demo = gr.Blocks()

with demo:
    gr.Markdown("# Simple Calculator and Greeter")
    
    # Create a tab for the calculator
    with gr.Tab("Calculator"):
        gr.Markdown("### Calculator")
        with gr.Row():
            a = gr.Number(label="Number 1")
            b = gr.Number(label="Number 2")
        with gr.Row():
            operation = gr.Radio(["Add", "Multiply"], label="Operation")
        result = gr.Number(label="Result")
        
        # Define the event handlers
        def calculate(a, b, operation):
            if operation == "Add":
                return add(a, b)
            elif operation == "Multiply":
                return multiply(a, b)
        
        # Link the event handlers to the UI components
        gr.Button("Calculate").click(
            calculate,
            inputs=[a, b, operation],
            outputs=result
        )
    
    # Create a tab for the greeter
    with gr.Tab("Greeter"):
        gr.Markdown("### Greeter")
        name = gr.Textbox(label="Name")
        greeting = gr.Textbox(label="Greeting")
        
        # Define the event handler
        def greet_person(name):
            return greet(name)
        
        # Link the event handler to the UI components
        gr.Button("Greet").click(
            greet_person,
            inputs=name,
            outputs=greeting
        )

if __name__ == "__main__":
    demo.launch()
```