File size: 2,208 Bytes
7f891bb
4e6d911
 
 
2e306db
3d05f5b
d9f1205
242b4ef
 
 
 
4e6d911
242b4ef
4e6d911
3d05f5b
242b4ef
 
 
 
8a31b39
242b4ef
 
 
 
8a31b39
4e6d911
 
9d86930
d2cb214
4e6d911
242b4ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e6d911
242b4ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9bd528
242b4ef
 
b9bd528
 
d53ee34
 
d2b0012
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
import gradio as gr
import numpy as np
import random
import spaces
import torch
from diffusers import FluxPipeline

# Enable cuDNN benchmarking for potential performance improvement
torch.backends.cudnn.benchmark = True

# Set up device and data types
device = "cuda" if torch.cuda.is_available() else "cpu"
DTYPE = torch.float16

# Load the model
pipe = FluxPipeline.from_pretrained(
    "black-forest-labs/FLUX.1-schnell",
    torch_dtype=torch.bfloat16,
)

# Configure the pipeline
pipe.enable_sequential_cpu_offload()
pipe.vae.enable_tiling()
pipe = pipe.to(DTYPE)

MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 2048

@spaces.GPU()
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
    if randomize_seed:
        seed = random.randint(0, MAX_SEED)
    generator = torch.Generator(device=device).manual_seed(seed)
    
    image = pipe(
        prompt,
        num_inference_steps=num_inference_steps,
        num_images_per_prompt=1,
        guidance_scale=0.0,
        height=height,
        width=width,
        generator=generator,
    ).images[0]
    
    return image, seed

# Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("# FLUX.1 [schnell] Image Generator")
    with gr.Row():
        with gr.Column():
            prompt = gr.Textbox(label="Prompt")
            run_button = gr.Button("Generate")
        with gr.Column():
            result = gr.Image(label="Generated Image")
    with gr.Accordion("Advanced Settings", open=False):
        seed = gr.Slider(minimum=0, maximum=MAX_SEED, step=1, label="Seed", randomize=True)
        randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
        width = gr.Slider(minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024, label="Width")
        height = gr.Slider(minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024, label="Height")
        num_inference_steps = gr.Slider(minimum=1, maximum=50, step=1, value=4, label="Number of inference steps")
    
    run_button.click(
        infer,
        inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps],
        outputs=[result, seed]
    )

demo.launch()