AmirTrader commited on
Commit
8905b6a
·
verified ·
1 Parent(s): 338bdc0

Create utils.py

Browse files
Files changed (1) hide show
  1. utils.py +60 -0
utils.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def upload_to_hf_dataset(file_path, dataset_name, token, repo_type="dataset"):
2
+ """
3
+ Upload a file to a Hugging Face dataset repository.
4
+
5
+ Args:
6
+ file_path (str): Path to the file to upload
7
+ dataset_name (str): Name of the dataset in format 'username/dataset-name'
8
+ token (str): Hugging Face API token
9
+ repo_type (str): Repository type, defaults to 'dataset'
10
+ """
11
+ from huggingface_hub import HfApi
12
+ import os
13
+
14
+ # Initialize the Hugging Face API client
15
+ api = HfApi()
16
+
17
+ try:
18
+ # Upload the file to the dataset repository
19
+ api.upload_file(
20
+ path_or_fileobj=file_path,
21
+ path_in_repo=os.path.basename(file_path), # Use filename as path in repo
22
+ repo_id=dataset_name,
23
+ repo_type=repo_type,
24
+ token=token,
25
+ commit_message=f"Upload {os.path.basename(file_path)}",
26
+ commit_description=f"Automated upload of {os.path.basename(file_path)} to dataset"
27
+ )
28
+ print(f"Successfully uploaded {file_path} to {dataset_name}")
29
+ except Exception as e:
30
+ print(f"Error uploading file: {str(e)}")
31
+
32
+
33
+ def download_from_hf_dataset(file_path, dataset_name, token, repo_type="dataset"):
34
+ """
35
+ Download a file from a Hugging Face dataset repository.
36
+
37
+ Args:
38
+ file_path (str): Path in the repository to download from
39
+ dataset_name (str): Name of the dataset in format 'username/dataset-name'
40
+ token (str): Hugging Face API token
41
+ repo_type (str): Repository type, defaults to 'dataset'
42
+ """
43
+ from huggingface_hub import HfApi
44
+ import os
45
+
46
+ # Initialize the Hugging Face API client
47
+ api = HfApi()
48
+
49
+ try:
50
+ # Download the file from the dataset repository
51
+ api.hf_hub_download(
52
+ repo_id=dataset_name,
53
+ filename=file_path,
54
+ repo_type=repo_type,
55
+ local_dir=".",
56
+ token=token
57
+ )
58
+ print(f"Successfully downloaded {file_path} from {dataset_name}")
59
+ except Exception as e:
60
+ print(f"Error downloading file: {str(e)}")