Xenobd commited on
Commit
50834fb
·
verified ·
1 Parent(s): 1b7ac36

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -23
app.py CHANGED
@@ -1,27 +1,45 @@
1
- import streamlit as st
2
 
3
- # App title
4
- st.title("🧮 Simple Calculator")
 
 
 
 
 
5
 
6
- # Input fields
7
- num1 = st.number_input("Enter first number", value=0.0)
8
- num2 = st.number_input("Enter second number", value=0.0)
 
9
 
10
- # Operation selector
11
- operation = st.selectbox("Select operation", ["Add", "Subtract", "Multiply", "Divide"])
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- # Calculate and display result
14
- if st.button("Calculate"):
15
- if operation == "Add":
16
- result = num1 + num2
17
- elif operation == "Subtract":
18
- result = num1 - num2
19
- elif operation == "Multiply":
20
- result = num1 * num2
21
- elif operation == "Divide":
22
- if num2 != 0:
23
- result = num1 / num2
24
- else:
25
- result = "Error: Division by zero"
26
-
27
- st.success(f"Result: {result}")
 
1
+ import gradio as gr
2
 
3
+ def calculator(num1: float, num2: float, operation: str) -> str:
4
+ """
5
+ Perform basic arithmetic operations:
6
+ - add: num1 + num2
7
+ - subtract: num1 - num2
8
+ - multiply: num1 * num2
9
+ - divide: num1 / num2 (with zero-check)
10
 
11
+ Args:
12
+ num1: first number
13
+ num2: second number
14
+ operation: one of "add", "subtract", "multiply", "divide"
15
 
16
+ Returns:
17
+ The result as a float, or an error message string.
18
+ """
19
+ if operation == "add":
20
+ return str(num1 + num2)
21
+ elif operation == "subtract":
22
+ return str(num1 - num2)
23
+ elif operation == "multiply":
24
+ return str(num1 * num2)
25
+ elif operation == "divide":
26
+ if num2 == 0:
27
+ return "Error: Division by zero"
28
+ return str(num1 / num2)
29
+ else:
30
+ return f"Error: Unknown operation '{operation}'"
31
 
32
+ demo = gr.Interface(
33
+ fn=calculator,
34
+ inputs=[
35
+ gr.Number(value=0.0, label="First Number"),
36
+ gr.Number(value=0.0, label="Second Number"),
37
+ gr.Dropdown(["add", "subtract", "multiply", "divide"], label="Operation"),
38
+ ],
39
+ outputs=gr.Textbox(label="Result"),
40
+ title="🧮 Simple Calculator",
41
+ description="Enter two numbers and choose an operation."
42
+ )
43
+
44
+ if __name__ == "__main__":
45
+ demo.launch(mcp_server=True)