omerXfaruq commited on
Commit
8805249
1 Parent(s): 1915c6c

- refactor the function ordering

Browse files
Files changed (1) hide show
  1. app.py +78 -78
app.py CHANGED
@@ -48,82 +48,6 @@ class SpaceBuilder:
48
  f"\ngr.mix.Parallel(*interfaces, title=\"{title}\", description=\"{description}\").launch()"
49
  )
50
 
51
- @classmethod
52
- def create_space(cls, names: str, space_name: str, hf_token: str, title: str, description: str) -> bool:
53
- """
54
- Creates the space.
55
-
56
- :param names: Input space name_list
57
- :param space_name: Target space_name
58
- :param hf_token: HuggingFace Write Token
59
- :param title: Target Interface Title
60
- :param description: Target Interface Description
61
- :return: True if success
62
- """
63
- name_list = cls.split_space_names(names)
64
- try:
65
- create_repo(name=space_name, token=hf_token, repo_type="space", space_sdk="gradio")
66
- except Exception as ex:
67
- print(ex)
68
- cls.error_message = "Please provide a correct space name as Only regular characters and '-', '_', '.' accepted. '--' and '..' are forbidden. '-' and '.' cannot start or end the name."
69
- return False
70
- repo_name = get_full_repo_name(model_id=space_name, token=hf_token)
71
-
72
- try:
73
- file_string = cls.file_as_a_string(name_list, title, description)
74
- temp_file = open("temp_file.txt", "w")
75
- temp_file.write(file_string)
76
- temp_file.close()
77
- except Exception as ex:
78
- print(ex)
79
- cls.error_message = "An exception occurred during temporary file writing"
80
- return False
81
-
82
- # Sleep a litle bit otherwise the interface might not build at the space after uploading a file
83
- time.sleep(1)
84
- try:
85
- file_url = upload_file(
86
- path_or_fileobj="temp_file.txt",
87
- path_in_repo="app.py",
88
- repo_id=repo_name,
89
- repo_type="space",
90
- token=hf_token,
91
- )
92
- cls.url = f"https://huggingface.co/spaces/{repo_name}"
93
- return True
94
- except Exception as ex:
95
- print(ex)
96
- cls.error_message = (
97
- "An exception occurred during writing app.py to the target space"
98
- )
99
- return False
100
-
101
- @classmethod
102
- def load_and_check_spaces(cls, names: str) -> bool:
103
- """
104
- Loads given space inputs as interfaces and checks whether if they are loadable.
105
-
106
- :param names: Input space names
107
- :return: True check is successful
108
- """
109
- name_list = cls.split_space_names(names)
110
-
111
- try:
112
- # We could gather these interfaces in parallel if gradio was supporting async gathering. It will probably possible after the migration to the FastAPI is completed.
113
- interfaces = [gr.Interface.load(name) for name in name_list]
114
- except Exception as ex:
115
- print(ex)
116
- cls.error_message = (
117
- f"One of the given space cannot be loaded to gradio, sorry for the inconvenience. "
118
- f"\nPlease use different input space names!"
119
- )
120
- return False
121
- if not cls.control_input_and_output_types(interfaces):
122
- return False
123
- else:
124
- print("Loaded and checked input spaces, great it works!")
125
- return True
126
-
127
  @classmethod
