File size: 1,782 Bytes
86d4cbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import io
from huggingface_hub import HfApi
from huggingface_hub import create_repo, upload_folder, whoami, upload_file

def deploy_workflow(
    name: str,
    app_code: str,
    requirements_file: str,
    hf_access_token: str,
):
    print("Syncing with Hugging Face Spaces...")

    api = HfApi()
    repo_id = f"{whoami(token=hf_access_token)['name']}/{name}"

    print("repo_id", repo_id)

    url = create_repo(
        repo_id,
        token=hf_access_token,
        exist_ok=True,
        repo_type='space',
        space_sdk='gradio',
        private=False,
        space_hardware="zero-a10g",
    )
    api.add_space_secret(repo_id=repo_id, key="HF_TOKEN", value=hf_access_token, token=hf_access_token)

    # # Sync folder
    commit_url = upload_folder(
        folder_path='.', 
        repo_id=repo_id,
        repo_type='space',
        token=hf_access_token,
        commit_message="Synced repo using 'sync_with_huggingface' Github Action",
        ignore_patterns=["*.git*", "*README.md*", "*env*", '*.cache*', '*ComfyUI-Manager*', '*ComfyUI-to-Python-Extension*', 'models/*'],
    )

    upload_file(
        path_or_fileobj=io.BytesIO(requirements_file.encode("utf-8")),
        path_in_repo='requirements.txt',
        repo_id=repo_id,
        repo_type='space',
        token=hf_access_token,
        commit_message="Uploaded requirements.txt file",
    )

    upload_file(
        path_or_fileobj=io.BytesIO(app_code.encode("utf-8")),
        path_in_repo='app.py',
        repo_id=repo_id,
        repo_type='space',
        token=hf_access_token,
        commit_message="Uploaded app.py file",
    )

    print(f"\t- Repo synced: {commit_url}")

    return {
        "url": url,
        "repo_id": repo_id,
        "commit_url": commit_url
    }