Swathi6 commited on
Commit
dad3afe
·
verified ·
1 Parent(s): 148c7ff

Create calculator.py

Browse files
Files changed (1) hide show
  1. calculator.py +34 -0
calculator.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ # Function to perform the calculator operations
4
+ def calculate_expression(expression):
5
+ try:
6
+ # Using eval to evaluate the mathematical expression
7
+ result = eval(expression)
8
+ return result
9
+ except:
10
+ return "Error"
11
+
12
+ # Create the interface using Gradio
13
+ def create_calculator():
14
+ with gr.Blocks() as demo:
15
+ gr.Markdown("### Simple Calculator")
16
+
17
+ # Create the input box where users can enter expressions
18
+ expression_input = gr.Textbox(label="Enter Expression", placeholder="e.g., 3 + 4", lines=1)
19
+
20
+ # Output area to show the result
21
+ result_output = gr.Textbox(label="Result", interactive=False)
22
+
23
+ # Button to trigger calculation
24
+ calculate_button = gr.Button("Calculate")
25
+
26
+ # Link the button to the calculation function
27
+ calculate_button.click(fn=calculate_expression, inputs=expression_input, outputs=result_output)
28
+
29
+ return demo
30
+
31
+ # Run the Gradio app
32
+ if __name__ == "__main__":
33
+ calculator_app = create_calculator()
34
+ calculator_app.launch()