superbsaeed commited on
Commit
8e3fcf8
·
verified ·
1 Parent(s): 045ba62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -27
app.py CHANGED
@@ -6,54 +6,52 @@ from diffusers import FluxPipeline
6
  from huggingface_hub import login
7
 
8
  # --- AUTHENTICATION ---
 
9
  token = os.getenv("HF_TOKEN")
10
  if token:
11
  login(token=token)
12
 
13
- # --- MODEL LOADING ---
14
  model_id = "black-forest-labs/FLUX.1-schnell"
15
 
16
- # Load the pipe ONCE.
17
- # Do NOT move it to cuda here; ZeroGPU handles the transfer.
18
  pipe = FluxPipeline.from_pretrained(
19
  model_id,
20
  torch_dtype=torch.bfloat16,
21
- token=token
 
22
  )
23
 
24
  @spaces.GPU(duration=60)
25
- def generate_image(prompt, seed, width, height, steps):
26
- # ZeroGPU automatically moves 'pipe' to CUDA when this function starts
27
- # and moves it back to CPU when it finishes.
28
-
29
- generator = torch.Generator("cuda").manual_seed(int(seed))
30
 
31
  image = pipe(
32
  prompt=prompt,
33
- width=width,
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(share=True)
 
6
  from huggingface_hub import login
7
 
8
  # --- AUTHENTICATION ---
9
+ # Ensure your secret is named HF_TOKEN in Space Settings
10
  token = os.getenv("HF_TOKEN")
11
  if token:
12
  login(token=token)
13
 
14
+ # --- MODEL LOADING WITH GATED CHECK ---
15
  model_id = "black-forest-labs/FLUX.1-schnell"
16
 
 
 
17
  pipe = FluxPipeline.from_pretrained(
18
  model_id,
19
  torch_dtype=torch.bfloat16,
20
+ token=token, # Explicitly passing token for gated access
21
+ use_auth_token=token # Backup parameter for older diffusers versions
22
  )
23
 
24
  @spaces.GPU(duration=60)
25
+ def generate_image(prompt):
26
+ # Hidden settings for a clean UI
27
+ generator = torch.Generator("cuda").manual_seed(42)
 
 
28
 
29
  image = pipe(
30
  prompt=prompt,
31
+ width=1024,
32
+ height=1024,
33
+ num_inference_steps=4,
34
  generator=generator,
35
  guidance_scale=0.0
36
  ).images[0]
37
 
38
  return image
39
 
40
+ # --- MINIMALIST UI ---
41
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
42
+ gr.Markdown("# FLUX.1 Minimal")
43
+
44
+ with gr.Group():
45
+ prompt = gr.Textbox(
46
+ placeholder="Describe your image...",
47
+ show_label=False,
48
+ container=False
49
+ )
50
+ btn = gr.Button("Generate", variant="primary")
51
+
52
+ output_img = gr.Image(show_label=False, interactive=False)
53
 
54
+ btn.click(fn=generate_image, inputs=[prompt], outputs=output_img)
55
 
56
  if __name__ == "__main__":
57
+ demo.launch()