-
Notifications
You must be signed in to change notification settings - Fork 129
/
example_predict.py
112 lines (92 loc) · 3.58 KB
/
example_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
106
107
108
109
110
111
112
# An example of how to convert a given API workflow into its own Replicate model
# Replace predict.py with this file when building your own workflow
import os
import mimetypes
import json
import shutil
from typing import List
from cog import BasePredictor, Input, Path
from comfyui import ComfyUI
from cog_model_helpers import optimise_images
from cog_model_helpers import seed as seed_helper
OUTPUT_DIR = "/tmp/outputs"
INPUT_DIR = "/tmp/inputs"
COMFYUI_TEMP_OUTPUT_DIR = "ComfyUI/temp"
ALL_DIRECTORIES = [OUTPUT_DIR, INPUT_DIR, COMFYUI_TEMP_OUTPUT_DIR]
mimetypes.add_type("image/webp", ".webp")
# Save your example JSON to the same directory as predict.py
api_json_file = "workflow_api.json"
# Force HF offline
os.environ["HF_DATASETS_OFFLINE"] = "1"
os.environ["TRANSFORMERS_OFFLINE"] = "1"
os.environ["HF_HUB_DISABLE_TELEMETRY"] = "1"
class Predictor(BasePredictor):
def setup(self):
self.comfyUI = ComfyUI("127.0.0.1:8188")
self.comfyUI.start_server(OUTPUT_DIR, INPUT_DIR)
# Give a list of weights filenames to download during setup
with open(api_json_file, "r") as file:
workflow = json.loads(file.read())
self.comfyUI.handle_weights(
workflow,
weights_to_download=[],
)
def filename_with_extension(self, input_file, prefix):
extension = os.path.splitext(input_file.name)[1]
return f"{prefix}{extension}"
def handle_input_file(
self,
input_file: Path,
filename: str = "image.png",
):
shutil.copy(input_file, os.path.join(INPUT_DIR, filename))
# Update nodes in the JSON workflow to modify your workflow based on the given inputs
def update_workflow(self, workflow, **kwargs):
# Below is an example showing how to get the node you need and update the inputs
# positive_prompt = workflow["6"]["inputs"]
# positive_prompt["text"] = kwargs["prompt"]
# negative_prompt = workflow["7"]["inputs"]
# negative_prompt["text"] = f"nsfw, {kwargs['negative_prompt']}"
# sampler = workflow["3"]["inputs"]
# sampler["seed"] = kwargs["seed"]
pass
def predict(
self,
prompt: str = Input(
default="",
),
negative_prompt: str = Input(
description="Things you do not want to see in your image",
default="",
),
image: Path = Input(
description="An input image",
default=None,
),
output_format: str = optimise_images.predict_output_format(),
output_quality: int = optimise_images.predict_output_quality(),
seed: int = seed_helper.predict_seed(),
) -> List[Path]:
"""Run a single prediction on the model"""
self.comfyUI.cleanup(ALL_DIRECTORIES)
# Make sure to set the seeds in your workflow
seed = seed_helper.generate(seed)
image_filename = None
if image:
image_filename = self.filename_with_extension(image, "image")
self.handle_input_file(image, image_filename)
with open(api_json_file, "r") as file:
workflow = json.loads(file.read())
self.update_workflow(
workflow,
prompt=prompt,
negative_prompt=negative_prompt,
image_filename=image_filename,
seed=seed,
)
wf = self.comfyUI.load_workflow(workflow)
self.comfyUI.connect()
self.comfyUI.run_workflow(wf)
return optimise_images.optimise_image_files(
output_format, output_quality, self.comfyUI.get_files(OUTPUT_DIR)
)