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()