Spaces:
Sleeping
Sleeping
MathMan.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
# 1. Write the function
|
| 5 |
+
def math_superpowers(number):
|
| 6 |
+
"""
|
| 7 |
+
Takes a number and returns its square, cube, and factorial.
|
| 8 |
+
"""
|
| 9 |
+
if number is None:
|
| 10 |
+
return None, None, None
|
| 11 |
+
|
| 12 |
+
# Check if the number is a negative integer or float for factorial safety
|
| 13 |
+
# (Factorial generally requires non-negative integers)
|
| 14 |
+
if number < 0 or not number.is_integer():
|
| 15 |
+
return number**2, number**3, "Factorial requires a non-negative integer"
|
| 16 |
+
|
| 17 |
+
val = int(number)
|
| 18 |
+
square = val ** 2
|
| 19 |
+
cube = val ** 3
|
| 20 |
+
factorial = math.factorial(val)
|
| 21 |
+
|
| 22 |
+
# Return as a list/tuple to populate multiple output boxes
|
| 23 |
+
return square, cube, factorial
|
| 24 |
+
|
| 25 |
+
# 2. Wrap the function in a Gradio Interface
|
| 26 |
+
# We map 1 input to 3 outputs
|
| 27 |
+
demo = gr.Interface(
|
| 28 |
+
fn=math_superpowers,
|
| 29 |
+
inputs=gr.Number(label="Enter a Number"),
|
| 30 |
+
outputs=[
|
| 31 |
+
gr.Number(label="Square"),
|
| 32 |
+
gr.Number(label="Cube"),
|
| 33 |
+
gr.Number(label="Factorial")
|
| 34 |
+
],
|
| 35 |
+
title="Math Superpowers Calculator",
|
| 36 |
+
description="Enter a number to see its Square, Cube, and Factorial instantly!"
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# 3. Launch the app with share=True as requested
|
| 40 |
+
demo.launch(share=True)
|