Spaces:
Runtime error
Runtime error
File size: 6,607 Bytes
f064a5b |
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
import os
import random
import sys
from typing import Sequence, Mapping, Any, Union
import torch
import gradio as gr
from PIL import Image
from huggingface_hub import hf_hub_download
import spaces
from comfy import model_management
# Download required models
t5_path = hf_hub_download(repo_id="comfyanonymous/flux_text_encoders", filename="t5xxl_fp8_e4m3fn.safetensors", local_dir="models/text_encoders/")
vae_path = hf_hub_download(repo_id="black-forest-labs/FLUX.1-dev", filename="ae.safetensors", local_dir="models/vae")
unet_path = hf_hub_download(repo_id="lodestones/Chroma", filename="chroma-unlocked-v31.safetensors", local_dir="models/unet")
# Import the workflow functions
from my_workflow import (
get_value_at_index,
add_comfyui_directory_to_sys_path,
add_extra_model_paths,
import_custom_nodes,
NODE_CLASS_MAPPINGS,
CLIPTextEncode,
CLIPLoader,
VAEDecode,
UNETLoader,
VAELoader,
SaveImage,
)
# Initialize ComfyUI
add_comfyui_directory_to_sys_path()
add_extra_model_paths()
import_custom_nodes()
# Initialize all model loaders outside the function
randomnoise = NODE_CLASS_MAPPINGS["RandomNoise"]()
emptysd3latentimage = NODE_CLASS_MAPPINGS["EmptySD3LatentImage"]()
ksamplerselect = NODE_CLASS_MAPPINGS["KSamplerSelect"]()
cliploader = CLIPLoader()
t5tokenizeroptions = NODE_CLASS_MAPPINGS["T5TokenizerOptions"]()
cliptextencode = CLIPTextEncode()
unetloader = UNETLoader()
vaeloader = VAELoader()
cfgguider = NODE_CLASS_MAPPINGS["CFGGuider"]()
basicscheduler = NODE_CLASS_MAPPINGS["BasicScheduler"]()
samplercustomadvanced = NODE_CLASS_MAPPINGS["SamplerCustomAdvanced"]()
vaedecode = VAEDecode()
saveimage = SaveImage()
# Load models
cliploader_78 = cliploader.load_clip(
clip_name="t5xxl_fp8_e4m3fn.safetensors", type="chroma", device="default"
)
t5tokenizeroptions_82 = t5tokenizeroptions.set_options(
min_padding=1, min_length=0, clip=get_value_at_index(cliploader_78, 0)
)
unetloader_76 = unetloader.load_unet(
unet_name="chroma-unlocked-v31.safetensors", weight_dtype="fp8_e4m3fn"
)
vaeloader_80 = vaeloader.load_vae(vae_name="ae.safetensors")
# Add all the models that load a safetensors file
model_loaders = [cliploader_78, unetloader_76, vaeloader_80]
# Check which models are valid and how to best load them
valid_models = [
getattr(loader[0], 'patcher', loader[0])
for loader in model_loaders
if not isinstance(loader[0], dict) and not isinstance(getattr(loader[0], 'patcher', None), dict)
]
# Finally loads the models
model_management.load_models_gpu(valid_models)
@spaces.GPU
def generate_image(prompt, negative_prompt, width, height, steps, cfg, seed):
with torch.inference_mode():
# Set random seed if provided
if seed == -1:
seed = random.randint(1, 2**64)
random.seed(seed)
randomnoise_68 = randomnoise.get_noise(noise_seed=seed)
emptysd3latentimage_69 = emptysd3latentimage.generate(
width=width, height=height, batch_size=1
)
ksamplerselect_72 = ksamplerselect.get_sampler(sampler_name="euler")
cliptextencode_74 = cliptextencode.encode(
text=prompt,
clip=get_value_at_index(t5tokenizeroptions_82, 0),
)
cliptextencode_75 = cliptextencode.encode(
text=negative_prompt,
clip=get_value_at_index(t5tokenizeroptions_82, 0),
)
cfgguider_73 = cfgguider.get_guider(
cfg=cfg,
model=get_value_at_index(unetloader_76, 0),
positive=get_value_at_index(cliptextencode_74, 0),
negative=get_value_at_index(cliptextencode_75, 0),
)
basicscheduler_84 = basicscheduler.get_sigmas(
scheduler="beta",
steps=steps,
denoise=1,
model=get_value_at_index(unetloader_76, 0),
)
samplercustomadvanced_67 = samplercustomadvanced.sample(
noise=get_value_at_index(randomnoise_68, 0),
guider=get_value_at_index(cfgguider_73, 0),
sampler=get_value_at_index(ksamplerselect_72, 0),
sigmas=get_value_at_index(basicscheduler_84, 0),
latent_image=get_value_at_index(emptysd3latentimage_69, 0),
)
vaedecode_79 = vaedecode.decode(
samples=get_value_at_index(samplercustomadvanced_67, 0),
vae=get_value_at_index(vaeloader_80, 0),
)
# Instead of saving to file, return the image directly
return get_value_at_index(vaedecode_79, 0)
# Create Gradio interface
with gr.Blocks() as app:
gr.Markdown("# Chroma Image Generator")
with gr.Row():
with gr.Column():
prompt = gr.Textbox(
label="Prompt",
placeholder="Enter your prompt here...",
lines=3
)
negative_prompt = gr.Textbox(
label="Negative Prompt",
placeholder="Enter negative prompt here...",
value="low quality, ugly, unfinished, out of focus, deformed, disfigure, blurry, smudged, restricted palette, flat colors",
lines=2
)
with gr.Row():
width = gr.Slider(
minimum=512,
maximum=2048,
value=1024,
step=64,
label="Width"
)
height = gr.Slider(
minimum=512,
maximum=2048,
value=1024,
step=64,
label="Height"
)
with gr.Row():
steps = gr.Slider(
minimum=1,
maximum=50,
value=26,
step=1,
label="Steps"
)
cfg = gr.Slider(
minimum=1,
maximum=20,
value=4,
step=0.5,
label="CFG Scale"
)
seed = gr.Number(
value=-1,
label="Seed (-1 for random)"
)
generate_btn = gr.Button("Generate")
with gr.Column():
output_image = gr.Image(label="Generated Image")
generate_btn.click(
fn=generate_image,
inputs=[prompt, negative_prompt, width, height, steps, cfg, seed],
outputs=[output_image]
)
if __name__ == "__main__":
app.launch(share=True)
|