Spaces:
Sleeping
Sleeping
File size: 3,333 Bytes
a619cad 00e9206 a1eb2df 7c4b13d faac2e1 e9d2131 7c4b13d 00e9206 7c4b13d fa8d129 00e9206 312e934 e9d2131 312e934 e9d2131 312e934 597c491 00e9206 a1eb2df 597c491 e9d2131 597c491 00e9206 312e934 597c491 00e9206 597c491 fa8d129 e9d2131 fa8d129 7c4b13d 00e9206 7c4b13d 00e9206 7c4b13d 00e9206 4a8e6b0 e9d2131 00e9206 8a4ba12 e9d2131 00e9206 a1eb2df 00e9206 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
import urllib.parse
import requests
import io
import json
import logging
# Set up logging
logging.basicConfig(level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s')
def download_gitlab_repo_to_hfspace(api_url, project_id, version, target_folder, hf_api, hf_space_name):
try:
# Construct the URL for the release's zip file
encoded_project_id = urllib.parse.quote(project_id, safe="")
url = f"{api_url}/projects/{encoded_project_id}/repository/archive.zip?sha={version}"
print("URL: ", url)
logging.info(f"Constructed URL: {url}")
# Send GET request to download the zip file
response = requests.get(url)
logging.info(f"HTTP GET Request sent to {url}, Status Code: {response.status_code}")
if response.status_code == 200:
content_disposition = response.headers.get('content-disposition')
print("Content_disposition:", content_disposition)
if content_disposition:
filename = content_disposition.split('filename=')[-1].strip('\"')
print("FILENAME: ", filename)
else:
filename = 'archive.zip' # Fallback to a default name if not found
logging.info(f"Filename determined: {filename}")
else:
logging.error(f"Failed to download the file. HTTP Status: {response.status_code}")
return
existing_files = hf_api.list_repo_files(repo_id=hf_space_name, repo_type='space')
target_path = f"{target_folder}/{filename}"
print("TARGET PATH: ", target_path)
logging.info(f"Target Path: '{target_path}'")
logging.info(f"Existing Files: {[repr(file) for file in existing_files]}")
if target_path in existing_files:
logging.warning(f"File '{target_path}' already exists in the repository. Skipping upload...")
print(f"File '{target_path}' already exists in the repository. Skipping upload...")
else:
repo_content= io.BytesIO(response.content)
print(repo_content)
_upload_file_to_hfspace(repo_content, hf_api, target_path, hf_space_name)
except FileNotFoundError:
logging.error("The config.json file was not found. Please ensure it exists in the project directory.")
print("The config.json file was not found. Please ensure it exists in the project directory.")
except json.JSONDecodeError:
logging.error("Failed to parse the config.json file. Please ensure it contains valid JSON.")
print("Failed to parse the config.json file. Please ensure it contains valid JSON.")
except Exception as e:
logging.error(f"An unexpected error occurred: {e}")
print(f"An unexpected error occurred: {e}")
def _upload_file_to_hfspace(content, hf_api, target_path, hf_space_name):
try:
logging.info(f"Preparing to upload file to '{target_path}'")
hf_api.upload_file(
path_or_fileobj=content,
path_in_repo=target_path,
repo_id=hf_space_name,
repo_type="space"
)
logging.info(f"File successfully uploaded to '{target_path}'")
except Exception as e:
logging.error(f"Error during file upload: {e}")
print(f"Error during file upload: {e}")
|