Spaces:
Sleeping
Sleeping
Update build_logic.py
Browse files- build_logic.py +51 -17
build_logic.py
CHANGED
@@ -14,9 +14,9 @@ from huggingface_hub import (
|
|
14 |
delete_file as hf_delete_file,
|
15 |
HfApi
|
16 |
)
|
17 |
-
# More direct import for CommitOperationDelete in case of re-export issues
|
18 |
from huggingface_hub.hf_api import CommitOperationDelete
|
19 |
-
from huggingface_hub.utils
|
|
|
20 |
|
21 |
# Setup basic logging
|
22 |
logging.basicConfig(
|
@@ -139,18 +139,41 @@ def get_space_repository_info(ui_api_token_from_textbox, space_name_ui, owner_ui
|
|
139 |
files = [sibling.rfilename for sibling in repo_info_obj.siblings if sibling.rfilename]
|
140 |
if not files and repo_info_obj.siblings:
|
141 |
logger.warning(f"Repo {repo_id} has siblings but no rfilenames extracted.")
|
142 |
-
except
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
logger.warning(f"Could not get full repo_info for {repo_id_for_error_logging}, falling back to list_repo_files: {e}")
|
144 |
try:
|
145 |
resolved_api_token_fb, token_err_fb = _get_api_token(ui_api_token_from_textbox)
|
146 |
-
if token_err_fb: return None, None, token_err_fb
|
147 |
repo_id_fb, err_repo_id_fb = _determine_repo_id(ui_api_token_from_textbox, space_name_ui, owner_ui)
|
148 |
-
if err_repo_id_fb: return None, None, err_repo_id_fb
|
149 |
files = list_repo_files(repo_id=repo_id_fb, token=resolved_api_token_fb, repo_type="space")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
except Exception as e2:
|
151 |
logger.exception(f"Error listing files for {repo_id_for_error_logging} during fallback: {e2}")
|
152 |
error = f"Error listing files for `{repo_id_for_error_logging}`: {str(e2)}"
|
153 |
|
|
|
154 |
if not files and not error:
|
155 |
error = f"No files found in Space `{repo_id_for_error_logging}` (or an issue fetching them)."
|
156 |
return sdk, files, error
|
@@ -175,9 +198,13 @@ def get_space_file_content(ui_api_token_from_textbox, space_name_ui, owner_ui, f
|
|
175 |
downloaded_file_path = hf_hub_download(repo_id=repo_id, filename=file_path_in_repo, repo_type="space", token=resolved_api_token)
|
176 |
content = Path(downloaded_file_path).read_text(encoding="utf-8")
|
177 |
return content, None
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
except Exception as e:
|
179 |
-
if "404" in str(e) or "not found" in str(e).lower() or "does not exist" in str(e).lower():
|
180 |
-
return None, f"Error: File '{file_path_in_repo}' not found in Space '{repo_id_for_error_logging}'."
|
181 |
logger.exception(f"Error fetching file content for {file_path_in_repo} from {repo_id_for_error_logging}:")
|
182 |
return None, f"Error fetching file content: {str(e)}"
|
183 |
|
@@ -217,6 +244,7 @@ def create_space(ui_api_token_from_textbox, space_name_ui, owner_ui, sdk_ui, mar
|
|
217 |
files_to_delete_on_hub = list(current_hub_files - markdown_filenames)
|
218 |
files_to_delete_on_hub = [f for f in files_to_delete_on_hub if not (f.startswith('.git') or (f == "README.md" and "README.md" not in markdown_filenames))]
|
219 |
|
|
|
220 |
if files_to_delete_on_hub:
|
221 |
logger.info(f"Deleting {len(files_to_delete_on_hub)} files from {repo_id} not in new markdown: {files_to_delete_on_hub}")
|
222 |
delete_operations = [CommitOperationDelete(path_in_repo=f) for f in files_to_delete_on_hub]
|
@@ -275,9 +303,13 @@ def delete_space_file(ui_api_token_from_textbox, space_name_ui, owner_ui, file_p
|
|
275 |
effective_commit_message = commit_message_ui or f"Deleted file: {file_path_in_repo} via AI Space Editor"
|
276 |
hf_delete_file(path_in_repo=file_path_in_repo, repo_id=repo_id, repo_type="space", token=resolved_api_token, commit_message=effective_commit_message)
|
277 |
return f"Successfully deleted file: {file_path_in_repo}"
|
|
|
|
|
|
|
|
|
|
|
|
|
278 |
except Exception as e:
|
279 |
-
if "404" in str(e) or "not found" in str(e).lower() or "does not exist" in str(e).lower():
|
280 |
-
return f"Error: File '{file_path_in_repo}' not found in Space '{repo_id_for_error_logging}' for deletion."
|
281 |
logger.exception(f"Error deleting file {file_path_in_repo} from {repo_id_for_error_logging}:")
|
282 |
return f"Error deleting file '{file_path_in_repo}': {str(e)}"
|
283 |
|
@@ -293,7 +325,7 @@ def get_space_runtime_status(ui_api_token_from_textbox, space_name_ui, owner_ui)
|
|
293 |
api = HfApi(token=resolved_api_token)
|
294 |
logger.info(f"Fetching runtime status for Space: {repo_id}")
|
295 |
|
296 |
-
runtime_info = api.get_space_runtime(repo_id=repo_id)
|
297 |
|
298 |
status_details = {
|
299 |
"stage": runtime_info.stage,
|
@@ -318,13 +350,15 @@ def get_space_runtime_status(ui_api_token_from_textbox, space_name_ui, owner_ui)
|
|
318 |
|
319 |
logger.info(f"Runtime status for {repo_id}: {status_details['stage']}")
|
320 |
return status_details, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
321 |
except Exception as e:
|
322 |
-
# Check for specific Hugging Face Hub exceptions if available, otherwise generic check
|
323 |
-
if "SpaceNotFoundError" in str(type(e)) or "404" in str(e):
|
324 |
-
logger.warning(f"Space not found or no runtime info: {repo_id_for_error_logging} - {e}")
|
325 |
-
return None, f"Error: Space '{repo_id_for_error_logging}' not found or has no active runtime."
|
326 |
-
elif "GatedRepoError" in str(type(e)) or "must be authenticated" in str(e).lower():
|
327 |
-
logger.warning(f"Access denied for Space runtime: {repo_id_for_error_logging} - {e}")
|
328 |
-
return None, f"Error: Access denied or authentication required for '{repo_id_for_error_logging}'."
|
329 |
logger.exception(f"Error fetching runtime status for {repo_id_for_error_logging}:")
|
330 |
return None, f"Error fetching runtime status: {str(e)}"
|
|
|
14 |
delete_file as hf_delete_file,
|
15 |
HfApi
|
16 |
)
|
|
|
17 |
from huggingface_hub.hf_api import CommitOperationDelete
|
18 |
+
# Import the general HTTP error from huggingface_hub.utils
|
19 |
+
from huggingface_hub.utils import HfHubHTTPError # For catching specific HF HTTP errors
|
20 |
|
21 |
# Setup basic logging
|
22 |
logging.basicConfig(
|
|
|
139 |
files = [sibling.rfilename for sibling in repo_info_obj.siblings if sibling.rfilename]
|
140 |
if not files and repo_info_obj.siblings:
|
141 |
logger.warning(f"Repo {repo_id} has siblings but no rfilenames extracted.")
|
142 |
+
except HfHubHTTPError as e_http: # Catch specific HF HTTP errors first
|
143 |
+
logger.error(f"HTTP error getting repo info for {repo_id_for_error_logging}: {e_http}")
|
144 |
+
error_message = str(e_http)
|
145 |
+
if e_http.response is not None: # Check if response object exists
|
146 |
+
if e_http.response.status_code == 404:
|
147 |
+
error = f"Space '{repo_id_for_error_logging}' not found (404)."
|
148 |
+
elif e_http.response.status_code in:
|
149 |
+
error = f"Access denied for '{repo_id_for_error_logging}' (401/403). Check token permissions."
|
150 |
+
else:
|
151 |
+
error = f"HTTP Error {e_http.response.status_code} for '{repo_id_for_error_logging}': {error_message}"
|
152 |
+
else: # No response object, use generic message
|
153 |
+
error = f"Network or HTTP Error for '{repo_id_for_error_logging}': {error_message}"
|
154 |
+
except Exception as e: # Catch other general exceptions
|
155 |
logger.warning(f"Could not get full repo_info for {repo_id_for_error_logging}, falling back to list_repo_files: {e}")
|
156 |
try:
|
157 |
resolved_api_token_fb, token_err_fb = _get_api_token(ui_api_token_from_textbox)
|
158 |
+
if token_err_fb: return None, None, token_err_fb # Propagate error
|
159 |
repo_id_fb, err_repo_id_fb = _determine_repo_id(ui_api_token_from_textbox, space_name_ui, owner_ui)
|
160 |
+
if err_repo_id_fb: return None, None, err_repo_id_fb # Propagate error
|
161 |
files = list_repo_files(repo_id=repo_id_fb, token=resolved_api_token_fb, repo_type="space")
|
162 |
+
except HfHubHTTPError as e2_http:
|
163 |
+
logger.error(f"HTTP error during fallback list_repo_files for {repo_id_for_error_logging}: {e2_http}")
|
164 |
+
error_message_fb = str(e2_http)
|
165 |
+
if e2_http.response is not None:
|
166 |
+
if e2_http.response.status_code == 404:
|
167 |
+
error = f"Space '{repo_id_for_error_logging}' not found during fallback (404)."
|
168 |
+
else:
|
169 |
+
error = f"HTTP Error {e2_http.response.status_code} for '{repo_id_for_error_logging}' during fallback: {error_message_fb}"
|
170 |
+
else:
|
171 |
+
error = f"Network or HTTP Error for '{repo_id_for_error_logging}' during fallback: {error_message_fb}"
|
172 |
except Exception as e2:
|
173 |
logger.exception(f"Error listing files for {repo_id_for_error_logging} during fallback: {e2}")
|
174 |
error = f"Error listing files for `{repo_id_for_error_logging}`: {str(e2)}"
|
175 |
|
176 |
+
|
177 |
if not files and not error:
|
178 |
error = f"No files found in Space `{repo_id_for_error_logging}` (or an issue fetching them)."
|
179 |
return sdk, files, error
|
|
|
198 |
downloaded_file_path = hf_hub_download(repo_id=repo_id, filename=file_path_in_repo, repo_type="space", token=resolved_api_token)
|
199 |
content = Path(downloaded_file_path).read_text(encoding="utf-8")
|
200 |
return content, None
|
201 |
+
except HfHubHTTPError as e_http:
|
202 |
+
logger.error(f"HTTP error fetching file {file_path_in_repo} from {repo_id_for_error_logging}: {e_http}")
|
203 |
+
error_message = str(e_http)
|
204 |
+
if e_http.response is not None and e_http.response.status_code == 404:
|
205 |
+
return None, f"Error: File '{file_path_in_repo}' not found in Space '{repo_id_for_error_logging}' (404)."
|
206 |
+
return None, f"HTTP Error fetching file '{file_path_in_repo}': {error_message}"
|
207 |
except Exception as e:
|
|
|
|
|
208 |
logger.exception(f"Error fetching file content for {file_path_in_repo} from {repo_id_for_error_logging}:")
|
209 |
return None, f"Error fetching file content: {str(e)}"
|
210 |
|
|
|
244 |
files_to_delete_on_hub = list(current_hub_files - markdown_filenames)
|
245 |
files_to_delete_on_hub = [f for f in files_to_delete_on_hub if not (f.startswith('.git') or (f == "README.md" and "README.md" not in markdown_filenames))]
|
246 |
|
247 |
+
|
248 |
if files_to_delete_on_hub:
|
249 |
logger.info(f"Deleting {len(files_to_delete_on_hub)} files from {repo_id} not in new markdown: {files_to_delete_on_hub}")
|
250 |
delete_operations = [CommitOperationDelete(path_in_repo=f) for f in files_to_delete_on_hub]
|
|
|
303 |
effective_commit_message = commit_message_ui or f"Deleted file: {file_path_in_repo} via AI Space Editor"
|
304 |
hf_delete_file(path_in_repo=file_path_in_repo, repo_id=repo_id, repo_type="space", token=resolved_api_token, commit_message=effective_commit_message)
|
305 |
return f"Successfully deleted file: {file_path_in_repo}"
|
306 |
+
except HfHubHTTPError as e_http: # Catch specific HF HTTP errors
|
307 |
+
logger.error(f"HTTP error deleting file {file_path_in_repo} from {repo_id_for_error_logging}: {e_http}")
|
308 |
+
error_message = str(e_http)
|
309 |
+
if e_http.response is not None and e_http.response.status_code == 404:
|
310 |
+
return f"Error: File '{file_path_in_repo}' not found in Space '{repo_id_for_error_logging}' for deletion (404)."
|
311 |
+
return f"HTTP Error deleting file '{file_path_in_repo}': {error_message}"
|
312 |
except Exception as e:
|
|
|
|
|
313 |
logger.exception(f"Error deleting file {file_path_in_repo} from {repo_id_for_error_logging}:")
|
314 |
return f"Error deleting file '{file_path_in_repo}': {str(e)}"
|
315 |
|
|
|
325 |
api = HfApi(token=resolved_api_token)
|
326 |
logger.info(f"Fetching runtime status for Space: {repo_id}")
|
327 |
|
328 |
+
runtime_info = api.get_space_runtime(repo_id=repo_id)
|
329 |
|
330 |
status_details = {
|
331 |
"stage": runtime_info.stage,
|
|
|
350 |
|
351 |
logger.info(f"Runtime status for {repo_id}: {status_details['stage']}")
|
352 |
return status_details, None
|
353 |
+
except HfHubHTTPError as e_http: # Catch specific HF HTTP errors
|
354 |
+
logger.error(f"HTTP error fetching runtime status for {repo_id_for_error_logging}: {e_http}")
|
355 |
+
error_message = str(e_http)
|
356 |
+
if e_http.response is not None:
|
357 |
+
if e_http.response.status_code == 404:
|
358 |
+
return None, f"Error: Space '{repo_id_for_error_logging}' not found or has no active runtime (404)."
|
359 |
+
elif e_http.response.status_code in:
|
360 |
+
return None, f"Error: Access denied or authentication required for '{repo_id_for_error_logging}' ({e_http.response.status_code})."
|
361 |
+
return None, f"HTTP Error fetching runtime status for '{repo_id_for_error_logging}': {error_message}"
|
362 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
363 |
logger.exception(f"Error fetching runtime status for {repo_id_for_error_logging}:")
|
364 |
return None, f"Error fetching runtime status: {str(e)}"
|