File size: 1,307 Bytes
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
51
52
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 with content: {repo_id} on file {html_file_path}")
    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
    )

    api.upload_file(
        path_or_fileobj=html_file_path,
        path_in_repo="index.html",
        repo_type="space",
        repo_id=repo_id,
        token=token,
    )

    logging.info(f"Pushing file to hub: {dataset_id} on file {plot_file_path}")

    try:
        logging.info(f"About to push {plot_file_path} - {dataset_id}")
        api.upload_file(
            path_or_fileobj=plot_file_path,
            path_in_repo="static_plot.png",
            repo_id=repo_id,
            repo_type="space",
        )
    except Exception as e:
        logging.info("Failed to push file", e)
        raise

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