Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,90 +20,81 @@ pipe = FluxWithCFGPipeline.from_pretrained(
|
|
| 20 |
"black-forest-labs/FLUX.1-schnell", torch_dtype=dtype
|
| 21 |
)
|
| 22 |
pipe.vae = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype)
|
| 23 |
-
# pipe.load_lora_weights("ostris/OpenFLUX.1", weight_name="openflux1-v0.1.0-fast-lora.safetensors", adapter_name="fast")
|
| 24 |
-
# pipe.set_adapters("fast")
|
| 25 |
-
# pipe.fuse_lora(adapter_names=["fast"], lora_scale=1.0)
|
| 26 |
pipe.to("cuda")
|
| 27 |
-
# pipe.transformer.to(memory_format=torch.channels_last)
|
| 28 |
-
# pipe.transformer = torch.compile(
|
| 29 |
-
# pipe.transformer, mode="max-autotune", fullgraph=True
|
| 30 |
-
# )
|
| 31 |
torch.cuda.empty_cache()
|
| 32 |
|
| 33 |
# Inference function
|
| 34 |
@spaces.GPU(duration=25)
|
| 35 |
-
def generate_image(prompt, seed=24, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT, randomize_seed=False,
|
| 36 |
if randomize_seed:
|
| 37 |
seed = random.randint(0, MAX_SEED)
|
| 38 |
generator = torch.Generator().manual_seed(int(float(seed)))
|
| 39 |
|
| 40 |
start_time = time.time()
|
| 41 |
|
| 42 |
-
# Only generate the last image in the sequence
|
| 43 |
img = pipe.generate_images(
|
| 44 |
prompt=prompt,
|
| 45 |
width=width,
|
| 46 |
height=height,
|
| 47 |
-
num_inference_steps=
|
| 48 |
generator=generator
|
| 49 |
)
|
| 50 |
-
latency = f"
|
| 51 |
return img, seed, latency
|
| 52 |
|
| 53 |
# Example prompts
|
| 54 |
examples = [
|
| 55 |
"sexy woman & man , under wear, full body, sunday",
|
| 56 |
-
"A glamorous young woman with long, wavy blonde hair and smokey eye makeup, posing in a luxury hotel room. She
|
| 57 |
-
"A fit male fitness influencer with short dark hair and stubble, standing shirtless in a modern gym. He has defined abs and arm muscles, and is holding a protein shake in one hand and a card that says 'Invite' in the other. Bright, clean lighting highlights his physique.",
|
| 58 |
-
"A bohemian-style female travel blogger with sun-kissed skin and messy beach waves, sitting on a tropical beach at sunset. She
|
| 59 |
-
"A trendy male fashion influencer with perfectly styled hair and designer stubble, posing on a city street. He
|
| 60 |
-
"A fresh-faced young female beauty guru with freckles and natural makeup, sitting at a vanity covered in cosmetics. She
|
| 61 |
-
"A stylish young woman with long, wavy ombre hair and winged eyeliner, posing in front of a neon-lit city skyline at night. She
|
| 62 |
-
|
| 63 |
]
|
| 64 |
|
| 65 |
-
|
| 66 |
css = """
|
| 67 |
-
footer {
|
| 68 |
-
|
| 69 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
"""
|
| 71 |
|
| 72 |
# --- Gradio UI ---
|
| 73 |
with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
|
| 74 |
-
with gr.Column(elem_id="
|
| 75 |
gr.Markdown("# Open FLUX 1.1 Pro")
|
| 76 |
|
| 77 |
-
|
| 78 |
with gr.Row():
|
| 79 |
-
with gr.Column(scale=2
|
| 80 |
result = gr.Image(label="Generated Image", show_label=False, interactive=False)
|
| 81 |
with gr.Column(scale=1):
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
)
|
| 89 |
-
generateBtn = gr.Button("🖼️ Generate Image")
|
| 90 |
-
enhanceBtn = gr.Button("🚀 Enhance Image")
|
| 91 |
-
|
| 92 |
-
with gr.Column("Advanced Options"):
|
| 93 |
with gr.Row():
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
with gr.Row():
|
| 97 |
seed = gr.Number(label="Seed", value=42)
|
| 98 |
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 99 |
with gr.Row():
|
| 100 |
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=DEFAULT_WIDTH)
|
| 101 |
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=DEFAULT_HEIGHT)
|
| 102 |
-
num_inference_steps = gr.Slider(label="Inference Steps", minimum=1, maximum=4, step=1, value=DEFAULT_INFERENCE_STEPS)
|
| 103 |
|
| 104 |
-
with gr.
|
| 105 |
-
gr.Markdown("###
|
| 106 |
-
with gr.Row():
|
| 107 |
gr.Examples(
|
| 108 |
examples=examples,
|
| 109 |
fn=generate_image,
|
|
@@ -117,56 +108,15 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
|
|
| 117 |
inputs=[prompt, seed, width, height],
|
| 118 |
outputs=[result, seed, latency],
|
| 119 |
show_progress="full",
|
| 120 |
-
queue=False,
|
| 121 |
-
concurrency_limit=None
|
| 122 |
)
|
| 123 |
|
| 124 |
generateBtn.click(
|
| 125 |
fn=generate_image,
|
| 126 |
-
inputs=[prompt, seed, width, height, randomize_seed
|
| 127 |
-
outputs=[result, seed, latency],
|
| 128 |
-
show_progress="full",
|
| 129 |
-
api_name="RealtimeFlux",
|
| 130 |
-
queue=False
|
| 131 |
-
)
|
| 132 |
-
|
| 133 |
-
def update_ui(realtime_enabled):
|
| 134 |
-
return {
|
| 135 |
-
prompt: gr.update(interactive=True),
|
| 136 |
-
generateBtn: gr.update(visible=not realtime_enabled)
|
| 137 |
-
}
|
| 138 |
-
|
| 139 |
-
realtime.change(
|
| 140 |
-
fn=update_ui,
|
| 141 |
-
inputs=[realtime],
|
| 142 |
-
outputs=[prompt, generateBtn],
|
| 143 |
-
queue=False,
|
| 144 |
-
concurrency_limit=None
|
| 145 |
-
)
|
| 146 |
-
|
| 147 |
-
def realtime_generation(*args):
|
| 148 |
-
if args[0]: # If realtime is enabled
|
| 149 |
-
return next(generate_image(*args[1:]))
|
| 150 |
-
|
| 151 |
-
prompt.submit(
|
| 152 |
-
fn=generate_image,
|
| 153 |
-
inputs=[prompt, seed, width, height, randomize_seed, num_inference_steps],
|
| 154 |
outputs=[result, seed, latency],
|
| 155 |
show_progress="full",
|
| 156 |
-
|
| 157 |
-
concurrency_limit=None
|
| 158 |
)
|
| 159 |
|
| 160 |
-
for component in [prompt, width, height, num_inference_steps]:
|
| 161 |
-
component.input(
|
| 162 |
-
fn=realtime_generation,
|
| 163 |
-
inputs=[realtime, prompt, seed, width, height, randomize_seed, num_inference_steps],
|
| 164 |
-
outputs=[result, seed, latency],
|
| 165 |
-
show_progress="hidden",
|
| 166 |
-
trigger_mode="always_last",
|
| 167 |
-
queue=False,
|
| 168 |
-
concurrency_limit=None
|
| 169 |
-
)
|
| 170 |
-
|
| 171 |
# Launch the app
|
| 172 |
-
demo.launch()
|
|
|
|
| 20 |
"black-forest-labs/FLUX.1-schnell", torch_dtype=dtype
|
| 21 |
)
|
| 22 |
pipe.vae = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype)
|
|
|
|
|
|
|
|
|
|
| 23 |
pipe.to("cuda")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
torch.cuda.empty_cache()
|
| 25 |
|
| 26 |
# Inference function
|
| 27 |
@spaces.GPU(duration=25)
|
| 28 |
+
def generate_image(prompt, seed=24, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT, randomize_seed=False, progress=gr.Progress(track_tqdm=True)):
|
| 29 |
if randomize_seed:
|
| 30 |
seed = random.randint(0, MAX_SEED)
|
| 31 |
generator = torch.Generator().manual_seed(int(float(seed)))
|
| 32 |
|
| 33 |
start_time = time.time()
|
| 34 |
|
|
|
|
| 35 |
img = pipe.generate_images(
|
| 36 |
prompt=prompt,
|
| 37 |
width=width,
|
| 38 |
height=height,
|
| 39 |
+
num_inference_steps=DEFAULT_INFERENCE_STEPS,
|
| 40 |
generator=generator
|
| 41 |
)
|
| 42 |
+
latency = f"Generation time: {(time.time()-start_time):.2f} seconds"
|
| 43 |
return img, seed, latency
|
| 44 |
|
| 45 |
# Example prompts
|
| 46 |
examples = [
|
| 47 |
"sexy woman & man , under wear, full body, sunday",
|
| 48 |
+
"A glamorous young woman with long, wavy blonde hair and smokey eye makeup, posing in a luxury hotel room. She's wearing a sparkly gold cocktail dress and holding up a white card with 'Invite' written on it in elegant calligraphy. Soft, warm lighting creates a luxurious atmosphere. ",
|
| 49 |
+
"A fit male fitness influencer with short dark hair and stubble, standing shirtless in a modern gym. He has defined abs and arm muscles, and is holding a protein shake in one hand and a card that says 'Invite' in the other. Bright, clean lighting highlights his physique.",
|
| 50 |
+
"A bohemian-style female travel blogger with sun-kissed skin and messy beach waves, sitting on a tropical beach at sunset. She's wearing a flowy white sundress and holding up a weathered postcard with 'Invite scrawled on it. Golden hour lighting bathes the scene in warm tones. ",
|
| 51 |
+
"A trendy male fashion influencer with perfectly styled hair and designer stubble, posing on a city street. He's wearing a tailored suit and holding up a sleek black business card with 'Invite' printed in minimalist white font. The background shows blurred city lights, creating a chic urban atmosphere.",
|
| 52 |
+
"A fresh-faced young female beauty guru with freckles and natural makeup, sitting at a vanity covered in cosmetics. She's wearing a pastel pink robe and holding up a makeup palette with 'Invite' written on it in lipstick. Soft, flattering lighting enhances her radiant complexion. ",
|
| 53 |
+
"A stylish young woman with long, wavy ombre hair and winged eyeliner, posing in front of a neon-lit city skyline at night. She's wearing a sleek black leather jacket over a sparkly crop top and holding up a holographic business card that says 'Invite' in futuristic font. The card reflects the colorful neon lights, creating a cyberpunk aesthetic.",
|
|
|
|
| 54 |
]
|
| 55 |
|
|
|
|
| 56 |
css = """
|
| 57 |
+
footer {visibility: hidden;}
|
| 58 |
+
.container {max-width: 1200px; margin: auto; padding: 20px;}
|
| 59 |
+
.generate-box {background-color: #f0f0f0; border-radius: 10px; padding: 20px; margin-bottom: 20px;}
|
| 60 |
+
.generate-box .row {display: flex; align-items: center; margin-bottom: 10px;}
|
| 61 |
+
.generate-box .row > * {margin-right: 10px;}
|
| 62 |
+
.generate-box .row > *:last-child {margin-right: 0;}
|
| 63 |
+
.advanced-options {background-color: #e0e0e0; border-radius: 10px; padding: 20px; margin-top: 20px;}
|
| 64 |
+
.examples-gallery {margin-top: 30px;}
|
| 65 |
"""
|
| 66 |
|
| 67 |
# --- Gradio UI ---
|
| 68 |
with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
|
| 69 |
+
with gr.Column(elem_id="container"):
|
| 70 |
gr.Markdown("# Open FLUX 1.1 Pro")
|
| 71 |
|
|
|
|
| 72 |
with gr.Row():
|
| 73 |
+
with gr.Column(scale=2):
|
| 74 |
result = gr.Image(label="Generated Image", show_label=False, interactive=False)
|
| 75 |
with gr.Column(scale=1):
|
| 76 |
+
with gr.Box(elem_classes="generate-box"):
|
| 77 |
+
prompt = gr.Text(
|
| 78 |
+
label="Prompt",
|
| 79 |
+
placeholder="sexy woman & man , under wear, full body, sunday",
|
| 80 |
+
lines=3,
|
| 81 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
with gr.Row():
|
| 83 |
+
generateBtn = gr.Button("Generate Image", variant="primary")
|
| 84 |
+
enhanceBtn = gr.Button("Enhance Image", variant="secondary")
|
| 85 |
+
|
| 86 |
+
latency = gr.Text(label="Generation Time")
|
| 87 |
+
|
| 88 |
+
with gr.Accordion("Advanced Options", open=False, elem_classes="advanced-options"):
|
| 89 |
with gr.Row():
|
| 90 |
seed = gr.Number(label="Seed", value=42)
|
| 91 |
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 92 |
with gr.Row():
|
| 93 |
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=DEFAULT_WIDTH)
|
| 94 |
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=DEFAULT_HEIGHT)
|
|
|
|
| 95 |
|
| 96 |
+
with gr.Box(elem_classes="examples-gallery"):
|
| 97 |
+
gr.Markdown("###Gallery")
|
|
|
|
| 98 |
gr.Examples(
|
| 99 |
examples=examples,
|
| 100 |
fn=generate_image,
|
|
|
|
| 108 |
inputs=[prompt, seed, width, height],
|
| 109 |
outputs=[result, seed, latency],
|
| 110 |
show_progress="full",
|
|
|
|
|
|
|
| 111 |
)
|
| 112 |
|
| 113 |
generateBtn.click(
|
| 114 |
fn=generate_image,
|
| 115 |
+
inputs=[prompt, seed, width, height, randomize_seed],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
outputs=[result, seed, latency],
|
| 117 |
show_progress="full",
|
| 118 |
+
api_name="GenerateImage",
|
|
|
|
| 119 |
)
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
# Launch the app
|
| 122 |
+
demo.launch()
|