Spaces:
Runtime error
Runtime error
File size: 3,929 Bytes
f51843b 66b0cef 0d2ed9b b3b13e2 66b0cef 0d2ed9b 5808f1f f51843b 5808f1f 0d2ed9b f51843b 0d2ed9b f51843b 0d2ed9b f51843b aa1cce3 f51843b 0d2ed9b f51843b 0d2ed9b aa1cce3 f51843b 0d2ed9b f51843b aa1cce3 f51843b aa1cce3 f51843b aa1cce3 f51843b aa1cce3 f51843b aa1cce3 f51843b 0d2ed9b f51843b aa1cce3 f51843b aa1cce3 f51843b aa1cce3 f51843b aa1cce3 f51843b aa1cce3 73a0c03 baf2123 |
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 |
import torch
from diffusers import FluxPipeline
import gradio as gr
import threading
import os
os.environ["OMP_NUM_THREADS"] = str(os.cpu_count())
torch.set_num_threads(os.cpu_count())
# Initialize Flux pipeline
pipe = FluxPipeline.from_pretrained(
"black-forest-labs/FLUX.1-dev",
torch_dtype=torch.bfloat16
)
pipe.enable_model_cpu_offload()
stop_event = threading.Event()
def generate_images(
prompt,
height,
width,
guidance_scale,
num_inference_steps,
max_sequence_length,
seed,
randomize_seed
):
stop_event.clear()
results = []
for i in range(3):
if stop_event.is_set():
return [None] * 3
# Handle seed randomization
if randomize_seed:
current_seed = torch.randint(0, 2**32 - 1, (1,)).item()
else:
current_seed = seed + i
generator = torch.Generator(device="cpu").manual_seed(current_seed)
# Generate image with current parameters
image = pipe(
prompt=prompt,
height=int(height),
width=int(width),
guidance_scale=guidance_scale,
num_inference_steps=int(num_inference_steps),
max_sequence_length=int(max_sequence_length),
generator=generator
).images[0]
results.append(image)
return results
def stop_generation():
stop_event.set()
return [None] * 3
with gr.Blocks() as interface:
gr.Markdown("""
### FLUX Image Generation
Adjust parameters below to control the image generation process
""")
with gr.Row():
text_input = gr.Textbox(
label="Prompt",
placeholder="Describe what you want to generate...",
scale=3
)
with gr.Accordion("Generation Parameters", open=False):
with gr.Row():
height = gr.Number(
label="Height",
value=1024,
minimum=512,
maximum=4096,
step=64,
precision=0
)
width = gr.Number(
label="Width",
value=1024,
minimum=512,
maximum=4096,
step=64,
precision=0
)
guidance_scale = gr.Slider(
label="Guidance Scale",
minimum=0.0,
maximum=20.0,
value=7.0,
step=0.5
)
num_inference_steps = gr.Slider(
label="Inference Steps",
minimum=10,
maximum=150,
value=50,
step=1
)
max_sequence_length = gr.Dropdown(
label="Max Sequence Length",
choices=[512, 768, 1024],
value=512
)
with gr.Row():
seed = gr.Number(
label="Seed",
value=42,
precision=0
)
randomize_seed = gr.Checkbox(
label="Randomize Seed",
value=True
)
with gr.Row():
generate_btn = gr.Button("Generate", variant="primary")
stop_btn = gr.Button("Stop Generation")
with gr.Row():
output1 = gr.Image(label="Output 1", type="pil")
output2 = gr.Image(label="Output 2", type="pil")
output3 = gr.Image(label="Output 3", type="pil")
generate_btn.click(
generate_images,
inputs=[
text_input,
height,
width,
guidance_scale,
num_inference_steps,
max_sequence_length,
seed,
randomize_seed
],
outputs=[output1, output2, output3]
)
stop_btn.click(
stop_generation,
inputs=[],
outputs=[output1, output2, output3]
)
interface.launch() |