Bushra346 commited on
Commit
2f02a14
·
verified ·
1 Parent(s): 931369f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -13
app.py CHANGED
@@ -1,22 +1,39 @@
1
  # app.py
 
2
  import gradio as gr
3
 
4
- # Define the calculator function
5
- def calculator(expression):
6
  try:
7
- result = eval(expression)
8
- return result
9
- except:
10
- return "Invalid input"
 
 
 
 
 
 
 
 
 
11
 
12
  # Create Gradio interface
13
- iface = gr.Interface(
14
- fn=calculator,
15
- inputs=gr.Textbox(label="Enter a math expression (e.g., 5 + 3 * 2)"),
 
 
 
 
16
  outputs=gr.Textbox(label="Result"),
17
- title="🧮 Calculator",
18
- description="This calculator can do addition (+), subtraction (-), multiplication (*), and division (/)."
 
 
19
  )
20
 
21
- # Launch the app
22
- iface.launch()
 
 
1
  # app.py
2
+
3
  import gradio as gr
4
 
5
+ # Define the calculator logic
6
+ def simple_calculator(num1, num2, operation):
7
  try:
8
+ if operation == "+":
9
+ result = num1 + num2
10
+ elif operation == "-":
11
+ result = num1 - num2
12
+ elif operation == "*":
13
+ result = num1 * num2
14
+ elif operation == "/":
15
+ result = num1 / num2 if num2 != 0 else "∞ (Divide by Zero)"
16
+ else:
17
+ result = "Unknown operation"
18
+ return f"✅ Result: {result}"
19
+ except Exception as e:
20
+ return f"❌ Error: {str(e)}"
21
 
22
  # Create Gradio interface
23
+ demo = gr.Interface(
24
+ fn=simple_calculator,
25
+ inputs=[
26
+ gr.Number(label="Enter First Number"),
27
+ gr.Number(label="Enter Second Number"),
28
+ gr.Radio(["+", "-", "*", "/"], label="Select Operation"),
29
+ ],
30
  outputs=gr.Textbox(label="Result"),
31
+ title="🧮 AI Calculator",
32
+ description="Perform basic math operations: Addition, Subtraction, Multiplication, and Division.",
33
+ allow_flagging="never",
34
+ theme="default"
35
  )
36
 
37
+ if __name__ == "__main__":
38
+ demo.launch()
39
+