Spaces:
Build error
Build error
Commit
·
4797320
1
Parent(s):
67075bc
Introduce SpaceBuilder Class
Browse files
app.py
CHANGED
|
@@ -11,105 +11,109 @@ from huggingface_hub import (
|
|
| 11 |
)
|
| 12 |
|
| 13 |
|
| 14 |
-
|
| 15 |
-
""
|
| 16 |
-
Returns the file that is going to be created in the new space as string.
|
| 17 |
-
|
| 18 |
-
:param name_list: list of space names
|
| 19 |
-
:return: file as a string
|
| 20 |
-
"""
|
| 21 |
-
return (
|
| 22 |
-
f"import gradio as gr"
|
| 23 |
-
f"\nname_list = {name_list} "
|
| 24 |
-
f"\ninterfaces = [gr.Interface.load(name) for name in name_list]"
|
| 25 |
-
f"\ngr.mix.Parallel(*interfaces).launch()"
|
| 26 |
-
)
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
def combine_several_space_inputs(names: str) -> str:
|
| 30 |
-
"""
|
| 31 |
-
Combines several space inputs and returns the file that is going to be created in the new space as string.
|
| 32 |
-
|
| 33 |
-
:param names: space inputs
|
| 34 |
-
:return: file as a string
|
| 35 |
-
"""
|
| 36 |
-
name_list = names.split("\n")
|
| 37 |
-
interfaces = [gr.Interface.load(name) for name in name_list]
|
| 38 |
-
if not control_input_and_output_types(interfaces):
|
| 39 |
-
print("Spaces have different input or output types, could not combine them!")
|
| 40 |
-
return ""
|
| 41 |
-
else:
|
| 42 |
-
return file_as_a_string(name_list)
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
def control_input_and_output_types(interface_list: List["gr.Interface"]) -> bool:
|
| 46 |
-
"""
|
| 47 |
-
Controls whether if input and output types of the given interfaces are the same.
|
| 48 |
-
|
| 49 |
-
:param interface_list: list of interfaces
|
| 50 |
-
:return: True if all input and output types are the same
|
| 51 |
-
"""
|
| 52 |
-
first_input_types = [type(input) for input in interface_list[0].input_components]
|
| 53 |
-
first_output_types = [
|
| 54 |
-
type(output) for output in interface_list[0].output_components
|
| 55 |
-
]
|
| 56 |
-
for interface in interface_list:
|
| 57 |
-
interface_input_types = [type(input) for input in interface.input_components]
|
| 58 |
-
if not np.all(
|
| 59 |
-
interface_input_types == first_input_types
|
| 60 |
-
): # Vectorize the comparison and don't use double for loop
|
| 61 |
-
return False
|
| 62 |
-
interface_output_types = [
|
| 63 |
-
type(output) for output in interface.output_components
|
| 64 |
-
]
|
| 65 |
-
if not np.all(interface_output_types == first_output_types):
|
| 66 |
-
return False
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
:param space_name:
|
| 77 |
-
:return: True if the space_name is available
|
| 78 |
-
"""
|
| 79 |
-
try:
|
| 80 |
-
repo_name = get_full_repo_name(model_id=space_name, token=hf_token)
|
| 81 |
-
except Exception as ex:
|
| 82 |
-
return False
|
| 83 |
-
url = f"https://huggingface.co/spaces/{repo_name}"
|
| 84 |
-
response = requests.get(url)
|
| 85 |
-
return not response.status_code == 200
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
def space_builder(spaces: str, hf_token: str, username: str, space_name: str, space_description: str) -> str:
|
| 89 |
-
"""
|
| 90 |
-
Creates a space with given inputs
|
| 91 |
-
:param spaces:
|
| 92 |
-
:param hf_token:
|
| 93 |
-
:param username:
|
| 94 |
-
:param space_name:
|
| 95 |
-
:param space_description:
|
| 96 |
-
:return:
|
| 97 |
-
"""
|
| 98 |
-
# return "Thank you for using the draft, it will be completed later"
|
| 99 |
-
if spaces == "" or hf_token == "" or username == "" or space_name == "" or space_description == "":
|
| 100 |
-
return "Please fill all the inputs"
|
| 101 |
-
if check_space_name_availability(hf_token, space_name):
|
| 102 |
-
return "The space name is available"
|
| 103 |
-
else:
|
| 104 |
return (
|
| 105 |
-
f"
|
| 106 |
-
"\
|
| 107 |
-
"\
|
|
|
|
| 108 |
)
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
iface = gr.Interface(
|
| 112 |
-
fn=
|
| 113 |
inputs=[
|
| 114 |
gr.inputs.Textbox(
|
| 115 |
lines=4,
|
|
@@ -119,8 +123,7 @@ iface = gr.Interface(
|
|
| 119 |
f"\nspaces/Amrrs/gradio-sentiment-analyzer"
|
| 120 |
),
|
| 121 |
),
|
| 122 |
-
gr.inputs.Textbox(lines=1, placeholder="
|
| 123 |
-
gr.inputs.Textbox(lines=1, placeholder="Your username"),
|
| 124 |
gr.inputs.Textbox(lines=1, placeholder="Name for the space"),
|
| 125 |
gr.inputs.Textbox(lines=1, placeholder="Description for the space"),
|
| 126 |
],
|
|
|
|
| 11 |
)
|
| 12 |
|
| 13 |
|
| 14 |
+
class SpaceBuilder:
|
| 15 |
+
error_message = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
@classmethod
|
| 18 |
+
def file_as_a_string(cls, name_list: List[str]) -> str:
|
| 19 |
+
"""
|
| 20 |
+
Returns the file that is going to be created in the new space as string.
|
| 21 |
+
|
| 22 |
+
:param name_list: list of space names
|
| 23 |
+
:return: file as a string
|
| 24 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
return (
|
| 26 |
+
f"import gradio as gr"
|
| 27 |
+
f"\nname_list = {name_list} "
|
| 28 |
+
f"\ninterfaces = [gr.Interface.load(name) for name in name_list]"
|
| 29 |
+
f"\ngr.mix.Parallel(*interfaces).launch()"
|
| 30 |
)
|
| 31 |
|
| 32 |
+
@classmethod
|
| 33 |
+
def combine_several_space_inputs(cls, names: str) -> str:
|
| 34 |
+
"""
|
| 35 |
+
Combines several space inputs and returns the file that is going to be created in the new space as string.
|
| 36 |
+
|
| 37 |
+
:param names: space inputs
|
| 38 |
+
:return: file as a string
|
| 39 |
+
"""
|
| 40 |
+
name_list = names.split("\n")
|
| 41 |
+
interfaces = [gr.Interface.load(name) for name in name_list]
|
| 42 |
+
if not cls.control_input_and_output_types(interfaces):
|
| 43 |
+
print("Spaces have different input or output types, could not combine them!")
|
| 44 |
+
return ""
|
| 45 |
+
else:
|
| 46 |
+
return cls.file_as_a_string(name_list)
|
| 47 |
+
|
| 48 |
+
@classmethod
|
| 49 |
+
def control_input_and_output_types(cls, interface_list: List["gr.Interface"]) -> bool:
|
| 50 |
+
"""
|
| 51 |
+
Controls whether if input and output types of the given interfaces are the same.
|
| 52 |
+
|
| 53 |
+
:param interface_list: list of interfaces
|
| 54 |
+
:return: True if all input and output types are the same
|
| 55 |
+
"""
|
| 56 |
+
first_input_types = [type(input) for input in interface_list[0].input_components]
|
| 57 |
+
first_output_types = [
|
| 58 |
+
type(output) for output in interface_list[0].output_components
|
| 59 |
+
]
|
| 60 |
+
for interface in interface_list:
|
| 61 |
+
interface_input_types = [type(input) for input in interface.input_components]
|
| 62 |
+
if not np.all(
|
| 63 |
+
interface_input_types == first_input_types
|
| 64 |
+
): # Vectorize the comparison and don't use double for loop
|
| 65 |
+
return False
|
| 66 |
+
interface_output_types = [
|
| 67 |
+
type(output) for output in interface.output_components
|
| 68 |
+
]
|
| 69 |
+
if not np.all(interface_output_types == first_output_types):
|
| 70 |
+
return False
|
| 71 |
+
|
| 72 |
+
return True
|
| 73 |
+
|
| 74 |
+
@classmethod
|
| 75 |
+
def check_space_name_availability(cls, hf_token: str, space_name: str) -> bool:
|
| 76 |
+
"""
|
| 77 |
+
Check whether if the space_name is currently used.
|
| 78 |
+
|
| 79 |
+
:param hf_token: hugging_face token
|
| 80 |
+
:param space_name:
|
| 81 |
+
:return: True if the space_name is available
|
| 82 |
+
"""
|
| 83 |
+
try:
|
| 84 |
+
repo_name = get_full_repo_name(model_id=space_name, token=hf_token)
|
| 85 |
+
except Exception as ex:
|
| 86 |
+
cls.error_message = "You have given an incorrect HuggingFace token"
|
| 87 |
+
return False
|
| 88 |
+
url = f"https://huggingface.co/spaces/{repo_name}"
|
| 89 |
+
response = requests.get(url)
|
| 90 |
+
|
| 91 |
+
if response.status_code == 200:
|
| 92 |
+
cls.error_message = "The the username/space_name is already used for the given token's user"
|
| 93 |
+
return False
|
| 94 |
+
else:
|
| 95 |
+
return True
|
| 96 |
+
|
| 97 |
+
@classmethod
|
| 98 |
+
def build_space(cls, spaces: str, hf_token: str, space_name: str, space_description: str) -> str:
|
| 99 |
+
"""
|
| 100 |
+
Creates a space with given inputs
|
| 101 |
+
:param spaces:
|
| 102 |
+
:param hf_token:
|
| 103 |
+
:param space_name:
|
| 104 |
+
:param space_description:
|
| 105 |
+
:return:
|
| 106 |
+
"""
|
| 107 |
+
if spaces == "" or hf_token == "" or space_name == "" or space_description == "":
|
| 108 |
+
return "Please fill all the inputs"
|
| 109 |
+
if cls.check_space_name_availability(hf_token, space_name):
|
| 110 |
+
return "The space name is available"
|
| 111 |
+
else:
|
| 112 |
+
return cls.error_message
|
| 113 |
+
|
| 114 |
|
| 115 |
iface = gr.Interface(
|
| 116 |
+
fn=SpaceBuilder.build_space,
|
| 117 |
inputs=[
|
| 118 |
gr.inputs.Textbox(
|
| 119 |
lines=4,
|
|
|
|
| 123 |
f"\nspaces/Amrrs/gradio-sentiment-analyzer"
|
| 124 |
),
|
| 125 |
),
|
| 126 |
+
gr.inputs.Textbox(lines=1, placeholder="HuggingFace Write Token"),
|
|
|
|
| 127 |
gr.inputs.Textbox(lines=1, placeholder="Name for the space"),
|
| 128 |
gr.inputs.Textbox(lines=1, placeholder="Description for the space"),
|
| 129 |
],
|