navkrishna commited on
Commit
8b4eea2
·
verified ·
1 Parent(s): 243fa01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -35
app.py CHANGED
@@ -1,45 +1,40 @@
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()
 
1
  import gradio as gr
2
 
3
+ def calculator(input_text):
4
  try:
5
+ return str(eval(input_text))
6
+ except:
7
+ return "Error"
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ def update_input(input_text, new_value):
10
+ if new_value == "C":
11
+ return ""
12
+ elif new_value == "=":
13
+ try:
14
+ return str(eval(input_text))
15
+ except:
16
+ return "Error"
17
+ else:
18
+ return input_text + new_value
19
+
20
+ with gr.Blocks() as iface:
21
  gr.Markdown("# Simple Calculator")
22
 
23
  with gr.Row():
24
+ input_display = gr.Textbox(value="", label="Display")
 
 
 
 
 
 
 
25
 
26
+ button_layout = [
27
+ ["7", "8", "9", "/"],
28
+ ["4", "5", "6", "*"],
29
+ ["1", "2", "3", "-"],
30
+ ["0", ".", "C", "+"],
31
+ ["="]
32
+ ]
33
 
34
+ for row in button_layout:
35
+ with gr.Row():
36
+ for button in row:
37
+ btn = gr.Button(button)
38
+ btn.click(update_input, inputs=[input_display, btn], outputs=input_display)
39
 
40
+ iface.launch()