Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
hf_token = os.getenv("FLUX_API_TOKEN")
|
| 9 |
model_id = "black-forest-labs/FLUX.1-schnell"
|
| 10 |
|
| 11 |
-
#
|
| 12 |
pipe = FluxPipeline.from_pretrained(
|
| 13 |
model_id,
|
| 14 |
torch_dtype=torch.bfloat16,
|
| 15 |
-
|
| 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")
|
| 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
|
| 33 |
).images[0]
|
| 34 |
|
| 35 |
return image
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
with gr.Blocks(
|
| 39 |
-
gr.Markdown("#
|
| 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="
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
with gr.Column():
|
| 54 |
-
output_image = gr.Image(label="Generated Image")
|
| 55 |
|
| 56 |
-
|
| 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()
|