Update app.py
Browse files
app.py
CHANGED
|
@@ -2,12 +2,19 @@ import torch
|
|
| 2 |
from diffusers import FluxPipeline
|
| 3 |
from huggingface_hub import login
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
login(token="hf_yourtokenhere")
|
| 7 |
|
| 8 |
-
|
| 9 |
-
pipe
|
|
|
|
|
|
|
|
|
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
prompt = "A cat holding a sign that says hello world"
|
| 12 |
image = pipe(
|
| 13 |
prompt,
|
|
@@ -16,59 +23,7 @@ image = pipe(
|
|
| 16 |
max_sequence_length=256,
|
| 17 |
generator=torch.Generator("cpu").manual_seed(0)
|
| 18 |
).images[0]
|
| 19 |
-
image.save("flux-schnell.png")
|
| 20 |
-
import torch
|
| 21 |
-
from diffusers import FluxPipeline
|
| 22 |
-
import gradio as gr
|
| 23 |
-
|
| 24 |
-
def generate_image(prompt, guidance_scale, num_inference_steps, seed):
|
| 25 |
-
# Load the pipeline (you might want to move this outside the function for better performance)
|
| 26 |
-
pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16)
|
| 27 |
-
pipe.enable_model_cpu_offload()
|
| 28 |
-
|
| 29 |
-
# Generate the image
|
| 30 |
-
generator = torch.Generator("cpu").manual_seed(seed)
|
| 31 |
-
image = pipe(
|
| 32 |
-
prompt,
|
| 33 |
-
guidance_scale=guidance_scale,
|
| 34 |
-
num_inference_steps=num_inference_steps,
|
| 35 |
-
max_sequence_length=256,
|
| 36 |
-
generator=generator
|
| 37 |
-
).images[0]
|
| 38 |
-
|
| 39 |
-
return image
|
| 40 |
-
|
| 41 |
-
# Create the Gradio interface
|
| 42 |
-
with gr.Blocks() as demo:
|
| 43 |
-
gr.Markdown("# FLUX.1-schnell Image Generator")
|
| 44 |
-
|
| 45 |
-
with gr.Row():
|
| 46 |
-
with gr.Column():
|
| 47 |
-
prompt = gr.Textbox(label="Prompt", value="A cat holding a sign that says hello world")
|
| 48 |
-
guidance_scale = gr.Slider(minimum=0.0, maximum=20.0, value=0.0, label="Guidance Scale")
|
| 49 |
-
num_steps = gr.Slider(minimum=1, maximum=20, step=1, value=4, label="Inference Steps")
|
| 50 |
-
seed = gr.Number(value=0, label="Seed", precision=0)
|
| 51 |
-
submit_btn = gr.Button("Generate Image")
|
| 52 |
-
|
| 53 |
-
with gr.Column():
|
| 54 |
-
output_image = gr.Image(label="Generated Image")
|
| 55 |
-
|
| 56 |
-
submit_btn.click(
|
| 57 |
-
fn=generate_image,
|
| 58 |
-
inputs=[prompt, guidance_scale, num_steps, seed],
|
| 59 |
-
outputs=output_image
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
gr.Examples(
|
| 63 |
-
examples=[
|
| 64 |
-
["A futuristic cityscape at sunset, digital art", 0.0, 4, 42],
|
| 65 |
-
["An astronaut riding a horse on Mars, photorealistic", 0.0, 6, 123],
|
| 66 |
-
["A steampunk library with floating books", 0.0, 4, 7]
|
| 67 |
-
],
|
| 68 |
-
inputs=[prompt, guidance_scale, num_steps, seed],
|
| 69 |
-
outputs=output_image,
|
| 70 |
-
fn=generate_image,
|
| 71 |
-
cache_examples=True
|
| 72 |
-
)
|
| 73 |
|
| 74 |
-
|
|
|
|
|
|
|
|
|
| 2 |
from diffusers import FluxPipeline
|
| 3 |
from huggingface_hub import login
|
| 4 |
|
| 5 |
+
# Authenticate (required for gated models)
|
| 6 |
+
login(token="hf_yourtokenhere") # Replace with your token
|
| 7 |
|
| 8 |
+
# Load the model (use bfloat16 for faster inference + less VRAM)
|
| 9 |
+
pipe = FluxPipeline.from_pretrained(
|
| 10 |
+
"black-forest-labs/FLUX.1-schnell",
|
| 11 |
+
torch_dtype=torch.bfloat16
|
| 12 |
+
)
|
| 13 |
|
| 14 |
+
# Enable CPU offloading if you have limited GPU memory
|
| 15 |
+
pipe.enable_model_cpu_offload()
|
| 16 |
+
|
| 17 |
+
# Generate an image
|
| 18 |
prompt = "A cat holding a sign that says hello world"
|
| 19 |
image = pipe(
|
| 20 |
prompt,
|
|
|
|
| 23 |
max_sequence_length=256,
|
| 24 |
generator=torch.Generator("cpu").manual_seed(0)
|
| 25 |
).images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# Save the output
|
| 28 |
+
image.save("flux-schnell-output.png")
|
| 29 |
+
print("Image saved successfully!")
|