Suchinthana Wijesundara commited on
Commit
528581a
·
1 Parent(s): 6d2493b

Create gradio_calculator.py

Browse files
Files changed (1) hide show
  1. gradio_calculator.py +41 -0
gradio_calculator.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+
4
+ def calculate(num1, num2, operation):
5
+ try:
6
+ num1 = float(num1)
7
+ num2 = float(num2)
8
+
9
+ if operation == "+":
10
+ result = num1 + num2
11
+ elif operation == "-":
12
+ result = num1 - num2
13
+ elif operation == "*":
14
+ result = num1 * num2
15
+ elif operation == "/":
16
+ if num2 == 0:
17
+ return "Cannot divide by zero"
18
+ result = num1 / num2
19
+ else:
20
+ return "Invalid operation"
21
+
22
+ return result
23
+ except ValueError:
24
+ return "Please enter valid numbers"
25
+
26
+
27
+ with gr.Blocks() as demo:
28
+ gr.Markdown("# Simple Calculator")
29
+
30
+ with gr.Row():
31
+ num1 = gr.Number(label="First number")
32
+ operation = gr.Dropdown(["+", "-", "*", "/"], label="Operation", value="+")
33
+ num2 = gr.Number(label="Second number")
34
+
35
+ calc_button = gr.Button("Calculate")
36
+ output = gr.Textbox(label="Result")
37
+
38
+ calc_button.click(fn=calculate, inputs=[num1, num2, operation], outputs=output)
39
+
40
+
41
+ demo.launch()