Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,45 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
def calculator(
|
4 |
try:
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
# Launch the app
|
22 |
if __name__ == "__main__":
|
23 |
-
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
def calculator(num1, num2, operation):
|
4 |
try:
|
5 |
+
num1, num2 = float(num1), float(num2)
|
6 |
+
if operation == "Add":
|
7 |
+
return num1 + num2
|
8 |
+
elif operation == "Subtract":
|
9 |
+
return num1 - num2
|
10 |
+
elif operation == "Multiply":
|
11 |
+
return num1 * num2
|
12 |
+
elif operation == "Divide":
|
13 |
+
if num2 == 0:
|
14 |
+
return "Error: Division by zero!"
|
15 |
+
return num1 / num2
|
16 |
+
else:
|
17 |
+
return "Invalid operation"
|
18 |
+
except ValueError:
|
19 |
+
return "Please enter valid numbers"
|
20 |
|
21 |
+
# Creating the interface
|
22 |
+
with gr.Blocks() as demo:
|
23 |
+
gr.Markdown("# Simple Calculator")
|
24 |
+
|
25 |
+
with gr.Row():
|
26 |
+
num1_input = gr.Number(label="First Number")
|
27 |
+
num2_input = gr.Number(label="Second Number")
|
28 |
+
|
29 |
+
operation_dropdown = gr.Dropdown(
|
30 |
+
choices=["Add", "Subtract", "Multiply", "Divide"],
|
31 |
+
label="Operation",
|
32 |
+
value="Add"
|
33 |
+
)
|
34 |
+
|
35 |
+
calculate_btn = gr.Button("Calculate")
|
36 |
+
result = gr.Textbox(label="Result")
|
37 |
+
|
38 |
+
calculate_btn.click(
|
39 |
+
fn=calculator,
|
40 |
+
inputs=[num1_input, num2_input, operation_dropdown],
|
41 |
+
outputs=result
|
42 |
+
)
|
43 |
|
|
|
44 |
if __name__ == "__main__":
|
45 |
+
demo.launch()
|