File size: 2,331 Bytes
856a47f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
if os.environ.get("SPACES_ZERO_GPU") is not None:
    import spaces
else:
    class spaces:
        @staticmethod
        def GPU(func):
            def wrapper(*args, **kwargs):
                return func(*args, **kwargs)
            return wrapper
import subprocess
import gc
from pathlib import Path
from utils import set_token, get_download_file

@spaces.GPU
def fake_gpu():
  pass

HF_TOKEN = os.environ.get("HF_TOKEN")
CIVITAI_API_KEY = os.environ.get("CIVITAI_API_KEY")
set_token(HF_TOKEN)
BASE_DIR = Path(__file__).resolve().parent
DATA_DIR = "data"
os.makedirs(str(Path(BASE_DIR, DATA_DIR)), exist_ok=True)

def get_file(url: str, path: str):
    print(f"Downloading {url} to {path}...")
    get_download_file(path, url, CIVITAI_API_KEY)

def git_clone(url: str, path: str, pip: bool=False, addcmd: str=""):
    os.makedirs(str(Path(BASE_DIR, path)), exist_ok=True)
    os.chdir(Path(BASE_DIR, path))
    print(f"Cloning {url} to {path}...")
    cmd = f'git clone {url}'
    print(f'Running {cmd} at {Path.cwd()}')
    i = subprocess.run(cmd, shell=True).returncode
    if i != 0: print(f'Error occured at running {cmd}')
    p = url.split("/")[-1]
    if not Path(p).exists: return
    if pip:
        os.chdir(Path(BASE_DIR, path, p))
        cmd = f'pip install -r requirements.txt'
        print(f'Running {cmd} at {Path.cwd()}')
        i = subprocess.run(cmd, shell=True).returncode
        if i != 0: print(f'Error occured at running {cmd}')
    if addcmd:
        os.chdir(Path(BASE_DIR, path, p))
        cmd = addcmd
        print(f'Running {cmd} at {Path.cwd()}')
        i = subprocess.run(cmd, shell=True).returncode
        if i != 0: print(f'Error occured at running {cmd}')

def run(cmd: str):
    print(f'Running {cmd} at {Path.cwd()}')
    i = subprocess.run(cmd, shell=True).returncode
    if i != 0: print(f'Error occured at running {cmd}')

git_clone("https://github.com/comfyanonymous/ComfyUI", ".", True)
run('pip install -U onnxruntime-gpu')

get_file("https://huggingface.co/SG161222/RealVisXL_V4.0_Lightning/resolve/main/RealVisXL_V4.0_Lightning.safetensors", "./models/checkpoints/")

run(f"python main.py --listen 0.0.0.0 --port 7860 --cpu --output-directory {DATA_DIR}")

# https://github.com/comfyanonymous/ComfyUI