File size: 1,115 Bytes
a893b55
 
 
 
 
 
 
 
 
 
 
 
 
9978925
 
a893b55
 
 
 
 
 
 
 
 
 
 
 
9978925
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a893b55
9978925
 
 
a893b55
 
 
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
import logging
from huggingface_hub import HfApi, SpaceCard


def create_space_with_content(
    api: HfApi,
    repo_id: str,
    dataset_id: str,
    html_file_path: str,
    plot_file_path: str,
    space_card: str,
    token: str,
):
    logging.info(f"Creating space {repo_id}")

    api.create_repo(
        repo_id=repo_id,
        repo_type="space",
        private=False,
        exist_ok=True,
        token=token,
        space_sdk="static",
    )
    SpaceCard(content=space_card.format(dataset_id=dataset_id)).push_to_hub(
        repo_id=repo_id, repo_type="space", token=token
    )

    upload_file(api, html_file_path, "index.html", repo_id, token)
    upload_file(api, plot_file_path, "static_plot.png", repo_id, token)

    logging.info("Space creation done")
    return repo_id


def upload_file(
    api: HfApi,
    file_path: str,
    path_in_repo: str,
    repo_id: str,
    token: str,
    repo_type: str = "space",
):
    api.upload_file(
        path_or_fileobj=file_path,
        path_in_repo=path_in_repo,
        repo_type=repo_type,
        repo_id=repo_id,
        token=token,
    )