Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,36 +6,44 @@ from diffusers import FluxPipeline
|
|
| 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
|
| 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
|
| 21 |
-
use_auth_token=token # Backup parameter for older diffusers versions
|
| 22 |
)
|
| 23 |
|
| 24 |
@spaces.GPU(duration=60)
|
| 25 |
def generate_image(prompt):
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
height=1024,
|
| 33 |
-
num_inference_steps=4,
|
| 34 |
-
generator=generator,
|
| 35 |
-
guidance_scale=0.0
|
| 36 |
-
).images[0]
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# --- MINIMALIST UI ---
|
| 41 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
@@ -54,4 +62,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 54 |
btn.click(fn=generate_image, inputs=[prompt], outputs=output_img)
|
| 55 |
|
| 56 |
if __name__ == "__main__":
|
| 57 |
-
demo.launch()
|
|
|
|
| 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 without .to("cuda") here. Let ZeroGPU handle it.
|
| 17 |
pipe = FluxPipeline.from_pretrained(
|
| 18 |
model_id,
|
| 19 |
torch_dtype=torch.bfloat16,
|
| 20 |
+
token=token
|
|
|
|
| 21 |
)
|
| 22 |
|
| 23 |
@spaces.GPU(duration=60)
|
| 24 |
def generate_image(prompt):
|
| 25 |
+
if not prompt:
|
| 26 |
+
return None
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
# ZeroGPU specific: ensure the pipe is on the right device
|
| 30 |
+
pipe.to("cuda")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
generator = torch.Generator("cuda").manual_seed(42)
|
| 33 |
+
|
| 34 |
+
image = pipe(
|
| 35 |
+
prompt=prompt,
|
| 36 |
+
width=1024,
|
| 37 |
+
height=1024,
|
| 38 |
+
num_inference_steps=4,
|
| 39 |
+
generator=generator,
|
| 40 |
+
guidance_scale=0.0
|
| 41 |
+
).images[0]
|
| 42 |
+
|
| 43 |
+
return image
|
| 44 |
+
except Exception as e:
|
| 45 |
+
# This will show the actual error message in the Gradio UI
|
| 46 |
+
raise gr.Error(f"Generation failed: {str(e)}")
|
| 47 |
|
| 48 |
# --- MINIMALIST UI ---
|
| 49 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
|
| 62 |
btn.click(fn=generate_image, inputs=[prompt], outputs=output_img)
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|
| 65 |
+
demo.launch(share=True)
|