File size: 1,271 Bytes
48b01c9
 
 
 
 
 
 
 
d2eb160
48b01c9
 
 
 
2fe7b61
 
48b01c9
 
 
5abb977
48b01c9
 
0e33439
48b01c9
0e33439
48b01c9
0e33439
48b01c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import math
class ValidationException(Exception):
    pass
    
def string_to_int(size_str: str):
    mappings = {'M': 10**6, 'B': 10**9, 'T': 10**12}
    suffix = size_str.upper()[-1]
    size = size_str[:-1]
    try:
        size = float(size)
    except ValueError:
        raise ValidationException("The numbers cannot be converted into a float")
    if suffix not in list(mappings.keys()) and (int(suffix)<48 or int(suffix)>57) :
        raise ValidationException(f"The suffix is not valid. It can only be one of {list(mappings.keys())}")
    return size * mappings[suffix]

def int_to_string(size: int, precision=2):
    power = math.ceil(math.log10(size))
    size_human_readable = ""
    if power > 12:
        size_human_readable = f"{(size/10**12):.3} Trillion"
    elif power > 9:
        size_human_readable = f"{(size/10**9):.3} Billion"
    elif power > 6:
        size_human_readable = f"{(size/10**6):.3} Million"
    else:
        size_human_readable = str(size)
    return size_human_readable
        
def compute_data_size(size_in):
    size_out = string_to_int(size_in)
    output = int_to_string(size_out)
    return output

demo = gr.Interface(
    fn=compute_data_size,
    inputs = "text",
    outputs = "text"
)

demo.launch()