Spaces:
Sleeping
Sleeping
File size: 4,526 Bytes
05bad85 561df63 05bad85 561df63 355b9a2 561df63 05bad85 561df63 05bad85 561df63 05bad85 561df63 05bad85 561df63 05bad85 561df63 05bad85 561df63 05bad85 561df63 05bad85 561df63 05bad85 561df63 05bad85 561df63 05bad85 |
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 |
import spaces
import gradio as gr
import torch
from diffusers import FluxPipeline, FluxTransformer2DModel, FlowMatchEulerDiscreteScheduler
from huggingface_hub import hf_hub_download
from PIL import Image
import numpy as np
import random
import os
hf_token = os.environ.get('HF_TOKEN')
# Constants
model = "black-forest-labs/FLUX.1-dev"
MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 2048
@spaces.GPU()
def infer(prompt, width, height, num_inference_steps, guidance_scale, nums, seed=42, randomize_seed=True, progress=gr.Progress(track_tqdm=True)):
device = "cuda" if torch.cuda.is_available() else "cpu"
# Initialize model inside the GPU-enabled function
try:
transformer = FluxTransformer2DModel.from_single_file(
"https://huggingface.co/lodestones/Chroma/resolve/main/chroma-unlocked-v27.safetensors",
torch_dtype=torch.bfloat16,
token=hf_token
)
except KeyError as e:
print(f"Error loading chroma-unlocked-v27.safetensors: {e}. Falling back to pretrained model.")
transformer = FluxTransformer2DModel.from_pretrained(
"lodestones/Chroma",
subfolder="transformer",
torch_dtype=torch.bfloat16,
token=hf_token
)
pipe = FluxPipeline.from_pretrained(
model,
transformer=transformer,
torch_dtype=torch.bfloat16,
token=hf_token
)
pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_config(
pipe.scheduler.config, use_beta_sigmas=True
)
pipe.to(device)
# Generate images
if randomize_seed:
seed = random.randint(0, MAX_SEED)
generator = torch.Generator(device=device).manual_seed(seed)
images = pipe(
prompt=prompt,
width=width,
height=height,
num_inference_steps=num_inference_steps,
guidance_scale=guidance_scale,
num_images_per_prompt=nums,
generator=generator
).images
return images, seed
css = """
#col-container {
margin: 0 auto;
max-width: 1024px;
}
"""
with gr.Blocks(css=css) as demo:
with gr.Column(elem_id="col-container"):
gr.HTML("<h1><center>Model Testing</center></h1><p><center>Chroma</center></p>")
with gr.Row():
prompt = gr.Text(
label="Prompt",
show_label=False,
max_lines=2,
placeholder="Enter your prompt",
container=False,
)
run_button = gr.Button("Run", scale=0)
result = gr.Gallery(label="Gallery", format="png", columns=1, preview=True, height=400)
with gr.Accordion("Advanced Settings", open=False):
with gr.Row():
width = gr.Slider(
label="Width",
minimum=256,
maximum=MAX_IMAGE_SIZE,
step=32,
value=1024,
)
height = gr.Slider(
label="Height",
minimum=256,
maximum=MAX_IMAGE_SIZE,
step=32,
value=1024,
)
with gr.Row():
num_inference_steps = gr.Slider(
label="Number of inference steps",
minimum=1,
maximum=50,
step=1,
value=30,
)
guidance_scale = gr.Slider(
label="Guidance Scale",
minimum=0,
maximum=10,
step=0.1,
value=3.5,
)
with gr.Row():
nums = gr.Slider(
label="Number of Images",
minimum=1,
maximum=2,
step=1,
value=1,
scale=1,
)
seed = gr.Slider(
label="Seed",
minimum=0,
maximum=MAX_SEED,
step=1,
value=-1,
)
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
gr.on(
triggers=[run_button.click, prompt.submit],
fn=infer,
inputs=[prompt, width, height, num_inference_steps, guidance_scale, nums, seed, randomize_seed],
outputs=[result, seed]
)
demo.launch() |