forked from fofr/cog-comfyui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
105 lines (88 loc) · 3.82 KB
/
predict.py
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
import os
import shutil
import tarfile
import zipfile
from typing import List
from cog import BasePredictor, Input, Path
from helpers.comfyui import ComfyUI
OUTPUT_DIR = "/tmp/outputs"
INPUT_DIR = "/tmp/inputs"
COMFYUI_TEMP_OUTPUT_DIR = "ComfyUI/temp"
with open("examples/photomaker.json", "r") as file:
EXAMPLE_WORKFLOW_JSON = file.read()
class Predictor(BasePredictor):
def setup(self):
self.comfyUI = ComfyUI("127.0.0.1:8188")
self.comfyUI.start_server(OUTPUT_DIR, INPUT_DIR)
def cleanup(self):
for directory in [OUTPUT_DIR, INPUT_DIR, COMFYUI_TEMP_OUTPUT_DIR]:
if os.path.exists(directory):
shutil.rmtree(directory)
os.makedirs(directory)
def handle_input_file(self, input_file: Path):
file_extension = os.path.splitext(input_file)[1]
if file_extension == ".tar":
with tarfile.open(input_file, "r") as tar:
tar.extractall(INPUT_DIR)
elif file_extension == ".zip":
with zipfile.ZipFile(input_file, "r") as zip_ref:
zip_ref.extractall(INPUT_DIR)
elif file_extension in [".jpg", ".jpeg", ".png", ".webp"]:
shutil.copy(input_file, os.path.join(INPUT_DIR, f"input{file_extension}"))
else:
raise ValueError(f"Unsupported file type: {file_extension}")
print("====================================")
print(f"Inputs uploaded to {INPUT_DIR}:")
self.log_and_collect_files(INPUT_DIR)
print("====================================")
def log_and_collect_files(self, directory, prefix=""):
files = []
for f in os.listdir(directory):
if f == "__MACOSX":
continue
path = os.path.join(directory, f)
if os.path.isfile(path):
print(f"{prefix}{f}")
files.append(Path(path))
elif os.path.isdir(path):
print(f"{prefix}{f}/")
files.extend(self.log_and_collect_files(path, prefix=f"{prefix}{f}/"))
return files
def predict(
self,
workflow_json: str = Input(
description="Your ComfyUI workflow as JSON. You must use the API version of your workflow. Get it from ComfyUI using ‘Save (API format)’. Instructions here: https://github.com/fofr/cog-comfyui",
default="",
),
input_file: Path = Input(
description="Input image, tar or zip file. Read guidance on workflows and input files here: https://github.com/fofr/cog-comfyui. Alternatively, you can replace inputs with URLs in your JSON workflow and the model will download them.",
default=None,
),
return_temp_files: bool = Input(
description="Return any temporary files, such as preprocessed controlnet images. Useful for debugging.",
default=False,
),
randomise_seeds: bool = Input(
description="Automatically randomise seeds (seed, noise_seed, rand_seed)",
default=True,
),
) -> List[Path]:
"""Run a single prediction on the model"""
self.cleanup()
if input_file:
self.handle_input_file(input_file)
# TODO: Record the previous models loaded
# If different, run /free to free up models and memory
wf = self.comfyUI.load_workflow(workflow_json or EXAMPLE_WORKFLOW_JSON)
if randomise_seeds:
self.comfyUI.randomise_seeds(wf)
self.comfyUI.connect()
self.comfyUI.run_workflow(wf)
files = []
output_directories = [OUTPUT_DIR]
if return_temp_files:
output_directories.append(COMFYUI_TEMP_OUTPUT_DIR)
for directory in output_directories:
print(f"Contents of {directory}:")
files.extend(self.log_and_collect_files(directory))
return files