128
  def control_input_and_output_types(
129
  cls, interface_list: List["gr.Interface"]
@@ -147,13 +71,13 @@ class SpaceBuilder:
147
  if not np.all(
148
  interface_input_types == first_input_types
149
  ): # Vectorize the comparison and don't use double for loop
150
- cls.error_message = "Input space input types are different"
151
  return False
152
  interface_output_types = [
153
  type(output) for output in interface.output_components
154
  ]
155
  if not np.all(interface_output_types == first_output_types):
156
- cls.error_message = "Input space output types are different"
157
  return False
158
 
159
  return True
@@ -187,6 +111,82 @@ class SpaceBuilder:
187
  cls.error_message = "Can not send a request to https://huggingface.co"
188
  return False
189
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
  @staticmethod
191
  def build_space(
192
  space_names: str, hf_token: str, target_space_name: str, interface_title: str, interface_description: str
 
48
  f"\ngr.mix.Parallel(*interfaces, title=\"{title}\", description=\"{description}\").launch()"
49
  )
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  @classmethod
52
  def control_input_and_output_types(
53
  cls, interface_list: List["gr.Interface"]
 
71
  if not np.all(
72
  interface_input_types == first_input_types
73
  ): # Vectorize the comparison and don't use double for loop
74
+ cls.error_message = "Provided space input types are different"
75
  return False
76
  interface_output_types = [
77
  type(output) for output in interface.output_components
78
  ]
79
  if not np.all(interface_output_types == first_output_types):
80
+ cls.error_message = "Provided space output types are different"
81
  return False
82
 
83
  return True
 
111
  cls.error_message = "Can not send a request to https://huggingface.co"
112
  return False
113
 
114
+ @classmethod
115
+ def load_and_check_spaces(cls, names: str) -> bool:
116
+ """
117
+ Loads given space inputs as interfaces and checks whether if they are loadable.
118
+
119
+ :param names: Input space names
120
+ :return: True check is successful
121
+ """
122
+ name_list = cls.split_space_names(names)
123
+
124
+ try:
125
+ # We could gather these interfaces in parallel if gradio was supporting async gathering. It will probably possible after the migration to the FastAPI is completed.
126
+ interfaces = [gr.Interface.load(name) for name in name_list]
127
+ except Exception as ex:
128
+ print(ex)
129
+ cls.error_message = (
130
+ f"One of the given space cannot be loaded to gradio, sorry for the inconvenience. "
131
+ f"\nPlease use different input space names!"
132
+ )
133
+ return False
134
+ if not cls.control_input_and_output_types(interfaces):
135
+ return False
136
+ else:
137
+ print("Loaded and checked input spaces, great it works!")
138
+ return True
139
+
140
+ @classmethod
141
+ def create_space(cls, names: str, space_name: str, hf_token: str, title: str, description: str) -> bool:
142
+ """
143
+ Creates the space.
144
+
145
+ :param names: Input space name_list
146
+ :param space_name: Target space_name
147
+ :param hf_token: HuggingFace Write Token
148
+ :param title: Target Interface Title
149
+ :param description: Target Interface Description
150
+ :return: True if success
151
+ """
152
+ name_list = cls.split_space_names(names)
153
+ try:
154
+ create_repo(name=space_name, token=hf_token, repo_type="space", space_sdk="gradio")
155
+ except Exception as ex:
156
+ print(ex)
157
+ cls.error_message = "Please provide a correct space name as Only regular characters and '-', '_', '.' accepted. '--' and '..' are forbidden. '-' and '.' cannot start or end the name."
158
+ return False
159
+ repo_name = get_full_repo_name(model_id=space_name, token=hf_token)
160
+
161
+ try:
162
+ file_string = cls.file_as_a_string(name_list, title, description)
163
+ temp_file = open("temp_file.txt", "w")
164
+ temp_file.write(file_string)
165
+ temp_file.close()
166
+ except Exception as ex:
167
+ print(ex)
168
+ cls.error_message = "An exception occurred during temporary file writing"
169
+ return False
170
+
171
+ # Sleep a little bit otherwise the interface might not build at the space after uploading a file
172
+ time.sleep(1)
173
+ try:
174
+ file_url = upload_file(
175
+ path_or_fileobj="temp_file.txt",
176
+ path_in_repo="app.py",
177
+ repo_id=repo_name,
178
+ repo_type="space",
179
+ token=hf_token,
180
+ )
181
+ cls.url = f"https://huggingface.co/spaces/{repo_name}"
182
+ return True
183
+ except Exception as ex:
184
+ print(ex)
185
+ cls.error_message = (
186
+ "An exception occurred during writing app.py to the target space"
187
+ )
188
+ return False
189
+
190
  @staticmethod
191
  def build_space(
192
  space_names: str, hf_token: str, target_space_name: str, interface_title: str, interface_description: str