File size: 1,185 Bytes
08bec29
04ab150
08bec29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04ab150
08bec29
 
 
04ab150
08bec29
 
 
04ab150
08bec29
 
 
 
04ab150
08bec29
 
 
 
 
 
 
 
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
import gradio as gr

# Define the calculator function
def calculate(num1, num2, operation):
    try:
        num1 = float(num1)
        num2 = float(num2)
        if operation == "Addition":
            return num1 + num2
        elif operation == "Subtraction":
            return num1 - num2
        elif operation == "Multiplication":
            return num1 * num2
        elif operation == "Division":
            if num2 == 0:
                return "❌ Cannot divide by zero"
            return num1 / num2
    except Exception as e:
        return f"Error: {str(e)}"

# Build the Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("## 🔢 Simple Calculator")

    with gr.Row():
        num1 = gr.Number(label="First Number")
        num2 = gr.Number(label="Second Number")

    operation = gr.Dropdown(
        choices=["Addition", "Subtraction", "Multiplication", "Division"],
        label="Select Operation"
    )

    result = gr.Textbox(label="Result", interactive=False)

    calc_button = gr.Button("Calculate")
    calc_button.click(fn=calculate, inputs=[num1, num2, operation], outputs=result)

# Launch the app
if __name__ == "__main__":
    demo.launch()