File size: 2,092 Bytes
8905b6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
def upload_to_hf_dataset(file_path, dataset_name, token, repo_type="dataset"):
    """
    Upload a file to a Hugging Face dataset repository.

    Args:
        file_path (str): Path to the file to upload
        dataset_name (str): Name of the dataset in format 'username/dataset-name'
        token (str): Hugging Face API token
        repo_type (str): Repository type, defaults to 'dataset'
    """
    from huggingface_hub import HfApi
    import os

    # Initialize the Hugging Face API client
    api = HfApi()

    try:
        # Upload the file to the dataset repository
        api.upload_file(
            path_or_fileobj=file_path,
            path_in_repo=os.path.basename(file_path),  # Use filename as path in repo
            repo_id=dataset_name,
            repo_type=repo_type,
            token=token,
            commit_message=f"Upload {os.path.basename(file_path)}",
            commit_description=f"Automated upload of {os.path.basename(file_path)} to dataset"
        )
        print(f"Successfully uploaded {file_path} to {dataset_name}")
    except Exception as e:
        print(f"Error uploading file: {str(e)}")


def download_from_hf_dataset(file_path, dataset_name, token, repo_type="dataset"):
    """
    Download a file from a Hugging Face dataset repository.

    Args:
        file_path (str): Path in the repository to download from
        dataset_name (str): Name of the dataset in format 'username/dataset-name'
        token (str): Hugging Face API token
        repo_type (str): Repository type, defaults to 'dataset'
    """
    from huggingface_hub import HfApi
    import os

    # Initialize the Hugging Face API client
    api = HfApi()

    try:
        # Download the file from the dataset repository
        api.hf_hub_download(
            repo_id=dataset_name,
            filename=file_path,
            repo_type=repo_type,
            local_dir=".",
            token=token
        )
        print(f"Successfully downloaded {file_path} from {dataset_name}")
    except Exception as e:
        print(f"Error downloading file: {str(e)}")