bupa1018 commited on
Commit
7c4b13d
·
1 Parent(s): fbe4134

Create download_repo_to_huggingface.py

Browse files
Files changed (1) hide show
  1. download_repo_to_huggingface.py +46 -0
download_repo_to_huggingface.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def download_and_upload_kadiAPY_repo_to_huggingfacespace():
2
+ try:
3
+ # Load the configuration from config.json
4
+
5
+ # Extract GitLab project information from the config
6
+ api_url = config2['gitlab']['api_url']
7
+ project_id = urllib.parse.quote(config2['gitlab']['project']['id'], safe="")
8
+ version = config2['gitlab']['project']['version']
9
+
10
+ # Construct the URL for the release's zip file
11
+ url = f"{api_url}/projects/{project_id}/repository/archive.zip?sha={version}"
12
+
13
+ # Send GET request to download the zip file
14
+ response = requests.get(url, stream=True)
15
+
16
+ if response.status_code == 200:
17
+ extract_and_upload_file(response, api, DATA_DIR, HF_SPACE_NAME)
18
+ else:
19
+ print(f"Failed to download the release: {response.status_code} - {response.reason}")
20
+ print(response.text)
21
+
22
+ except FileNotFoundError:
23
+ print("The config.json file was not found. Please ensure it exists in the project directory.")
24
+ except json.JSONDecodeError:
25
+ print("Failed to parse the config.json file. Please ensure it contains valid JSON.")
26
+ except Exception as e:
27
+ print(f"An error occurred: {e}")
28
+
29
+
30
+
31
+
32
+ def extract_and_upload_file(response, api, DATA_DIR, HF_SPACE_NAME):
33
+ """Extracts the archive content and uploads the file."""
34
+ archive_bytes = io.BytesIO(response.content)
35
+ # Extract filename from content-disposition header
36
+ content_disposition = response.headers.get("content-disposition")
37
+ if content_disposition and "filename=" in content_disposition:
38
+ filename = content_disposition.split("filename=")[-1].strip('"')
39
+
40
+ # Upload the file
41
+ api.upload_file(
42
+ path_or_fileobj=archive_bytes,
43
+ path_in_repo=f"{DATA_DIR}/{filename}",
44
+ repo_id=HF_SPACE_NAME,
45
+ repo_type="space"
46
+ )