demo / app.py
codeboosterstech's picture
Update app.py via Space Creator
f09edcf verified
```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()
```