File size: 1,192 Bytes
eb616b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import math

# 1. Write the function
def math_superpowers(number):
    """
    Takes a number and returns its square, cube, and factorial.
    """
    if number is None:
        return None, None, None
    
    # Check if the number is a negative integer or float for factorial safety
    # (Factorial generally requires non-negative integers)
    if number < 0 or not number.is_integer():
        return number**2, number**3, "Factorial requires a non-negative integer"
    
    val = int(number)
    square = val ** 2
    cube = val ** 3
    factorial = math.factorial(val)
    
    # Return as a list/tuple to populate multiple output boxes
    return square, cube, factorial

# 2. Wrap the function in a Gradio Interface
# We map 1 input to 3 outputs
demo = gr.Interface(
    fn=math_superpowers,
    inputs=gr.Number(label="Enter a Number"),
    outputs=[
        gr.Number(label="Square"),
        gr.Number(label="Cube"),
        gr.Number(label="Factorial")
    ],
    title="Math Superpowers Calculator",
    description="Enter a number to see its Square, Cube, and Factorial instantly!"
)

# 3. Launch the app with share=True as requested
demo.launch(share=True)