Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
generator = torch.Generator("cuda").manual_seed(int(seed))
|
| 30 |
|
| 31 |
image = pipe(
|
| 32 |
prompt=prompt,
|
| 33 |
-
width=
|
| 34 |
-
height=
|
| 35 |
-
num_inference_steps=
|
| 36 |
generator=generator,
|
| 37 |
guidance_scale=0.0
|
| 38 |
).images[0]
|
| 39 |
|
| 40 |
return image
|
| 41 |
|
| 42 |
-
# ---
|
| 43 |
-
with gr.Blocks() as demo:
|
| 44 |
-
gr.Markdown("#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
-
btn.click(fn=generate_image, inputs=[prompt
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
| 59 |
-
demo.launch(
|
|
|
|
| 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()
|