Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,40 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
from fractions import Fraction
|
|
|
|
| 3 |
|
| 4 |
def calculator(num1, operation, num2=None):
|
| 5 |
if operation == "add":
|
| 6 |
-
|
| 7 |
elif operation == "subtract":
|
| 8 |
-
|
| 9 |
elif operation == "multiply":
|
| 10 |
-
|
| 11 |
elif operation == "divide":
|
| 12 |
-
|
| 13 |
elif operation == "square":
|
| 14 |
-
|
| 15 |
elif operation == "cube":
|
| 16 |
-
|
| 17 |
elif operation == "exponential":
|
| 18 |
-
|
| 19 |
elif operation == "frac add":
|
| 20 |
-
|
| 21 |
elif operation == "frac subtract":
|
| 22 |
-
|
| 23 |
elif operation == "frac multiply":
|
| 24 |
-
|
| 25 |
elif operation == "frac divide":
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
demo = gr.Interface(
|
| 30 |
fn=calculator,
|
|
@@ -40,10 +50,10 @@ demo = gr.Interface(
|
|
| 40 |
[-4, "multiply", 2.5],
|
| 41 |
[0, "subtract", 1.2],
|
| 42 |
[2, "exponential", 3],
|
| 43 |
-
[Fraction(
|
| 44 |
-
[Fraction(3, 4), "frac subtract", Fraction(1,
|
| 45 |
-
[Fraction(
|
| 46 |
-
[Fraction(3, 4), "frac divide", Fraction(1,
|
| 47 |
],
|
| 48 |
title="Scientific Calculator",
|
| 49 |
description="Here's a sample scientific calculator. Enjoy! Code by: Freddy Aboulton Improved by: Usually3 and ChatGpt"
|
|
|
|
|
|
|
| 1 |
from fractions import Fraction
|
| 2 |
+
import gradio as gr
|
| 3 |
|
| 4 |
def calculator(num1, operation, num2=None):
|
| 5 |
if operation == "add":
|
| 6 |
+
return num1 + num2
|
| 7 |
elif operation == "subtract":
|
| 8 |
+
return num1 - num2
|
| 9 |
elif operation == "multiply":
|
| 10 |
+
return num1 * num2
|
| 11 |
elif operation == "divide":
|
| 12 |
+
return num1 / num2
|
| 13 |
elif operation == "square":
|
| 14 |
+
return num1 * num1
|
| 15 |
elif operation == "cube":
|
| 16 |
+
return num1 * num1 * num1
|
| 17 |
elif operation == "exponential":
|
| 18 |
+
return num1 ** num2
|
| 19 |
elif operation == "frac add":
|
| 20 |
+
return num1 + num2
|
| 21 |
elif operation == "frac subtract":
|
| 22 |
+
return num1 - num2
|
| 23 |
elif operation == "frac multiply":
|
| 24 |
+
return num1 * num2
|
| 25 |
elif operation == "frac divide":
|
| 26 |
+
return num1 / num2
|
| 27 |
+
|
| 28 |
+
# convert all inputs to fractions
|
| 29 |
+
num1 = Fraction(num1)
|
| 30 |
+
if num2 is not None:
|
| 31 |
+
num2 = Fraction(num2)
|
| 32 |
+
|
| 33 |
+
# convert result back to float if input was not a fraction
|
| 34 |
+
if isinstance(num1, float) or isinstance(num2, float):
|
| 35 |
+
return float(calculator(float(num1), operation, float(num2)))
|
| 36 |
+
else:
|
| 37 |
+
return num1
|
| 38 |
|
| 39 |
demo = gr.Interface(
|
| 40 |
fn=calculator,
|
|
|
|
| 50 |
[-4, "multiply", 2.5],
|
| 51 |
[0, "subtract", 1.2],
|
| 52 |
[2, "exponential", 3],
|
| 53 |
+
[Fraction(1, 2), "frac add", Fraction(1, 3)],
|
| 54 |
+
[Fraction(3, 4), "frac subtract", Fraction(1, 5)],
|
| 55 |
+
[Fraction(2, 3), "frac multiply", Fraction(4, 5)],
|
| 56 |
+
[Fraction(3, 4), "frac divide", Fraction(1, 2)]
|
| 57 |
],
|
| 58 |
title="Scientific Calculator",
|
| 59 |
description="Here's a sample scientific calculator. Enjoy! Code by: Freddy Aboulton Improved by: Usually3 and ChatGpt"
|