shamikbose89's picture
Update app.py
d2eb160
raw
history blame
1.27 kB
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.log(size))
size_human_readable = ""
if power > 12:
size_human_readable = "%.2f Trillion"%size/(10**12)
elif power > 9:
size_human_readable = "%.2f Billion"%size/(10**9)
elif power > 6:
size_human_readable = "%.2f Million"%size/(10**6)
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()