Spaces:
Running
on
Zero
Running
on
Zero
File size: 5,966 Bytes
42d7985 bc2e792 42d7985 bc2e792 42d7985 bc2e792 42d7985 6c3ac85 42d7985 bc2e792 42d7985 bc2e792 42d7985 bc2e792 42d7985 bc2e792 42d7985 bc2e792 42d7985 bc2e792 42d7985 bc2e792 42d7985 bc2e792 42d7985 bc2e792 42d7985 bc2e792 42d7985 |
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 |
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)
|