Spaces:
Build error
Build error
add lora
Browse files- app.py +54 -38
- requirements.txt +3 -2
app.py
CHANGED
@@ -3,39 +3,60 @@ import numpy as np
|
|
3 |
import random
|
4 |
import spaces
|
5 |
import torch
|
6 |
-
from diffusers import
|
7 |
-
from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast
|
|
|
|
|
8 |
|
9 |
dtype = torch.bfloat16
|
10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
|
12 |
-
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16).to(device)
|
13 |
-
|
14 |
MAX_SEED = np.iinfo(np.int32).max
|
15 |
MAX_IMAGE_SIZE = 2048
|
16 |
|
|
|
|
|
|
|
17 |
@spaces.GPU(duration=190)
|
18 |
-
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=5.0, num_inference_steps=28, 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 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
examples = [
|
33 |
-
"a tiny astronaut hatching from an egg on the moon",
|
34 |
-
"a cat holding a sign that says hello world",
|
35 |
-
"an anime illustration of a wiener schnitzel",
|
36 |
]
|
37 |
|
38 |
-
css="""
|
39 |
#col-container {
|
40 |
margin: 0 auto;
|
41 |
max-width: 520px;
|
@@ -43,15 +64,13 @@ css="""
|
|
43 |
"""
|
44 |
|
45 |
with gr.Blocks(css=css) as demo:
|
46 |
-
|
47 |
with gr.Column(elem_id="col-container"):
|
48 |
-
gr.Markdown(f"""# FLUX.1 [dev]
|
49 |
12B param rectified flow transformer guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)
|
50 |
[[non-commercial license](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)] [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-dev)]
|
51 |
""")
|
52 |
|
53 |
with gr.Row():
|
54 |
-
|
55 |
prompt = gr.Text(
|
56 |
label="Prompt",
|
57 |
show_label=False,
|
@@ -59,13 +78,17 @@ with gr.Blocks(css=css) as demo:
|
|
59 |
placeholder="Enter your prompt",
|
60 |
container=False,
|
61 |
)
|
62 |
-
|
63 |
run_button = gr.Button("Run", scale=0)
|
64 |
|
|
|
|
|
|
|
|
|
|
|
65 |
result = gr.Image(label="Result", show_label=False)
|
|
|
66 |
|
67 |
with gr.Accordion("Advanced Settings", open=False):
|
68 |
-
|
69 |
seed = gr.Slider(
|
70 |
label="Seed",
|
71 |
minimum=0,
|
@@ -73,11 +96,8 @@ with gr.Blocks(css=css) as demo:
|
|
73 |
step=1,
|
74 |
value=0,
|
75 |
)
|
76 |
-
|
77 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
78 |
-
|
79 |
with gr.Row():
|
80 |
-
|
81 |
width = gr.Slider(
|
82 |
label="Width",
|
83 |
minimum=256,
|
@@ -85,7 +105,6 @@ with gr.Blocks(css=css) as demo:
|
|
85 |
step=32,
|
86 |
value=1024,
|
87 |
)
|
88 |
-
|
89 |
height = gr.Slider(
|
90 |
label="Height",
|
91 |
minimum=256,
|
@@ -93,9 +112,7 @@ with gr.Blocks(css=css) as demo:
|
|
93 |
step=32,
|
94 |
value=1024,
|
95 |
)
|
96 |
-
|
97 |
with gr.Row():
|
98 |
-
|
99 |
guidance_scale = gr.Slider(
|
100 |
label="Guidance Scale",
|
101 |
minimum=1,
|
@@ -103,7 +120,6 @@ with gr.Blocks(css=css) as demo:
|
|
103 |
step=0.1,
|
104 |
value=3.5,
|
105 |
)
|
106 |
-
|
107 |
num_inference_steps = gr.Slider(
|
108 |
label="Number of inference steps",
|
109 |
minimum=1,
|
@@ -113,18 +129,18 @@ with gr.Blocks(css=css) as demo:
|
|
113 |
)
|
114 |
|
115 |
gr.Examples(
|
116 |
-
examples
|
117 |
-
fn
|
118 |
-
inputs
|
119 |
-
outputs
|
120 |
cache_examples="lazy"
|
121 |
)
|
122 |
|
123 |
gr.on(
|
124 |
triggers=[run_button.click, prompt.submit],
|
125 |
-
fn
|
126 |
-
inputs
|
127 |
-
outputs
|
128 |
)
|
129 |
|
130 |
demo.launch()
|
|
|
3 |
import random
|
4 |
import spaces
|
5 |
import torch
|
6 |
+
from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler
|
7 |
+
from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5TokenizerFast
|
8 |
+
from huggingface_hub import hf_hub_download
|
9 |
+
import os
|
10 |
|
11 |
dtype = torch.bfloat16
|
12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
|
|
|
|
|
14 |
MAX_SEED = np.iinfo(np.int32).max
|
15 |
MAX_IMAGE_SIZE = 2048
|
16 |
|
17 |
+
# Initialize the pipeline globally
|
18 |
+
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16).to(device)
|
19 |
+
|
20 |
@spaces.GPU(duration=190)
|
21 |
+
def infer(prompt, lora_model, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=5.0, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
|
22 |
+
global pipe
|
23 |
+
|
24 |
+
# Load LoRA if specified
|
25 |
+
if lora_model:
|
26 |
+
try:
|
27 |
+
pipe.load_lora_weights(lora_model)
|
28 |
+
except Exception as e:
|
29 |
+
return None, seed, f"Failed to load LoRA model: {str(e)}"
|
30 |
+
|
31 |
if randomize_seed:
|
32 |
seed = random.randint(0, MAX_SEED)
|
33 |
generator = torch.Generator().manual_seed(seed)
|
34 |
+
|
35 |
+
try:
|
36 |
+
image = pipe(
|
37 |
+
prompt=prompt,
|
38 |
+
width=width,
|
39 |
+
height=height,
|
40 |
+
num_inference_steps=num_inference_steps,
|
41 |
+
generator=generator,
|
42 |
+
guidance_scale=guidance_scale
|
43 |
+
).images[0]
|
44 |
+
|
45 |
+
# Unload LoRA weights after generation
|
46 |
+
if lora_model:
|
47 |
+
pipe.unload_lora_weights()
|
48 |
+
|
49 |
+
return image, seed, "Image generated successfully."
|
50 |
+
except Exception as e:
|
51 |
+
return None, seed, f"Error during image generation: {str(e)}"
|
52 |
+
|
53 |
examples = [
|
54 |
+
["a tiny astronaut hatching from an egg on the moon", ""],
|
55 |
+
["a cat holding a sign that says hello world", ""],
|
56 |
+
["an anime illustration of a wiener schnitzel", ""],
|
57 |
]
|
58 |
|
59 |
+
css = """
|
60 |
#col-container {
|
61 |
margin: 0 auto;
|
62 |
max-width: 520px;
|
|
|
64 |
"""
|
65 |
|
66 |
with gr.Blocks(css=css) as demo:
|
|
|
67 |
with gr.Column(elem_id="col-container"):
|
68 |
+
gr.Markdown(f"""# FLUX.1 [dev] with LoRA Support
|
69 |
12B param rectified flow transformer guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)
|
70 |
[[non-commercial license](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)] [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-dev)]
|
71 |
""")
|
72 |
|
73 |
with gr.Row():
|
|
|
74 |
prompt = gr.Text(
|
75 |
label="Prompt",
|
76 |
show_label=False,
|
|
|
78 |
placeholder="Enter your prompt",
|
79 |
container=False,
|
80 |
)
|
|
|
81 |
run_button = gr.Button("Run", scale=0)
|
82 |
|
83 |
+
lora_model = gr.Text(
|
84 |
+
label="LoRA Model ID (optional)",
|
85 |
+
placeholder="Enter Hugging Face LoRA model ID",
|
86 |
+
)
|
87 |
+
|
88 |
result = gr.Image(label="Result", show_label=False)
|
89 |
+
output_message = gr.Textbox(label="Output Message")
|
90 |
|
91 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
92 |
seed = gr.Slider(
|
93 |
label="Seed",
|
94 |
minimum=0,
|
|
|
96 |
step=1,
|
97 |
value=0,
|
98 |
)
|
|
|
99 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
|
|
100 |
with gr.Row():
|
|
|
101 |
width = gr.Slider(
|
102 |
label="Width",
|
103 |
minimum=256,
|
|
|
105 |
step=32,
|
106 |
value=1024,
|
107 |
)
|
|
|
108 |
height = gr.Slider(
|
109 |
label="Height",
|
110 |
minimum=256,
|
|
|
112 |
step=32,
|
113 |
value=1024,
|
114 |
)
|
|
|
115 |
with gr.Row():
|
|
|
116 |
guidance_scale = gr.Slider(
|
117 |
label="Guidance Scale",
|
118 |
minimum=1,
|
|
|
120 |
step=0.1,
|
121 |
value=3.5,
|
122 |
)
|
|
|
123 |
num_inference_steps = gr.Slider(
|
124 |
label="Number of inference steps",
|
125 |
minimum=1,
|
|
|
129 |
)
|
130 |
|
131 |
gr.Examples(
|
132 |
+
examples=examples,
|
133 |
+
fn=infer,
|
134 |
+
inputs=[prompt, lora_model],
|
135 |
+
outputs=[result, seed, output_message],
|
136 |
cache_examples="lazy"
|
137 |
)
|
138 |
|
139 |
gr.on(
|
140 |
triggers=[run_button.click, prompt.submit],
|
141 |
+
fn=infer,
|
142 |
+
inputs=[prompt, lora_model, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
143 |
+
outputs=[result, seed, output_message]
|
144 |
)
|
145 |
|
146 |
demo.launch()
|
requirements.txt
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
accelerate
|
2 |
-
git+https://github.com/huggingface/diffusers
|
3 |
torch
|
4 |
transformers==4.42.4
|
5 |
xformers
|
6 |
-
sentencepiece
|
|
|
|
1 |
accelerate
|
2 |
+
git+https://github.com/huggingface/diffusers@lora-support-flux
|
3 |
torch
|
4 |
transformers==4.42.4
|
5 |
xformers
|
6 |
+
sentencepiece
|
7 |
+
peft
|