Spaces:
Build error
Build error
omerXfaruq
commited on
Commit
•
7e0c7d3
1
Parent(s):
027ed4c
- Create_space function is implemented
Browse files
app.py
CHANGED
@@ -12,7 +12,8 @@ from huggingface_hub import (
|
|
12 |
|
13 |
|
14 |
class SpaceBuilder:
|
15 |
-
error_message =
|
|
|
16 |
|
17 |
@classmethod
|
18 |
def file_as_a_string(cls, name_list: List[str]) -> str:
|
@@ -30,20 +31,60 @@ class SpaceBuilder:
|
|
30 |
)
|
31 |
|
32 |
@classmethod
|
33 |
-
def
|
34 |
"""
|
35 |
-
|
36 |
|
37 |
-
:param
|
38 |
-
:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
"""
|
40 |
name_list = names.split("\n")
|
41 |
-
|
|
|
|
|
|
|
|
|
42 |
if not cls.control_input_and_output_types(interfaces):
|
43 |
-
|
44 |
-
return
|
45 |
else:
|
46 |
-
return
|
47 |
|
48 |
@classmethod
|
49 |
def control_input_and_output_types(cls, interface_list: List["gr.Interface"]) -> bool:
|
@@ -92,22 +133,30 @@ class SpaceBuilder:
|
|
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
|
96 |
|
97 |
@staticmethod
|
98 |
-
def build_space(
|
99 |
"""
|
100 |
Creates a space with given inputs
|
101 |
-
:param
|
102 |
:param hf_token:
|
103 |
-
:param
|
104 |
:param space_description:
|
105 |
:return:
|
106 |
"""
|
107 |
-
if
|
108 |
return "Please fill all the inputs"
|
109 |
-
if SpaceBuilder.check_space_name_availability(hf_token,
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
else:
|
112 |
return SpaceBuilder.error_message
|
113 |
|
|
|
12 |
|
13 |
|
14 |
class SpaceBuilder:
|
15 |
+
error_message = None
|
16 |
+
url = None
|
17 |
|
18 |
@classmethod
|
19 |
def file_as_a_string(cls, name_list: List[str]) -> str:
|
|
|
31 |
)
|
32 |
|
33 |
@classmethod
|
34 |
+
def create_space(cls, names: str, space_name: str, hf_token: str) -> bool:
|
35 |
"""
|
36 |
+
Creates the space.
|
37 |
|
38 |
+
:param name_list: Input space name_list
|
39 |
+
:param space_name: Target space_name
|
40 |
+
:param hf_token: HuggingFace Write Token
|
41 |
+
:return: True if success
|
42 |
+
"""
|
43 |
+
name_list = names.split("\n")
|
44 |
+
create_repo(name=space_name, token=hf_token, repo_type="space", space_sdk="gradio")
|
45 |
+
repo = get_full_repo_name(model_id=space_name, token=hf_token)
|
46 |
+
|
47 |
+
try:
|
48 |
+
file_string = cls.file_as_a_string(name_list)
|
49 |
+
temp_file = open("temp_file.txt", "w")
|
50 |
+
temp_file.write(file_string)
|
51 |
+
temp_file.close()
|
52 |
+
except Exception as ex:
|
53 |
+
cls.error_message = "An exception occurred during temporary file writing"
|
54 |
+
return False
|
55 |
+
|
56 |
+
try:
|
57 |
+
cls.url = upload_file(
|
58 |
+
path_or_fileobj="temp_file.txt",
|
59 |
+
path_in_repo="app.py",
|
60 |
+
repo_id=repo,
|
61 |
+
repo_type="space",
|
62 |
+
token=hf_token,
|
63 |
+
)
|
64 |
+
return True
|
65 |
+
except Exception as ex:
|
66 |
+
cls.error_message = "An exception occurred during writing app.py to the target space"
|
67 |
+
return False
|
68 |
+
|
69 |
+
@classmethod
|
70 |
+
def load_and_check_spaces(cls, names: str) -> bool:
|
71 |
+
"""
|
72 |
+
Loads given space inputs as interfaces and checks whether if they are loadable.
|
73 |
+
|
74 |
+
:param names: Input space names
|
75 |
+
:return: True check is successful
|
76 |
"""
|
77 |
name_list = names.split("\n")
|
78 |
+
try:
|
79 |
+
interfaces = [gr.Interface.load(name) for name in name_list]
|
80 |
+
except Exception as ex:
|
81 |
+
cls.error_message = "One of the given models cannot be loaded to gradio, sorry for the inconvenience"
|
82 |
+
return False
|
83 |
if not cls.control_input_and_output_types(interfaces):
|
84 |
+
cls.error_message = "Spaces have different input or output types, could not combine them!"
|
85 |
+
return False
|
86 |
else:
|
87 |
+
return True
|
88 |
|
89 |
@classmethod
|
90 |
def control_input_and_output_types(cls, interface_list: List["gr.Interface"]) -> bool:
|
|
|
133 |
cls.error_message = "The the username/space_name is already used for the given token's user"
|
134 |
return False
|
135 |
else:
|
136 |
+
return False
|
137 |
|
138 |
@staticmethod
|
139 |
+
def build_space(space_names: str, hf_token: str, target_space_name: str, space_description: str) -> str:
|
140 |
"""
|
141 |
Creates a space with given inputs
|
142 |
+
:param space_names:
|
143 |
:param hf_token:
|
144 |
+
:param target_space_name:
|
145 |
:param space_description:
|
146 |
:return:
|
147 |
"""
|
148 |
+
if space_names == "" or hf_token == "" or target_space_name == "" or space_description == "":
|
149 |
return "Please fill all the inputs"
|
150 |
+
if SpaceBuilder.check_space_name_availability(hf_token, target_space_name):
|
151 |
+
print("The space name is available")
|
152 |
+
if SpaceBuilder.load_and_check_spaces(names=space_names):
|
153 |
+
print("Loaded and checked input spaces")
|
154 |
+
if SpaceBuilder.create_space(names=space_names, space_name=target_space_name, hf_token=hf_token):
|
155 |
+
return SpaceBuilder.url
|
156 |
+
else:
|
157 |
+
return SpaceBuilder.error_message
|
158 |
+
else:
|
159 |
+
return SpaceBuilder.error_message
|
160 |
else:
|
161 |
return SpaceBuilder.error_message
|
162 |
|