File size: 3,173 Bytes
fe3046d
 
 
 
 
 
 
 
 
 
 
 
 
5a2c6b2
fe3046d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import secrets

from huggingface_hub import HfApi, SpaceHardware, Volume


def main() -> None:
    parser = argparse.ArgumentParser(
        description="Create/upload/provision the RCA Label Studio Docker Space."
    )
    parser.add_argument("--space-id", required=True, help="Space repo id, e.g. namespace/space-name.")
    parser.add_argument("--bucket-id", required=True, help="HF bucket id, e.g. namespace/label-studio-data.")
    parser.add_argument("--space-dir", default="label_studio_space", help="Local Space directory to upload.")
    parser.add_argument("--private", action="store_true", default=False, help="Create the Space as private.")
    parser.add_argument("--admin-email", default="", help="Optional Label Studio admin email.")
    parser.add_argument("--admin-password", default="", help="Optional Label Studio admin password.")
    parser.add_argument("--admin-token", default="", help="Optional stable Label Studio legacy API token.")
    parser.add_argument("--secret-key", default="", help="Stable Django SECRET_KEY. Generated if omitted.")
    parser.add_argument("--hardware", default="", help="Optional Space hardware enum, e.g. cpu-basic.")
    args = parser.parse_args()

    api = HfApi()
    secret_key = args.secret_key or secrets.token_urlsafe(48)
    admin_email = args.admin_email or "admin@example.com"
    admin_password = args.admin_password or secrets.token_urlsafe(18)
    admin_token = args.admin_token or secrets.token_urlsafe(32)

    api.create_repo(
        repo_id=args.space_id,
        repo_type="space",
        space_sdk="docker",
        private=args.private,
        exist_ok=True,
    )
    api.create_bucket(args.bucket_id, private=True, exist_ok=True)

    if args.hardware:
        api.request_space_hardware(args.space_id, hardware=SpaceHardware(args.hardware))

    api.upload_folder(
        repo_id=args.space_id,
        repo_type="space",
        folder_path=args.space_dir,
        commit_message="Deploy RCA Label Studio annotation Space",
    )

    api.set_space_volumes(
        args.space_id,
        volumes=[
            Volume(type="bucket", source=args.bucket_id, mount_path="/data"),
        ],
    )

    api.add_space_variable(args.space_id, "LABEL_STUDIO_BASE_DATA_DIR", "/data")
    api.add_space_variable(args.space_id, "STORAGE_PERSISTENCE", "1")
    api.add_space_variable(args.space_id, "LABEL_STUDIO_ENABLE_LEGACY_API_TOKEN", "true")
    api.add_space_variable(args.space_id, "LABEL_STUDIO_DISABLE_SIGNUP_WITHOUT_LINK", "true")

    api.add_space_secret(args.space_id, "SECRET_KEY", secret_key)
    api.add_space_secret(args.space_id, "LABEL_STUDIO_USERNAME", admin_email)
    api.add_space_secret(args.space_id, "LABEL_STUDIO_PASSWORD", admin_password)
    api.add_space_secret(args.space_id, "LABEL_STUDIO_USER_TOKEN", admin_token)

    api.restart_space(args.space_id, factory_reboot=True)
    print(f"Deployed and restarted https://huggingface.co/spaces/{args.space_id}")
    print(f"Label Studio username: {admin_email}")
    print(f"Label Studio password: {admin_password}")
    print(f"Label Studio API token: {admin_token}")


if __name__ == "__main__":
    main()