Spaces:
Sleeping
Sleeping
File size: 1,309 Bytes
50834fb 1b7ac36 50834fb 1b7ac36 50834fb 1b7ac36 50834fb 1b7ac36 50834fb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import gradio as gr
def calculator(num1: float, num2: float, operation: str) -> str:
"""
Perform basic arithmetic operations:
- add: num1 + num2
- subtract: num1 - num2
- multiply: num1 * num2
- divide: num1 / num2 (with zero-check)
Args:
num1: first number
num2: second number
operation: one of "add", "subtract", "multiply", "divide"
Returns:
The result as a float, or an error message string.
"""
if operation == "add":
return str(num1 + num2)
elif operation == "subtract":
return str(num1 - num2)
elif operation == "multiply":
return str(num1 * num2)
elif operation == "divide":
if num2 == 0:
return "Error: Division by zero"
return str(num1 / num2)
else:
return f"Error: Unknown operation '{operation}'"
demo = gr.Interface(
fn=calculator,
inputs=[
gr.Number(value=0.0, label="First Number"),
gr.Number(value=0.0, label="Second Number"),
gr.Dropdown(["add", "subtract", "multiply", "divide"], label="Operation"),
],
outputs=gr.Textbox(label="Result"),
title="🧮 Simple Calculator",
description="Enter two numbers and choose an operation."
)
if __name__ == "__main__":
demo.launch(mcp_server=True)
|