from typing import List import numpy as np import requests import gradio as gr from huggingface_hub import ( create_repo, get_full_repo_name, upload_file, ) class SpaceBuilder: error_message = "" @classmethod def file_as_a_string(cls, name_list: List[str]) -> str: """ Returns the file that is going to be created in the new space as string. :param name_list: list of space names :return: file as a string """ return ( f"import gradio as gr" f"\nname_list = {name_list} " f"\ninterfaces = [gr.Interface.load(name) for name in name_list]" f"\ngr.mix.Parallel(*interfaces).launch()" ) @classmethod def combine_several_space_inputs(cls, names: str) -> str: """ Combines several space inputs and returns the file that is going to be created in the new space as string. :param names: space inputs :return: file as a string """ name_list = names.split("\n") interfaces = [gr.Interface.load(name) for name in name_list] if not cls.control_input_and_output_types(interfaces): print("Spaces have different input or output types, could not combine them!") return "" else: return cls.file_as_a_string(name_list) @classmethod def control_input_and_output_types(cls, interface_list: List["gr.Interface"]) -> bool: """ Controls whether if input and output types of the given interfaces are the same. :param interface_list: list of interfaces :return: True if all input and output types are the same """ first_input_types = [type(input) for input in interface_list[0].input_components] first_output_types = [ type(output) for output in interface_list[0].output_components ] for interface in interface_list: interface_input_types = [type(input) for input in interface.input_components] if not np.all( interface_input_types == first_input_types ): # Vectorize the comparison and don't use double for loop return False interface_output_types = [ type(output) for output in interface.output_components ] if not np.all(interface_output_types == first_output_types): return False return True @classmethod def check_space_name_availability(cls, hf_token: str, space_name: str) -> bool: """ Check whether if the space_name is currently used. :param hf_token: hugging_face token :param space_name: :return: True if the space_name is available """ try: repo_name = get_full_repo_name(model_id=space_name, token=hf_token) except Exception as ex: cls.error_message = "You have given an incorrect HuggingFace token" return False url = f"https://huggingface.co/spaces/{repo_name}" response = requests.get(url) if response.status_code == 200: cls.error_message = "The the username/space_name is already used for the given token's user" return False else: return True @staticmethod def build_space(spaces: str, hf_token: str, space_name: str, space_description: str) -> str: """ Creates a space with given inputs :param spaces: :param hf_token: :param space_name: :param space_description: :return: """ if spaces == "" or hf_token == "" or space_name == "" or space_description == "": return "Please fill all the inputs" if SpaceBuilder.check_space_name_availability(hf_token, space_name): return "The space name is available" else: return SpaceBuilder.error_message iface = gr.Interface( fn=SpaceBuilder.build_space, inputs=[ gr.inputs.Textbox( lines=4, placeholder=( f"Drop space links at each line, ie:" f"\nspaces/Norod78/ComicsHeroHD" f"\nspaces/Amrrs/gradio-sentiment-analyzer" ), ), gr.inputs.Textbox(lines=1, placeholder="HuggingFace Write Token"), gr.inputs.Textbox(lines=1, placeholder="Name for the space"), gr.inputs.Textbox(lines=1, placeholder="Description for the space"), ], outputs="text", ) iface.launch()