Spaces:
Runtime error
Runtime error
Commit
·
48b01c9
1
Parent(s):
77d989e
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import math
|
3 |
+
class ValidationException(Exception):
|
4 |
+
pass
|
5 |
+
|
6 |
+
def string_to_int(size_str: str):
|
7 |
+
mappings = {'M': 10**6, 'B': 10**9, 'T': 10**12}
|
8 |
+
suffix = size_str.upper()[-1]
|
9 |
+
size = size_str.[:-1]
|
10 |
+
try:
|
11 |
+
size = float(size)
|
12 |
+
except ValueError:
|
13 |
+
raise ValidationException("The numbers cannot be converted into a float")
|
14 |
+
if suffix not in list[mappings.keys()] and (int(suffix)<48 or int(suffix)>57) :
|
15 |
+
raise ValidationException(f"The suffix is not valid. It can only be one of {list[mappings.keys()]}")
|
16 |
+
return size * mappings[suffix]
|
17 |
+
|
18 |
+
def int_to_string(size: int, precision=2):
|
19 |
+
power = math.ceil(math.log(size))
|
20 |
+
size_human_readable = ""
|
21 |
+
if power > 12:
|
22 |
+
size_human_readable = "%.2f Trillion"%size/(10**12)
|
23 |
+
elif power > 9:
|
24 |
+
size_human_readable = "%.2f Billion"%size/(10**9)
|
25 |
+
elif power > 6:
|
26 |
+
size_human_readable = "%.2f Million"%size/(10**6)
|
27 |
+
else:
|
28 |
+
size_human_readable = str(size)
|
29 |
+
return size_human_readable
|
30 |
+
|
31 |
+
def compute_data_size(size_in):
|
32 |
+
size_out = string_to_int(size_in)
|
33 |
+
output = int_to_string(size_out)
|
34 |
+
return output
|
35 |
+
|
36 |
+
demo = gr.Interface(
|
37 |
+
fn=compute_data_size,
|
38 |
+
inputs = "text",
|
39 |
+
outputs = "text"
|
40 |
+
)
|
41 |
+
|
42 |
+
demo.launch()
|