omerXfaruq commited on
Commit
c500ff5
1 Parent(s): 7c0fef2

Refactoring

Browse files
Files changed (1) hide show
  1. app.py +33 -12
app.py CHANGED
@@ -81,7 +81,9 @@ class SpaceBuilder:
81
  return True
82
  except Exception as ex:
83
  print(ex)
84
- cls.error_message = "An exception occurred during writing app.py to the target space"
 
 
85
  return False
86
 
87
  @classmethod
@@ -97,38 +99,46 @@ class SpaceBuilder:
97
  interfaces = [gr.Interface.load(name) for name in name_list]
98
  except Exception as ex:
99
  print(ex)
100
- cls.error_message = "One of the given models cannot be loaded to gradio, sorry for the inconvenience"
 
 
 
101
  return False
102
  if not cls.control_input_and_output_types(interfaces):
103
- cls.error_message = "Spaces have different input or output types, could not combine them!"
104
  return False
105
  else:
106
  return True
107
 
108
  @classmethod
109
- def control_input_and_output_types(cls, interface_list: List["gr.Interface"]) -> bool:
 
 
110
  """
111
  Controls whether if input and output types of the given interfaces are the same.
112
 
113
  :param interface_list: list of interfaces
114
  :return: True if all input and output types are the same
115
  """
116
- first_input_types = [type(input) for input in interface_list[0].input_components]
 
 
117
  first_output_types = [
118
  type(output) for output in interface_list[0].output_components
119
  ]
120
  for interface in interface_list:
121
- interface_input_types = [type(input) for input in interface.input_components]
 
 
122
  if not np.all(
123
  interface_input_types == first_input_types
124
  ): # Vectorize the comparison and don't use double for loop
125
- cls.error_message = "Given spaces's input types are different"
126
  return False
127
  interface_output_types = [
128
  type(output) for output in interface.output_components
129
  ]
130
  if not np.all(interface_output_types == first_output_types):
131
- cls.error_message = "Given spaces's input types are different"
132
  return False
133
 
134
  return True
@@ -161,7 +171,9 @@ class SpaceBuilder:
161
  return False
162
 
163
  @staticmethod
164
- def build_space(space_names: str, hf_token: str, target_space_name: str, space_description: str) -> str:
 
 
165
  """
166
  Creates a space with given inputs
167
  :param space_names:
@@ -170,13 +182,22 @@ class SpaceBuilder:
170
  :param space_description:
171
  :return:
172
  """
173
- if space_names == "" or hf_token == "" or target_space_name == "" or space_description == "":
 
 
 
 
 
174
  return "Please fill all the inputs"
175
- if SpaceBuilder.check_space_name_availability(hf_token=hf_token, space_name=target_space_name):
 
 
176
  print("The space name is available")
177
  if SpaceBuilder.load_and_check_spaces(names=space_names):
178
  print("Loaded and checked input spaces")
179
- if SpaceBuilder.create_space(names=space_names, space_name=target_space_name, hf_token=hf_token):
 
 
180
  return SpaceBuilder.url
181
  else:
182
  return SpaceBuilder.error_message
 
81
  return True
82
  except Exception as ex:
83
  print(ex)
84
+ cls.error_message = (
85
+ "An exception occurred during writing app.py to the target space"
86
+ )
87
  return False
88
 
89
  @classmethod
 
99
  interfaces = [gr.Interface.load(name) for name in name_list]
100
  except Exception as ex:
101
  print(ex)
102
+ cls.error_message = (
103
+ f"One of the given space cannot be loaded to gradio, sorry for the inconvenience. "
104
+ f"\nPlease use different space inputs!"
105
+ )
106
  return False
107
  if not cls.control_input_and_output_types(interfaces):
 
108
  return False
109
  else:
110
  return True
111
 
112
  @classmethod
113
+ def control_input_and_output_types(
114
+ cls, interface_list: List["gr.Interface"]
115
+ ) -> bool:
116
  """
117
  Controls whether if input and output types of the given interfaces are the same.
118
 
119
  :param interface_list: list of interfaces
120
  :return: True if all input and output types are the same
121
  """
122
+ first_input_types = [
123
+ type(input) for input in interface_list[0].input_components
124
+ ]
125
  first_output_types = [
126
  type(output) for output in interface_list[0].output_components
127
  ]
128
  for interface in interface_list:
129
+ interface_input_types = [
130
+ type(input) for input in interface.input_components
131
+ ]
132
  if not np.all(
133
  interface_input_types == first_input_types
134
  ): # Vectorize the comparison and don't use double for loop
135
+ cls.error_message = "Input space input types are different"
136
  return False
137
  interface_output_types = [
138
  type(output) for output in interface.output_components
139
  ]
140
  if not np.all(interface_output_types == first_output_types):
141
+ cls.error_message = "Input space output types are different"
142
  return False
143
 
144
  return True
 
171
  return False
172
 
173
  @staticmethod
174
+ def build_space(
175
+ space_names: str, hf_token: str, target_space_name: str, space_description: str
176
+ ) -> str:
177
  """
178
  Creates a space with given inputs
179
  :param space_names:
 
182
  :param space_description:
183
  :return:
184
  """
185
+ if (
186
+ space_names == ""
187
+ or hf_token == ""
188
+ or target_space_name == ""
189
+ or space_description == ""
190
+ ):
191
  return "Please fill all the inputs"
192
+ if SpaceBuilder.check_space_name_availability(
193
+ hf_token=hf_token, space_name=target_space_name
194
+ ):
195
  print("The space name is available")
196
  if SpaceBuilder.load_and_check_spaces(names=space_names):
197
  print("Loaded and checked input spaces")
198
+ if SpaceBuilder.create_space(
199
+ names=space_names, space_name=target_space_name, hf_token=hf_token
200
+ ):
201
  return SpaceBuilder.url
202
  else:
203
  return SpaceBuilder.error_message