Spaces:
Runtime error
Runtime error
Update app.py via Space Creator
Browse files
app.py
CHANGED
|
@@ -1,53 +1,65 @@
|
|
| 1 |
```python
|
| 2 |
-
# Simple Calculator Example
|
| 3 |
-
import math
|
| 4 |
import gradio as gr
|
| 5 |
|
|
|
|
| 6 |
def add(a, b):
|
| 7 |
-
"""
|
| 8 |
return a + b
|
| 9 |
|
| 10 |
-
def subtract(a, b):
|
| 11 |
-
"""Return the difference of two numbers."""
|
| 12 |
-
return a - b
|
| 13 |
-
|
| 14 |
def multiply(a, b):
|
| 15 |
-
"""
|
| 16 |
return a * b
|
| 17 |
|
| 18 |
-
def
|
| 19 |
-
"""
|
| 20 |
-
|
| 21 |
-
return "Error: Cannot divide by zero"
|
| 22 |
-
return a / b
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
if operation == "Addition":
|
| 27 |
-
return add(a, b)
|
| 28 |
-
elif operation == "Subtraction":
|
| 29 |
-
return subtract(a, b)
|
| 30 |
-
elif operation == "Multiplication":
|
| 31 |
-
return multiply(a, b)
|
| 32 |
-
elif operation == "Division":
|
| 33 |
-
return divide(a, b)
|
| 34 |
-
else:
|
| 35 |
-
return "Error: Invalid operation"
|
| 36 |
|
| 37 |
-
demo
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
gr.
|
| 43 |
-
|
| 44 |
-
label="
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
| 53 |
demo.launch()
|
|
|
|
| 1 |
```python
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
+
# Define the functions
|
| 5 |
def add(a, b):
|
| 6 |
+
"""Add two numbers"""
|
| 7 |
return a + b
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def multiply(a, b):
|
| 10 |
+
"""Multiply two numbers"""
|
| 11 |
return a * b
|
| 12 |
|
| 13 |
+
def greet(name):
|
| 14 |
+
"""Greet a person by name"""
|
| 15 |
+
return f"Hello, {name}!"
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Create the Gradio interface
|
| 18 |
+
demo = gr.Blocks()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
with demo:
|
| 21 |
+
gr.Markdown("# Simple Calculator and Greeter")
|
| 22 |
+
|
| 23 |
+
# Create a tab for the calculator
|
| 24 |
+
with gr.Tab("Calculator"):
|
| 25 |
+
gr.Markdown("### Calculator")
|
| 26 |
+
with gr.Row():
|
| 27 |
+
a = gr.Number(label="Number 1")
|
| 28 |
+
b = gr.Number(label="Number 2")
|
| 29 |
+
with gr.Row():
|
| 30 |
+
operation = gr.Radio(["Add", "Multiply"], label="Operation")
|
| 31 |
+
result = gr.Number(label="Result")
|
| 32 |
+
|
| 33 |
+
# Define the event handlers
|
| 34 |
+
def calculate(a, b, operation):
|
| 35 |
+
if operation == "Add":
|
| 36 |
+
return add(a, b)
|
| 37 |
+
elif operation == "Multiply":
|
| 38 |
+
return multiply(a, b)
|
| 39 |
+
|
| 40 |
+
# Link the event handlers to the UI components
|
| 41 |
+
gr.Button("Calculate").click(
|
| 42 |
+
calculate,
|
| 43 |
+
inputs=[a, b, operation],
|
| 44 |
+
outputs=result
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# Create a tab for the greeter
|
| 48 |
+
with gr.Tab("Greeter"):
|
| 49 |
+
gr.Markdown("### Greeter")
|
| 50 |
+
name = gr.Textbox(label="Name")
|
| 51 |
+
greeting = gr.Textbox(label="Greeting")
|
| 52 |
+
|
| 53 |
+
# Define the event handler
|
| 54 |
+
def greet_person(name):
|
| 55 |
+
return greet(name)
|
| 56 |
+
|
| 57 |
+
# Link the event handler to the UI components
|
| 58 |
+
gr.Button("Greet").click(
|
| 59 |
+
greet_person,
|
| 60 |
+
inputs=name,
|
| 61 |
+
outputs=greeting
|
| 62 |
+
)
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|
| 65 |
demo.launch()
|