Spaces:
Runtime error
Runtime error
Jordan Legg
commited on
Commit
β’
2e306db
1
Parent(s):
126a4f5
send it
Browse files- app.py +128 -42
- requirements.txt +1 -1
app.py
CHANGED
@@ -1,60 +1,146 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
|
|
3 |
import spaces
|
4 |
-
|
|
|
5 |
|
|
|
|
|
6 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
7 |
-
|
8 |
-
|
9 |
-
MODEL_ID = "drbaph/FLUX.1-schnell-dev-merged-fp8-4step"
|
10 |
-
MODEL_FILE = "flux1-schnell-dev-merged-fp8-4step.safetensors"
|
11 |
-
|
12 |
-
def load_model():
|
13 |
-
pipe = FluxPipeline.from_single_file(
|
14 |
-
f"https://huggingface.co/{MODEL_ID}/resolve/main/{MODEL_FILE}",
|
15 |
-
torch_dtype=dtype
|
16 |
-
)
|
17 |
-
pipe.to(device)
|
18 |
-
return pipe
|
19 |
-
|
20 |
-
pipe = load_model()
|
21 |
-
|
22 |
-
MAX_SEED = 2**32 - 1
|
23 |
MAX_IMAGE_SIZE = 2048
|
24 |
|
|
|
|
|
|
|
25 |
@spaces.GPU()
|
26 |
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
|
27 |
if randomize_seed:
|
28 |
-
seed =
|
29 |
-
generator = torch.Generator(
|
30 |
image = pipe(
|
31 |
-
prompt=prompt,
|
32 |
width=width,
|
33 |
height=height,
|
34 |
-
num_inference_steps=num_inference_steps,
|
35 |
generator=generator,
|
36 |
-
guidance_scale=0.0
|
37 |
-
max_sequence_length=256
|
38 |
).images[0]
|
39 |
return image, seed
|
40 |
|
41 |
-
#
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import random
|
4 |
import spaces
|
5 |
+
import torch
|
6 |
+
from diffusers import DiffusionPipeline
|
7 |
|
8 |
+
# Define constants
|
9 |
+
dtype = torch.bfloat16
|
10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
+
MAX_SEED = np.iinfo(np.int32).max
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
MAX_IMAGE_SIZE = 2048
|
13 |
|
14 |
+
# Load the diffusion pipeline
|
15 |
+
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
|
16 |
+
|
17 |
@spaces.GPU()
|
18 |
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
|
19 |
if randomize_seed:
|
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
|
31 |
|
32 |
+
# Define example prompts
|
33 |
+
examples = [
|
34 |
+
"a tiny astronaut hatching from an egg on the moon",
|
35 |
+
"a cat holding a sign that says hello world",
|
36 |
+
"an anime illustration of a wiener schnitzel",
|
37 |
+
]
|
38 |
+
|
39 |
+
# CSS styling for the Japanese-inspired interface
|
40 |
+
css = """
|
41 |
+
body {
|
42 |
+
background-color: #fff;
|
43 |
+
font-family: 'Noto Sans JP', sans-serif;
|
44 |
+
color: #333;
|
45 |
+
}
|
46 |
+
#col-container {
|
47 |
+
margin: 0 auto;
|
48 |
+
max-width: 520px;
|
49 |
+
border: 2px solid #000;
|
50 |
+
padding: 20px;
|
51 |
+
background-color: #f7f7f7;
|
52 |
+
border-radius: 10px;
|
53 |
+
}
|
54 |
+
.gr-button {
|
55 |
+
background-color: #e60012;
|
56 |
+
color: #fff;
|
57 |
+
border: 2px solid #000;
|
58 |
+
}
|
59 |
+
.gr-button:hover {
|
60 |
+
background-color: #c20010;
|
61 |
+
}
|
62 |
+
.gr-slider, .gr-checkbox, .gr-textbox {
|
63 |
+
border: 2px solid #000;
|
64 |
+
}
|
65 |
+
.gr-accordion {
|
66 |
+
border: 2px solid #000;
|
67 |
+
background-color: #fff;
|
68 |
+
}
|
69 |
+
.gr-image {
|
70 |
+
border: 2px solid #000;
|
71 |
+
}
|
72 |
+
"""
|
73 |
+
|
74 |
+
# Create the Gradio interface
|
75 |
+
with gr.Blocks(css=css) as demo:
|
76 |
|
77 |
+
with gr.Column(elem_id="col-container"):
|
78 |
+
gr.Markdown("""
|
79 |
+
# FLUX.1 [schnell]
|
80 |
+
12B param rectified flow transformer distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/) for 4 step generation
|
81 |
+
[[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-schnell)]
|
82 |
+
""")
|
83 |
+
|
84 |
+
with gr.Row():
|
85 |
+
prompt = gr.Textbox(
|
86 |
+
label="Prompt",
|
87 |
+
show_label=False,
|
88 |
+
max_lines=1,
|
89 |
+
placeholder="Enter your prompt",
|
90 |
+
container=False,
|
91 |
+
)
|
92 |
+
run_button = gr.Button("Run", scale=0)
|
93 |
+
|
94 |
+
result = gr.Image(label="Result", show_label=False)
|
95 |
+
|
96 |
+
with gr.Accordion("Advanced Settings", open=False):
|
97 |
+
seed = gr.Slider(
|
98 |
+
label="Seed",
|
99 |
+
minimum=0,
|
100 |
+
maximum=MAX_SEED,
|
101 |
+
step=1,
|
102 |
+
value=42,
|
103 |
+
)
|
104 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
105 |
+
|
106 |
+
with gr.Row():
|
107 |
+
width = gr.Slider(
|
108 |
+
label="Width",
|
109 |
+
minimum=256,
|
110 |
+
maximum=MAX_IMAGE_SIZE,
|
111 |
+
step=32,
|
112 |
+
value=1024,
|
113 |
+
)
|
114 |
+
height = gr.Slider(
|
115 |
+
label="Height",
|
116 |
+
minimum=256,
|
117 |
+
maximum=MAX_IMAGE_SIZE,
|
118 |
+
step=32,
|
119 |
+
value=1024,
|
120 |
+
)
|
121 |
+
|
122 |
+
with gr.Row():
|
123 |
+
num_inference_steps = gr.Slider(
|
124 |
+
label="Number of inference steps",
|
125 |
+
minimum=1,
|
126 |
+
maximum=50,
|
127 |
+
step=1,
|
128 |
+
value=4,
|
129 |
+
)
|
130 |
+
|
131 |
+
gr.Examples(
|
132 |
+
examples=examples,
|
133 |
+
fn=infer,
|
134 |
+
inputs=[prompt],
|
135 |
+
outputs=[result, seed],
|
136 |
+
cache_examples="lazy"
|
137 |
+
)
|
138 |
+
|
139 |
+
gr.on(
|
140 |
+
triggers=[run_button.click, prompt.submit],
|
141 |
+
fn=infer,
|
142 |
+
inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps],
|
143 |
+
outputs=[result, seed]
|
144 |
+
)
|
145 |
|
146 |
+
demo.launch()
|
requirements.txt
CHANGED
@@ -2,6 +2,6 @@ accelerate
|
|
2 |
diffusers
|
3 |
invisible_watermark
|
4 |
torch
|
5 |
-
transformers
|
6 |
xformers
|
7 |
sentencepiece
|
|
|
2 |
diffusers
|
3 |
invisible_watermark
|
4 |
torch
|
5 |
+
transformers
|
6 |
xformers
|
7 |
sentencepiece
|