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()