File size: 5,701 Bytes
f93691f
 
 
59654fb
f93691f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59654fb
f93691f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59654fb
 
 
 
 
 
 
 
f93691f
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# /// script
# requires-python = ">=3.10"
# dependencies = [
#     "huggingface-hub>=1.9.0",
# ]
# ///

"""Deploy an Embedding Atlas Space from bucket data.

Reads atlas-config.json from the bucket to generate the right Dockerfile,
creates a Docker Space, and prints instructions to mount the bucket.

Examples:

    # Deploy from existing atlas build in a bucket
    uv run atlas-deploy.py \\
        --name my-atlas \\
        --bucket user/atlas-data \\
        --space-id user/my-atlas-space

    # With custom Space hardware
    uv run atlas-deploy.py \\
        --name my-atlas \\
        --bucket user/atlas-data \\
        --space-id user/my-atlas-space \\
        --hardware cpu-upgrade
"""

import argparse
import json
import os

from huggingface_hub import HfApi, Volume, create_repo, upload_file


DOCKERFILE_TEMPLATE = """FROM python:3.12-slim

RUN useradd -m -u 1000 user
RUN pip install --no-cache-dir "embedding-atlas>=0.19.1"

USER user
EXPOSE 7860

CMD ["embedding-atlas", \\
     "/data/{name}/data/dataset.parquet", \\
     "--text", "{text_column}", \\
     "--x", "projection_x", \\
     "--y", "projection_y", \\
     "--disable-projection", \\
     "--duckdb", "server", \\
     "--host", "0.0.0.0", \\
     "--port", "7860"]
"""

README_TEMPLATE = """---
title: {title}
emoji: 🗺️
colorFrom: blue
colorTo: purple
sdk: docker
pinned: false
---

# 🗺️ {title}

Interactive embedding visualization of {sample_desc}.

Built with [HF Jobs](https://huggingface.co/docs/hub/jobs) + [Storage Buckets](https://huggingface.co/docs/hub/storage-buckets) + [Embedding Atlas](https://github.com/apple/embedding-atlas).

## How it works

- **Data**: Stored in a Storage Bucket (mounted read-only)
- **Server**: embedding-atlas in server mode with DuckDB
- **Build**: GPU UMAP via cuml.accel ({build_info})

## Features

- Interactive scatter plot with WebGPU acceleration
- Real-time search and filtering
- SQL queries via DuckDB server mode
- Click points to see details
"""


def main():
    parser = argparse.ArgumentParser(description="Deploy an Atlas Space from bucket data")
    parser.add_argument("--name", required=True, help="Atlas name (subdirectory in bucket)")
    parser.add_argument("--bucket", required=True, help="Data bucket ID (e.g. user/atlas-data)")
    parser.add_argument("--space-id", default=None, help="Space ID (default: {user}/{name})")
    parser.add_argument("--hardware", default="cpu-basic", help="Space hardware (default: cpu-basic)")
    parser.add_argument("--text-column", default=None, help="Override text column (reads from config if not set)")
    parser.add_argument("--private", action="store_true", help="Make Space private")
    args = parser.parse_args()

    api = HfApi()

    # Resolve space ID
    if args.space_id is None:
        user = api.whoami()["name"]
        args.space_id = f"{user}/{args.name}"

    # Try to read config from bucket
    text_column = args.text_column or "text"
    sample_desc = "dataset"
    build_info = ""

    try:
        from huggingface_hub import download_bucket_files
        import tempfile

        with tempfile.TemporaryDirectory() as tmp:
            config_remote = f"{args.name}/atlas-config.json"
            config_local = os.path.join(tmp, "atlas-config.json")
            download_bucket_files(args.bucket, files=[(config_remote, config_local)])

            with open(config_local) as f:
                config = json.load(f)

            text_column = config.get("text_column", text_column)
            sample = config.get("sample")
            build_time = config.get("build_time_seconds")
            gpu = config.get("gpu_info", {}).get("gpu", "")

            if sample:
                sample_desc = f"{sample:,} samples"
            if build_time and gpu:
                build_info = f"{build_time:.0f}s on {gpu}"
            elif build_time:
                build_info = f"{build_time:.0f}s"

            print(f"Read config from bucket: text_column={text_column}, sample={sample}")
    except Exception as e:
        print(f"Could not read atlas-config.json from bucket: {e}")
        print(f"Using defaults: text_column={text_column}")

    if args.text_column:
        text_column = args.text_column

    # Create Space
    print(f"\nCreating Space: {args.space_id}")
    create_repo(
        args.space_id,
        repo_type="space",
        space_sdk="docker",
        private=args.private,
        exist_ok=True,
    )

    # Generate and upload Dockerfile
    dockerfile = DOCKERFILE_TEMPLATE.format(name=args.name, text_column=text_column)
    upload_file(
        path_or_fileobj=dockerfile.encode(),
        path_in_repo="Dockerfile",
        repo_id=args.space_id,
        repo_type="space",
    )

    # Generate and upload README
    title = args.name.replace("-", " ").replace("_", " ").title()
    readme = README_TEMPLATE.format(
        title=title,
        sample_desc=sample_desc,
        build_info=build_info,
    )
    upload_file(
        path_or_fileobj=readme.encode(),
        path_in_repo="README.md",
        repo_id=args.space_id,
        repo_type="space",
    )

    # Set hardware
    if args.hardware != "cpu-basic":
        api.request_space_hardware(args.space_id, args.hardware)
        print(f"Hardware: {args.hardware}")

    # Mount bucket volume
    api.set_space_volumes(
        args.space_id,
        volumes=[
            Volume(type="bucket", source=args.bucket, mount_path="/data", read_only=True),
        ],
    )
    print(f"Bucket mounted: {args.bucket} -> /data (read-only)")

    space_url = f"https://huggingface.co/spaces/{args.space_id}"
    print(f"\nSpace deployed: {space_url}")


if __name__ == "__main__":
    main()