superbsaeed commited on
Commit
dc03b48
·
verified ·
1 Parent(s): 5c08fcb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -33
app.py CHANGED
@@ -1,27 +1,32 @@
 
1
  import gradio as gr
2
  import torch
3
  import spaces
4
  from diffusers import FluxPipeline
5
- import os
 
 
 
 
 
 
 
 
6
 
7
- # 1. Initialize the Pipeline
8
- hf_token = os.getenv("FLUX_API_TOKEN")
9
  model_id = "black-forest-labs/FLUX.1-schnell"
10
 
11
- # Load the model with bfloat16 to save memory and improve speed
12
  pipe = FluxPipeline.from_pretrained(
13
  model_id,
14
  torch_dtype=torch.bfloat16,
15
- hf_token = os.getenv("FLUX_API_TOKEN")
16
  )
17
 
18
- # 2. Define the Generation Function with ZeroGPU decorator
19
- # The @spaces.GPU decorator handles the dynamic GPU allocation on Hugging Face
20
  @spaces.GPU(duration=60)
21
  def generate_image(prompt, seed, width, height, steps):
22
- pipe.to("cuda") # Moves model to GPU only during execution
23
-
24
- generator = torch.Generator("cuda").manual_seed(seed)
25
 
26
  image = pipe(
27
  prompt=prompt,
@@ -29,36 +34,26 @@ def generate_image(prompt, seed, width, height, steps):
29
  height=height,
30
  num_inference_steps=steps,
31
  generator=generator,
32
- guidance_scale=0.0 # Schnell version works best with 0 guidance
33
  ).images[0]
34
 
35
  return image
36
 
37
- # 3. Create the Gradio Interface
38
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
39
- gr.Markdown("# 🎨 FLUX.1-schnell Image Generator")
40
- gr.Markdown("Generating high-quality images on Hugging Face ZeroGPU.")
41
-
42
  with gr.Row():
43
  with gr.Column():
44
- prompt = gr.Textbox(label="Enter your prompt", placeholder="A neon-lit cyberpunk cat...")
45
- generate_btn = gr.Button("Generate", variant="primary")
46
-
47
- with gr.Accordion("Settings", open=False):
48
- seed = gr.Slider(0, 1000000, label="Seed", value=42, step=1)
49
- width = gr.Slider(512, 1024, label="Width", value=1024, step=64)
50
- height = gr.Slider(512, 1024, label="Height", value=1024, step=64)
51
- steps = gr.Slider(1, 4, label="Inference Steps", value=4, step=1)
52
-
53
- with gr.Column():
54
- output_image = gr.Image(label="Generated Image")
55
 
56
- generate_btn.click(
57
- fn=generate_image,
58
- inputs=[prompt, seed, width, height, steps],
59
- outputs=output_image
60
- )
61
 
62
- # 4. Launch the App
63
  if __name__ == "__main__":
64
  demo.launch()
 
1
+ import os
2
  import gradio as gr
3
  import torch
4
  import spaces
5
  from diffusers import FluxPipeline
6
+ from huggingface_hub import login
7
+
8
+ # --- AUTHENTICATION LAYER ---
9
+ # This ensures the session is logged in BEFORE loading the model
10
+ token = os.getenv("FLUX_API_TOKEN")
11
+ if token:
12
+ login(token=token)
13
+ else:
14
+ print("❌ Error: HF_TOKEN secret not found. Go to Settings > Secrets.")
15
 
16
+ # --- MODEL LOADING ---
 
17
  model_id = "black-forest-labs/FLUX.1-schnell"
18
 
19
+ # We pass the token explicitly here as well
20
  pipe = FluxPipeline.from_pretrained(
21
  model_id,
22
  torch_dtype=torch.bfloat16,
23
+ token=token
24
  )
25
 
 
 
26
  @spaces.GPU(duration=60)
27
  def generate_image(prompt, seed, width, height, steps):
28
+ pipe.to("cuda")
29
+ generator = torch.Generator("cuda").manual_seed(int(seed))
 
30
 
31
  image = pipe(
32
  prompt=prompt,
 
34
  height=height,
35
  num_inference_steps=steps,
36
  generator=generator,
37
+ guidance_scale=0.0
38
  ).images[0]
39
 
40
  return image
41
 
42
+ # --- GRADIO INTERFACE ---
43
+ with gr.Blocks() as demo:
44
+ gr.Markdown("# 🚀 FLUX.1 Schnell - Direct Access")
 
 
45
  with gr.Row():
46
  with gr.Column():
47
+ prompt = gr.Textbox(label="Prompt", placeholder="A glass sculpture of a bird...")
48
+ btn = gr.Button("Generate", variant="primary")
49
+ with gr.Accordion("Advanced", open=False):
50
+ seed = gr.Number(label="Seed", value=42)
51
+ width = gr.Slider(512, 1024, value=1024, step=64)
52
+ height = gr.Slider(512, 1024, value=1024, step=64)
53
+ steps = gr.Slider(1, 4, value=4, step=1)
54
+ output_img = gr.Image(label="Result")
 
 
 
55
 
56
+ btn.click(fn=generate_image, inputs=[prompt, seed, width, height, steps], outputs=output_img)
 
 
 
 
57
 
 
58
  if __name__ == "__main__":
59
  demo.launch()