NAG_FLUX.1-dev / app.py
ChenDY's picture
width n height
bc2e792
import random
import os
import spaces
import numpy as np
import torch
from PIL import Image
import huggingface_hub
import gradio as gr
from src.pipeline_flux_nag import NAGFluxPipeline
from src.transformer_flux import NAGFluxTransformer2DModel
MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 2048
theme = gr.themes.Base(
font=[gr.themes.GoogleFont('Libre Franklin'), gr.themes.GoogleFont('Public Sans'), 'system-ui', 'sans-serif'],
)
transformer = NAGFluxTransformer2DModel.from_pretrained(
"black-forest-labs/FLUX.1-dev",
subfolder="transformer",
torch_dtype=torch.bfloat16,
)
pipe = NAGFluxPipeline.from_pretrained(
"black-forest-labs/FLUX.1-dev",
transformer=transformer,
torch_dtype=torch.bfloat16,
)
device = "cuda"
pipe = pipe.to(device)
examples = [
["Portrait of AI researcher.", "Glasses.", 5],
["Portrait of AI researcher.", "Male.", 5],
["A baby phoenix made of fire and flames is born from the smoking ashes.", "Low resolution, blurry, lack of details, illustration, cartoon, painting.", 5],
["A tiny astronaut hatching from an egg on the moon.", "Low resolution, blurry, lack of details, illustration, cartoon, painting.", 9]
]
@spaces.GPU
def sample(
prompt,
negative_prompt=None, guidance_scale=3.5,
nag_negative_prompt=None, nag_scale=5.0,
width=1024, height=1024,
num_inference_steps=25,
seed=2025, randomize_seed=False,
compare=True,
):
prompt = prompt.strip()
negative_prompt = negative_prompt.strip() if negative_prompt and negative_prompt.strip() else None
guidance_scale = float(guidance_scale)
width, height = int(width), int(height)
num_inference_steps = int(num_inference_steps)
if (randomize_seed):
seed = random.randint(0, MAX_SEED)
else:
seed = int(seed)
generator = torch.Generator(device="cuda").manual_seed(seed)
image_nag = pipe(
prompt,
negative_prompt=negative_prompt,
guidance_scale=guidance_scale,
nag_negative_prompt=nag_negative_prompt,
nag_scale=nag_scale,
generator=generator,
width=width,
height=height,
num_inference_steps=num_inference_steps,
).images[0]
if compare:
generator = torch.Generator(device="cuda").manual_seed(seed)
image_normal = pipe(
prompt,
negative_prompt=negative_prompt,
guidance_scale=guidance_scale,
generator=generator,
width=width,
height=height,
num_inference_steps=num_inference_steps,
).images[0]
else:
image_normal = Image.new("RGB", image_nag.size, color=(0, 0, 0))
return (image_normal, image_nag), seed
def sample_example(
prompt,
nag_negative_prompt,
nag_scale,
):
outputs, seed = sample(
prompt=prompt,
nag_negative_prompt=nag_negative_prompt,
nag_scale=nag_scale,
)
return outputs, 3.5, 1024, 1024, 25, seed, True
css = '''
.gradio-container{
max-width: 768px !important;
margin: 0 auto;
}
'''
with gr.Blocks(css=css, theme=theme) as demo:
gr.Markdown('''# Normalized Attention Guidance (NAG) Flux-Dev
Implementation of [Normalized Attention Guidance](https://chendaryen.github.io/NAG.github.io/)
''')
with gr.Group():
prompt = gr.Textbox(
label="Prompt",
max_lines=1,
placeholder="Enter your prompt",
)
nag_negative_prompt = gr.Textbox(
label="Negative Prompt for NAG",
value="Low resolution, blurry, lack of details, illustration, cartoon, painting.",
max_lines=1,
)
nag_scale = gr.Slider(label="NAG Scale", minimum=1., maximum=20., step=0.25, value=5.)
compare = gr.Checkbox(label="Compare with baseline", info="If unchecked, only sample with NAG will be generated.", value=True)
button = gr.Button("Generate", min_width=120)
output = gr.ImageSlider(label="Left: Baseline, Right: With NAG", interactive=False)
with gr.Accordion("Advanced Settings", open=False):
negative_prompt = gr.Textbox(label="Negative Prompt", value=None, visible=False)
guidance_scale = gr.Slider(label="Guidance Scale", minimum=1., maximum=15., step=0.1, value=3.5)
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,
)
num_inference_steps = gr.Slider(label="Inference Steps", minimum=1, maximum=50, step=1, value=25)
seed = gr.Slider(label="Seed", minimum=1, maximum=MAX_SEED, step=1, randomize=True)
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
gr.Examples(
examples=examples,
fn=sample_example,
inputs=[
prompt,
nag_negative_prompt,
nag_scale,
],
outputs=[output, guidance_scale, width, height, num_inference_steps, seed, compare],
cache_examples="lazy",
)
gr.on(
triggers=[
button.click,
prompt.submit
],
fn=sample,
inputs=[
prompt,
negative_prompt, guidance_scale,
nag_negative_prompt, nag_scale,
width, height,
num_inference_steps,
seed, randomize_seed,
compare,
],
outputs=[output, seed],
)
if __name__ == "__main__":
huggingface_hub.login(os.getenv('HF_TOKEN'))
demo.launch(share=True)