Spaces:
Runtime error
Runtime error
updated-added api endpoint
Browse files
app.py
CHANGED
|
@@ -8,6 +8,7 @@ from diffusers import DiffusionPipeline
|
|
| 8 |
dtype = torch.bfloat16
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
|
|
|
|
| 11 |
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
|
| 12 |
|
| 13 |
MAX_SEED = np.iinfo(np.int32).max
|
|
@@ -19,11 +20,11 @@ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_in
|
|
| 19 |
seed = random.randint(0, MAX_SEED)
|
| 20 |
generator = torch.Generator().manual_seed(seed)
|
| 21 |
image = pipe(
|
| 22 |
-
prompt
|
| 23 |
-
width
|
| 24 |
-
height
|
| 25 |
-
num_inference_steps
|
| 26 |
-
generator
|
| 27 |
guidance_scale=0.0
|
| 28 |
).images[0]
|
| 29 |
return image, seed
|
|
@@ -34,15 +35,15 @@ examples = [
|
|
| 34 |
"an anime illustration of a wiener schnitzel",
|
| 35 |
]
|
| 36 |
|
| 37 |
-
css="""
|
| 38 |
#col-container {
|
| 39 |
margin: 0 auto;
|
| 40 |
max-width: 520px;
|
| 41 |
}
|
| 42 |
"""
|
| 43 |
|
|
|
|
| 44 |
with gr.Blocks(css=css) as demo:
|
| 45 |
-
|
| 46 |
with gr.Column(elem_id="col-container"):
|
| 47 |
gr.Markdown(f"""# FLUX.1 [schnell]
|
| 48 |
12B param rectified flow transformer distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/) for 4 step generation
|
|
@@ -50,7 +51,6 @@ with gr.Blocks(css=css) as demo:
|
|
| 50 |
""")
|
| 51 |
|
| 52 |
with gr.Row():
|
| 53 |
-
|
| 54 |
prompt = gr.Text(
|
| 55 |
label="Prompt",
|
| 56 |
show_label=False,
|
|
@@ -58,13 +58,11 @@ with gr.Blocks(css=css) as demo:
|
|
| 58 |
placeholder="Enter your prompt",
|
| 59 |
container=False,
|
| 60 |
)
|
| 61 |
-
|
| 62 |
run_button = gr.Button("Run", scale=0)
|
| 63 |
|
| 64 |
result = gr.Image(label="Result", show_label=False)
|
| 65 |
|
| 66 |
with gr.Accordion("Advanced Settings", open=False):
|
| 67 |
-
|
| 68 |
seed = gr.Slider(
|
| 69 |
label="Seed",
|
| 70 |
minimum=0,
|
|
@@ -72,11 +70,9 @@ with gr.Blocks(css=css) as demo:
|
|
| 72 |
step=1,
|
| 73 |
value=0,
|
| 74 |
)
|
| 75 |
-
|
| 76 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 77 |
|
| 78 |
with gr.Row():
|
| 79 |
-
|
| 80 |
width = gr.Slider(
|
| 81 |
label="Width",
|
| 82 |
minimum=256,
|
|
@@ -84,7 +80,6 @@ with gr.Blocks(css=css) as demo:
|
|
| 84 |
step=32,
|
| 85 |
value=1024,
|
| 86 |
)
|
| 87 |
-
|
| 88 |
height = gr.Slider(
|
| 89 |
label="Height",
|
| 90 |
minimum=256,
|
|
@@ -94,8 +89,6 @@ with gr.Blocks(css=css) as demo:
|
|
| 94 |
)
|
| 95 |
|
| 96 |
with gr.Row():
|
| 97 |
-
|
| 98 |
-
|
| 99 |
num_inference_steps = gr.Slider(
|
| 100 |
label="Number of inference steps",
|
| 101 |
minimum=1,
|
|
@@ -105,18 +98,20 @@ with gr.Blocks(css=css) as demo:
|
|
| 105 |
)
|
| 106 |
|
| 107 |
gr.Examples(
|
| 108 |
-
examples
|
| 109 |
-
fn
|
| 110 |
-
inputs
|
| 111 |
-
outputs
|
| 112 |
cache_examples="lazy"
|
| 113 |
)
|
| 114 |
|
|
|
|
| 115 |
gr.on(
|
| 116 |
triggers=[run_button.click, prompt.submit],
|
| 117 |
-
fn
|
| 118 |
-
inputs
|
| 119 |
-
outputs
|
|
|
|
| 120 |
)
|
| 121 |
|
| 122 |
-
demo.launch()
|
|
|
|
| 8 |
dtype = torch.bfloat16
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
|
| 11 |
+
# Load the model locally
|
| 12 |
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
|
| 13 |
|
| 14 |
MAX_SEED = np.iinfo(np.int32).max
|
|
|
|
| 20 |
seed = random.randint(0, MAX_SEED)
|
| 21 |
generator = torch.Generator().manual_seed(seed)
|
| 22 |
image = pipe(
|
| 23 |
+
prompt=prompt,
|
| 24 |
+
width=width,
|
| 25 |
+
height=height,
|
| 26 |
+
num_inference_steps=num_inference_steps,
|
| 27 |
+
generator=generator,
|
| 28 |
guidance_scale=0.0
|
| 29 |
).images[0]
|
| 30 |
return image, seed
|
|
|
|
| 35 |
"an anime illustration of a wiener schnitzel",
|
| 36 |
]
|
| 37 |
|
| 38 |
+
css = """
|
| 39 |
#col-container {
|
| 40 |
margin: 0 auto;
|
| 41 |
max-width: 520px;
|
| 42 |
}
|
| 43 |
"""
|
| 44 |
|
| 45 |
+
# Define the Gradio interface
|
| 46 |
with gr.Blocks(css=css) as demo:
|
|
|
|
| 47 |
with gr.Column(elem_id="col-container"):
|
| 48 |
gr.Markdown(f"""# FLUX.1 [schnell]
|
| 49 |
12B param rectified flow transformer distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/) for 4 step generation
|
|
|
|
| 51 |
""")
|
| 52 |
|
| 53 |
with gr.Row():
|
|
|
|
| 54 |
prompt = gr.Text(
|
| 55 |
label="Prompt",
|
| 56 |
show_label=False,
|
|
|
|
| 58 |
placeholder="Enter your prompt",
|
| 59 |
container=False,
|
| 60 |
)
|
|
|
|
| 61 |
run_button = gr.Button("Run", scale=0)
|
| 62 |
|
| 63 |
result = gr.Image(label="Result", show_label=False)
|
| 64 |
|
| 65 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
|
| 66 |
seed = gr.Slider(
|
| 67 |
label="Seed",
|
| 68 |
minimum=0,
|
|
|
|
| 70 |
step=1,
|
| 71 |
value=0,
|
| 72 |
)
|
|
|
|
| 73 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 74 |
|
| 75 |
with gr.Row():
|
|
|
|
| 76 |
width = gr.Slider(
|
| 77 |
label="Width",
|
| 78 |
minimum=256,
|
|
|
|
| 80 |
step=32,
|
| 81 |
value=1024,
|
| 82 |
)
|
|
|
|
| 83 |
height = gr.Slider(
|
| 84 |
label="Height",
|
| 85 |
minimum=256,
|
|
|
|
| 89 |
)
|
| 90 |
|
| 91 |
with gr.Row():
|
|
|
|
|
|
|
| 92 |
num_inference_steps = gr.Slider(
|
| 93 |
label="Number of inference steps",
|
| 94 |
minimum=1,
|
|
|
|
| 98 |
)
|
| 99 |
|
| 100 |
gr.Examples(
|
| 101 |
+
examples=examples,
|
| 102 |
+
fn=infer,
|
| 103 |
+
inputs=[prompt],
|
| 104 |
+
outputs=[result, seed],
|
| 105 |
cache_examples="lazy"
|
| 106 |
)
|
| 107 |
|
| 108 |
+
# Add API endpoint setup
|
| 109 |
gr.on(
|
| 110 |
triggers=[run_button.click, prompt.submit],
|
| 111 |
+
fn=infer,
|
| 112 |
+
inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps],
|
| 113 |
+
outputs=[result, seed],
|
| 114 |
+
api_name="generate_image" # Expose this function as an API
|
| 115 |
)
|
| 116 |
|
| 117 |
+
demo.launch()
|