File size: 3,437 Bytes
0345768
 
 
 
 
 
243d864
0345768
b775a96
0345768
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19ecd98
8ad04f8
19ecd98
 
0295b84
 
 
0345768
 
 
 
 
 
 
 
 
19ecd98
b775a96
 
0345768
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from typing import List

import numpy as np
import requests
import gradio as gr

import huggingface_hub


def file_as_a_string(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()"
    )


def combine_several_space_inputs(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 control_input_and_output_types(interfaces):
        print("Spaces have different input or output types, could not combine them!")
        return ""
    else:
        return file_as_a_string(name_list)


def control_input_and_output_types(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


def check_space_availability(hf_token: str, space_name: str) -> str:
    repo_name = huggingface_hub.hf_api.get_full_repo_name(model_id=space_name, token=hf_token)
    # requests.get()
    return repo_name


def space_builder(spaces: str, hf_token: str, username: str, space_name: str, space_description: str) -> str:
    """
    Creates a space with given inputs
    :param spaces:
    :param hf_token:
    :param username:
    :param space_name:
    :param space_description:
    :return:
    """
    # return "Thank you for using the draft, it will be completed later"
    # check_space_availability(hf_token, space_name) +
    return "version of huggingface_hub :" + huggingface_hub.__version__


iface = gr.Interface(
    fn=space_builder,
    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="HF Write Token"),
        gr.inputs.Textbox(lines=1, placeholder="Your username"),
        gr.inputs.Textbox(lines=1, placeholder="Name for the space"),
        gr.inputs.Textbox(lines=1, placeholder="Description for the space"),
    ],
    outputs="text",
)
iface.launch()