calculator / app.py
Xenobd's picture
Update app.py
50834fb verified
raw
history blame
1.31 kB
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)