File size: 8,216 Bytes
aa01795 4c18bc5 aa01795 |
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
import gradio as gr
import torch
from diffusers import AuraFlowPipeline
import spaces
import numpy as np
pipeline = AuraFlowPipeline.from_pretrained(
"fal/AuraFlow-v0.3",
torch_dtype=torch.float16,
variant="fp16",
use_safetensors=True,
).to("cuda")
STYLE_PRESETS = {
"None": "",
"Comic": ", in comic book style, bold outlines, vibrant colors, dynamic shading",
"Watercolor": ", in watercolor style, soft edges, translucent colors, delicate brushstrokes",
"Oil Painting": ", in oil painting style, rich textures, bold brushstrokes, deep colors",
"Cyberpunk": ", in cyberpunk style, neon lights, dark atmosphere, futuristic elements",
"Photorealistic": ", in photorealistic style, highly detailed, lifelike textures, realistic lighting"
}
examples = [
{"prompt": "A rustic village nestled in a golden autumn valley, with rolling hills and a winding river bathed in warm light", "style": "Oil Painting"},
{"prompt": "A majestic dragon soaring high above a range of snow-capped mountains under a golden sunset sky", "style": "Comic"},
{"prompt": "A shiba inu on a rocky cliff overlooking a vibrant sunset ocean view", "style": "Photorealistic"},
{"prompt": "A futuristic city skyline glowing with neon lights, towering skyscrapers, and flying cars under a stormy night", "style": "Cyberpunk"},
]
@spaces.GPU(duration=120)
def generate_images(
prompt,
negative_prompt,
style,
width=1024,
height=1024,
steps=20,
guidance=5.0,
seed=1,
num_images=1,
):
generator = torch.Generator(device="cuda").manual_seed(seed)
styled_prompt = f"{prompt}{STYLE_PRESETS[style]}"
gallery = []
for i in range(num_images):
image = pipeline(
prompt=styled_prompt,
negative_prompt=negative_prompt,
width=width,
height=height,
num_inference_steps=steps,
guidance_scale=guidance,
generator=generator,
output_type="pil",
).images[0]
gallery.append((image, ""))
torch.cuda.empty_cache()
return gallery
def interface_fn(
prompt,
negative_prompt,
style,
width,
height,
steps,
guidance,
seed,
num_images,
randomize_seed,
history,
progress: gr.Progress = gr.Progress(track_tqdm=True)
):
if not prompt:
raise gr.Error("Please enter a prompt!")
if randomize_seed:
seed = np.random.randint(0, 1000000)
gallery = generate_images(
prompt=prompt,
negative_prompt=negative_prompt,
style=style,
width=width,
height=height,
steps=steps,
guidance=guidance,
seed=seed,
num_images=num_images
)
updated_history = update_history(gallery, history)
return gallery, seed, updated_history
def update_history(new_images, history):
if history is None:
history = []
for img in reversed(new_images):
history.insert(0, img[0])
return history
def clear_result():
return gr.update(value=[]), gr.update(value=None)
#my custom css for layout
custom_css = """
.gr-button {margin: 5px;}
.output-image {border-radius: 8px;}
#advanced_options {margin-top: 20px;}
.style-dropdown {width: 100%; max-width: 800px;}
.gr-textbox {width: 100%;}
.example-row {margin-top: 20px;}
.example-button {white-space: normal; height: auto; min-height: 60px;}
/* Center the Generated Images gallery */
#output-gallery {
display: block;
width: 100%;
text-align: center;
}
#output-gallery .gallery {
display: inline-flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
margin: 0 auto;
}
#output-gallery .gallery > div {
display: flex;
justify-content: center;
align-items: center;
margin: 5px;
}
#output-gallery img {
display: block;
margin: 0 auto;
}
"""
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as interface:
gr.Markdown("# AuraFlow v0.3 Image Generator")
gr.Markdown("Enter a prompt and select a style to generate images. Use the advanced settings for more control or try an example below.")
with gr.Row():
with gr.Column(scale=1):
prompt_input = gr.Textbox(
label="Prompt",
placeholder="Enter your creative prompt here",
lines=3
)
neg_prompt_input = gr.Textbox(
label="Negative Prompt",
placeholder="What you don’t want in the image",
lines=2
)
style_input = gr.Dropdown(
choices=list(STYLE_PRESETS.keys()),
value="None",
label="Art Style",
elem_classes=["style-dropdown"]
)
with gr.Accordion("Advanced Settings", open=False, elem_id="advanced_options"):
width_input = gr.Slider(256, 1536, step=256, value=1024, label="Width")
height_input = gr.Slider(256, 1536, step=256, value=1024, label="Height")
steps_input = gr.Slider(1, 50, step=1, value=20, label="Inference Steps")
guidance_input = gr.Slider(0, 10, step=0.5, value=5.0, label="Guidance Scale")
with gr.Row():
seed_input = gr.Number(value=1, label="Seed", visible=False)
randomize_seed_input = gr.Checkbox(value=True, label="Randomize Seed")
num_images_input = gr.Slider(1, 4, step=1, value=1, label="Number of Images")
with gr.Column(scale=2):
image_output = gr.Gallery(
label="Generated Images",
show_label=True,
preview=True,
elem_id="output-gallery"
)
with gr.Row():
clear_btn = gr.Button("Clear", variant="secondary")
generate_btn = gr.Button("Generate", variant="primary")
history_gallery = gr.Gallery(
label="History",
columns=6,
object_fit="contain",
interactive=False
)
with gr.Row(equal_height=True, elem_classes=["example-row"]):
gr.Markdown("### Try these examples:")
with gr.Row(equal_height=True, elem_classes=["example-row"]):
for ex in examples:
with gr.Column(scale=1, min_width=200):
btn = gr.Button(ex["prompt"], variant="secondary", elem_classes=["example-button"])
btn.click(
fn=lambda p=ex["prompt"], s=ex["style"]: (gr.update(value=p), gr.update(value=s)),
inputs=[],
outputs=[prompt_input, style_input]
).then(
fn=interface_fn,
inputs=[prompt_input, neg_prompt_input, style_input, width_input, height_input,
steps_input, guidance_input, seed_input, num_images_input, randomize_seed_input, history_gallery],
outputs=[image_output, seed_input, history_gallery]
)
generate_btn.click(
fn=lambda: clear_result(),
inputs=[],
outputs=[image_output, seed_input]
).then(
fn=interface_fn,
inputs=[prompt_input, neg_prompt_input, style_input, width_input, height_input,
steps_input, guidance_input, seed_input, num_images_input, randomize_seed_input, history_gallery],
outputs=[image_output, seed_input, history_gallery]
)
clear_btn.click(
fn=clear_result,
inputs=[],
outputs=[image_output, seed_input]
).then(
fn=lambda x: (gr.update(value=[]), gr.update(value=None), x),
inputs=[history_gallery],
outputs=[image_output, seed_input, history_gallery]
)
randomize_seed_input.change(
fn=lambda randomize: gr.update(visible=not randomize),
inputs=randomize_seed_input,
outputs=seed_input
)
interface.launch(
show_error=True,
server_name="0.0.0.0",
server_port=7860,
share=True
) |