File size: 2,702 Bytes
d6e8bff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3df6305
d6e8bff
 
3df6305
d6e8bff
d7086f8
 
d6e8bff
d7086f8
 
d6e8bff
 
 
 
3df6305
d6e8bff
d7086f8
 
 
 
 
d6e8bff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
78
79
80
81
82
83
84
#!/usr/bin/env python3
"""

Download application code from private HuggingFace Space

"""
import os
import sys
from huggingface_hub import snapshot_download
from pathlib import Path

def download_code(code_space_id, token):
    """

    Download app and data from the private code space



    Args:

        code_space_id: Full space ID (e.g., "mutisya/polyglot-backend-code")

        token: HuggingFace token for authentication

    """
    print(f"Downloading code from: {code_space_id}")

    try:
        # Download the entire space to a temporary directory
        download_path = snapshot_download(
            repo_id=code_space_id,
            repo_type="space",
            token=token,
            local_dir="/tmp/code_download",
            local_dir_use_symlinks=False
        )

        print(f"Code downloaded to: {download_path}")

        # Copy app and data to the correct locations
        import shutil

        # Copy app directory
        if Path("/tmp/code_download/app").exists():
            # Remove existing app directory if present
            if Path("/app/app").exists():
                shutil.rmtree("/app/app")
            # Copy the entire app directory
            shutil.copytree("/tmp/code_download/app", "/app/app")
            print("OK app/ directory copied")
        else:
            print("WARNING: app/ directory not found in code space")

        # Copy data directory
        if Path("/tmp/code_download/data").exists():
            # Remove existing data directory if present
            if Path("/app/data").exists():
                shutil.rmtree("/app/data")
            # Copy the entire data directory
            shutil.copytree("/tmp/code_download/data", "/app/data")
            print("OK data/ directory copied")
        else:
            print("WARNING: data/ directory not found in code space")

        # Clean up
        if Path("/tmp/code_download").exists():
            shutil.rmtree("/tmp/code_download")

        print("OK Code download complete")
        return True

    except Exception as e:
        print(f"ERROR downloading code: {e}")
        import traceback
        traceback.print_exc()
        return False

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python download_code.py <code_space_id> [token]")
        sys.exit(1)

    code_space_id = sys.argv[1]
    token = sys.argv[2] if len(sys.argv) > 2 else os.getenv("HUGGING_FACE_HUB_TOKEN")

    if not token:
        print("ERROR: No HuggingFace token provided")
        sys.exit(1)

    success = download_code(code_space_id, token)
    sys.exit(0 if success else 1)