navkrishna commited on
Commit
243fa01
·
verified ·
1 Parent(s): 0815b5a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -17
app.py CHANGED
@@ -1,23 +1,45 @@
1
  import gradio as gr
2
 
3
- def calculator(input_str):
4
  try:
5
- result = eval(input_str)
6
- return result
7
- except ZeroDivisionError:
8
- return "Division by zero error"
9
- except Exception as e:
10
- return f"Error: {str(e)}"
 
 
 
 
 
 
 
 
 
11
 
12
- # Create the Gradio interface
13
- interface = gr.Interface(
14
- fn=calculator,
15
- inputs=gr.inputs.Textbox(lines=2, placeholder="Enter calculation (e.g., 3 + 5 * 2)", label="Calculator Input"),
16
- outputs="text",
17
- title="Simple Calculator",
18
- description="Enter a mathematical expression to calculate the result."
19
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- # Launch the app
22
  if __name__ == "__main__":
23
- interface.launch()
 
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()