appsnprojectsstpl-tech commited on
Commit ·
6dee846
1
Parent(s): c5491ef
Switch to FLUX.1-schnell model
Browse files- app.py +42 -45
- requirements.txt +0 -0
app.py
CHANGED
|
@@ -1,63 +1,55 @@
|
|
| 1 |
import torch
|
| 2 |
import spaces
|
| 3 |
import gradio as gr
|
| 4 |
-
from diffusers import
|
| 5 |
-
from diffusers.utils import load_image
|
| 6 |
|
| 7 |
-
print("Loading Models...")
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
)
|
| 14 |
-
|
| 15 |
-
# 2. Instruct-Pix2Pix Model (Instruction-Based Editing)
|
| 16 |
-
pipe_edit = StableDiffusionInstructPix2PixPipeline.from_pretrained(
|
| 17 |
-
"timbrooks/instruct-pix2pix",
|
| 18 |
-
torch_dtype=torch.float16,
|
| 19 |
-
safety_checker=None
|
| 20 |
)
|
| 21 |
|
|
|
|
| 22 |
print("Models loaded successfully!")
|
| 23 |
|
| 24 |
@spaces.GPU
|
| 25 |
-
def generate_or_edit(prompt, input_image,
|
|
|
|
| 26 |
pipe_t2i.to("cuda")
|
| 27 |
pipe_edit.to("cuda")
|
|
|
|
| 28 |
if randomize_seed:
|
| 29 |
seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
| 30 |
generator = torch.Generator("cuda").manual_seed(int(seed))
|
| 31 |
|
| 32 |
if not prompt:
|
| 33 |
-
raise gr.Error("Please enter a prompt
|
| 34 |
|
| 35 |
if input_image is not None:
|
| 36 |
-
# Edit mode
|
| 37 |
input_image = input_image.convert("RGB")
|
| 38 |
-
# Resize image for SD1.5 (InstructPix2Pix)
|
| 39 |
-
input_image = input_image.resize((512, 512))
|
| 40 |
-
|
| 41 |
image = pipe_edit(
|
| 42 |
-
prompt,
|
| 43 |
image=input_image,
|
|
|
|
| 44 |
num_inference_steps=int(num_inference_steps),
|
| 45 |
-
|
| 46 |
-
guidance_scale=guidance_scale,
|
| 47 |
generator=generator,
|
| 48 |
).images[0]
|
| 49 |
else:
|
| 50 |
-
# Generate mode
|
| 51 |
image = pipe_t2i(
|
| 52 |
-
prompt,
|
| 53 |
num_inference_steps=int(num_inference_steps),
|
| 54 |
-
guidance_scale=
|
| 55 |
generator=generator,
|
| 56 |
).images[0]
|
| 57 |
|
| 58 |
return image, seed
|
| 59 |
|
| 60 |
-
#
|
| 61 |
custom_theme = gr.themes.Soft(
|
| 62 |
primary_hue="blue",
|
| 63 |
secondary_hue="indigo",
|
|
@@ -67,34 +59,40 @@ custom_theme = gr.themes.Soft(
|
|
| 67 |
with gr.Blocks(theme=custom_theme, fill_height=True) as demo:
|
| 68 |
gr.Markdown(
|
| 69 |
"""
|
| 70 |
-
#
|
| 71 |
-
Generate images from scratch, or
|
| 72 |
"""
|
| 73 |
)
|
| 74 |
|
| 75 |
with gr.Row():
|
| 76 |
with gr.Column(scale=1):
|
| 77 |
prompt = gr.Textbox(
|
| 78 |
-
label="✨
|
| 79 |
lines=3,
|
| 80 |
-
placeholder="e.g.
|
| 81 |
autofocus=True
|
| 82 |
)
|
| 83 |
input_image = gr.Image(
|
| 84 |
-
label="🖼️ Input Image (Optional -
|
| 85 |
type="pil"
|
| 86 |
)
|
| 87 |
|
| 88 |
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
)
|
| 99 |
|
| 100 |
with gr.Row():
|
|
@@ -113,15 +111,14 @@ with gr.Blocks(theme=custom_theme, fill_height=True) as demo:
|
|
| 113 |
output_image = gr.Image(label="Result", type="pil", interactive=False)
|
| 114 |
used_seed = gr.Number(label="Seed Used", interactive=False)
|
| 115 |
|
| 116 |
-
# Connections
|
| 117 |
generate_btn.click(
|
| 118 |
fn=generate_or_edit,
|
| 119 |
-
inputs=[prompt, input_image,
|
| 120 |
outputs=[output_image, used_seed]
|
| 121 |
)
|
| 122 |
prompt.submit(
|
| 123 |
fn=generate_or_edit,
|
| 124 |
-
inputs=[prompt, input_image,
|
| 125 |
outputs=[output_image, used_seed]
|
| 126 |
)
|
| 127 |
|
|
|
|
| 1 |
import torch
|
| 2 |
import spaces
|
| 3 |
import gradio as gr
|
| 4 |
+
from diffusers import FluxPipeline, FluxImg2ImgPipeline
|
|
|
|
| 5 |
|
| 6 |
+
print("Loading FLUX.1 Models (CPU)...")
|
| 7 |
+
# Load the base FLUX model
|
| 8 |
+
# We load the pipelines on CPU so ZeroGPU doesn't crash during startup.
|
| 9 |
+
pipe_t2i = FluxPipeline.from_pretrained(
|
| 10 |
+
"black-forest-labs/FLUX.1-schnell",
|
| 11 |
+
torch_dtype=torch.bfloat16,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
)
|
| 13 |
|
| 14 |
+
pipe_edit = FluxImg2ImgPipeline.from_pipe(pipe_t2i)
|
| 15 |
print("Models loaded successfully!")
|
| 16 |
|
| 17 |
@spaces.GPU
|
| 18 |
+
def generate_or_edit(prompt, input_image, denoising_strength, num_inference_steps, seed, randomize_seed, progress=gr.Progress(track_tqdm=True)):
|
| 19 |
+
# Move pipelines to GPU inside the ZeroGPU decorated function
|
| 20 |
pipe_t2i.to("cuda")
|
| 21 |
pipe_edit.to("cuda")
|
| 22 |
+
|
| 23 |
if randomize_seed:
|
| 24 |
seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
| 25 |
generator = torch.Generator("cuda").manual_seed(int(seed))
|
| 26 |
|
| 27 |
if not prompt:
|
| 28 |
+
raise gr.Error("Please enter a prompt!")
|
| 29 |
|
| 30 |
if input_image is not None:
|
| 31 |
+
# Edit mode (Img2Img)
|
| 32 |
input_image = input_image.convert("RGB")
|
|
|
|
|
|
|
|
|
|
| 33 |
image = pipe_edit(
|
| 34 |
+
prompt=prompt,
|
| 35 |
image=input_image,
|
| 36 |
+
strength=denoising_strength,
|
| 37 |
num_inference_steps=int(num_inference_steps),
|
| 38 |
+
guidance_scale=0.0, # FLUX.1-schnell uses 0 guidance scale
|
|
|
|
| 39 |
generator=generator,
|
| 40 |
).images[0]
|
| 41 |
else:
|
| 42 |
+
# Generate mode (T2I)
|
| 43 |
image = pipe_t2i(
|
| 44 |
+
prompt=prompt,
|
| 45 |
num_inference_steps=int(num_inference_steps),
|
| 46 |
+
guidance_scale=0.0, # FLUX.1-schnell uses 0 guidance scale
|
| 47 |
generator=generator,
|
| 48 |
).images[0]
|
| 49 |
|
| 50 |
return image, seed
|
| 51 |
|
| 52 |
+
# UI
|
| 53 |
custom_theme = gr.themes.Soft(
|
| 54 |
primary_hue="blue",
|
| 55 |
secondary_hue="indigo",
|
|
|
|
| 59 |
with gr.Blocks(theme=custom_theme, fill_height=True) as demo:
|
| 60 |
gr.Markdown(
|
| 61 |
"""
|
| 62 |
+
# ⚡ FLUX.1 Image Studio (Grok Quality)
|
| 63 |
+
Generate state-of-the-art images from scratch, or edit existing ones using the FLUX.1-schnell model.
|
| 64 |
"""
|
| 65 |
)
|
| 66 |
|
| 67 |
with gr.Row():
|
| 68 |
with gr.Column(scale=1):
|
| 69 |
prompt = gr.Textbox(
|
| 70 |
+
label="✨ Prompt",
|
| 71 |
lines=3,
|
| 72 |
+
placeholder="e.g. A cyberpunk cat...",
|
| 73 |
autofocus=True
|
| 74 |
)
|
| 75 |
input_image = gr.Image(
|
| 76 |
+
label="🖼️ Input Image (Optional - For editing)",
|
| 77 |
type="pil"
|
| 78 |
)
|
| 79 |
|
| 80 |
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 81 |
+
denoising_strength = gr.Slider(
|
| 82 |
+
minimum=0.0,
|
| 83 |
+
maximum=1.0,
|
| 84 |
+
value=0.5,
|
| 85 |
+
step=0.05,
|
| 86 |
+
label="Denoising Strength (Editing Only)",
|
| 87 |
+
info="Lower = keeps more of original image. Higher = completely changes image to match prompt."
|
| 88 |
+
)
|
| 89 |
+
num_inference_steps = gr.Slider(
|
| 90 |
+
minimum=1,
|
| 91 |
+
maximum=12,
|
| 92 |
+
value=4,
|
| 93 |
+
step=1,
|
| 94 |
+
label="Inference Steps",
|
| 95 |
+
info="FLUX.1-schnell is optimized for 4 steps."
|
| 96 |
)
|
| 97 |
|
| 98 |
with gr.Row():
|
|
|
|
| 111 |
output_image = gr.Image(label="Result", type="pil", interactive=False)
|
| 112 |
used_seed = gr.Number(label="Seed Used", interactive=False)
|
| 113 |
|
|
|
|
| 114 |
generate_btn.click(
|
| 115 |
fn=generate_or_edit,
|
| 116 |
+
inputs=[prompt, input_image, denoising_strength, num_inference_steps, seed, randomize_seed],
|
| 117 |
outputs=[output_image, used_seed]
|
| 118 |
)
|
| 119 |
prompt.submit(
|
| 120 |
fn=generate_or_edit,
|
| 121 |
+
inputs=[prompt, input_image, denoising_strength, num_inference_steps, seed, randomize_seed],
|
| 122 |
outputs=[output_image, used_seed]
|
| 123 |
)
|
| 124 |
|
requirements.txt
CHANGED
|
Binary files a/requirements.txt and b/requirements.txt differ
|
|
|