superbsaeed commited on
Commit
a98ebd8
·
verified ·
1 Parent(s): e5f8317

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import spaces
4
+ from diffusers import FluxPipeline
5
+
6
+ # 1. Initialize the Pipeline
7
+ model_id = "black-forest-labs/FLUX.1-schnell"
8
+
9
+ # Load the model with bfloat16 to save memory and improve speed
10
+ pipe = FluxPipeline.from_pretrained(
11
+ model_id,
12
+ torch_dtype=torch.bfloat16
13
+ )
14
+
15
+ # 2. Define the Generation Function with ZeroGPU decorator
16
+ # The @spaces.GPU decorator handles the dynamic GPU allocation on Hugging Face
17
+ @spaces.GPU(duration=60)
18
+ def generate_image(prompt, seed, width, height, steps):
19
+ pipe.to("cuda") # Moves model to GPU only during execution
20
+
21
+ generator = torch.Generator("cuda").manual_seed(seed)
22
+
23
+ image = pipe(
24
+ prompt=prompt,
25
+ width=width,
26
+ height=height,
27
+ num_inference_steps=steps,
28
+ generator=generator,
29
+ guidance_scale=0.0 # Schnell version works best with 0 guidance
30
+ ).images[0]
31
+
32
+ return image
33
+
34
+ # 3. Create the Gradio Interface
35
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
36
+ gr.Markdown("# 🎨 FLUX.1-schnell Image Generator")
37
+ gr.Markdown("Generating high-quality images on Hugging Face ZeroGPU.")
38
+
39
+ with gr.Row():
40
+ with gr.Column():
41
+ prompt = gr.Textbox(label="Enter your prompt", placeholder="A neon-lit cyberpunk cat...")
42
+ generate_btn = gr.Button("Generate", variant="primary")
43
+
44
+ with gr.Accordion("Settings", open=False):
45
+ seed = gr.Slider(0, 1000000, label="Seed", value=42, step=1)
46
+ width = gr.Slider(512, 1024, label="Width", value=1024, step=64)
47
+ height = gr.Slider(512, 1024, label="Height", value=1024, step=64)
48
+ steps = gr.Slider(1, 4, label="Inference Steps", value=4, step=1)
49
+
50
+ with gr.Column():
51
+ output_image = gr.Image(label="Generated Image")
52
+
53
+ generate_btn.click(
54
+ fn=generate_image,
55
+ inputs=[prompt, seed, width, height, steps],
56
+ outputs=output_image
57
+ )
58
+
59
+ # 4. Launch the App
60
+ if __name__ == "__main__":
61
+ demo.launch()