codeboosterstech commited on
Commit
f09edcf
·
verified ·
1 Parent(s): 8d2c318

Update app.py via Space Creator

Browse files
Files changed (1) hide show
  1. app.py +51 -39
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
- """Return the sum of two numbers."""
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
- """Return the product of two numbers."""
16
  return a * b
17
 
18
- def divide(a, b):
19
- """Return the quotient of two numbers."""
20
- if b == 0:
21
- return "Error: Cannot divide by zero"
22
- return a / b
23
 
24
- def calculator(a, b, operation):
25
- """Perform the specified operation on two numbers."""
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 = gr.Interface(
38
- calculator,
39
- [
40
- gr.Number(label="Number 1"),
41
- gr.Number(label="Number 2"),
42
- gr.Radio(
43
- choices=["Addition", "Subtraction", "Multiplication", "Division"],
44
- label="Operation",
45
- ),
46
- ],
47
- gr.Output(label="Result"),
48
- title="Simple Calculator",
49
- description="A simple calculator that performs basic arithmetic operations.",
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()