Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import spaces
|
2 |
-
|
3 |
import gradio as gr
|
4 |
import numpy as np
|
5 |
import os
|
@@ -12,16 +11,14 @@ import zipfile
|
|
12 |
|
13 |
from diffusers import FluxFillPipeline, AutoencoderKL
|
14 |
from PIL import Image
|
15 |
-
# from samgeo.text_sam import LangSAM
|
16 |
|
17 |
MAX_SEED = np.iinfo(np.int32).max
|
18 |
MAX_IMAGE_SIZE = 2048
|
19 |
|
20 |
-
#
|
21 |
-
# sam = LangSAM(model_type="sam2-hiera-large").to(device)
|
22 |
-
|
23 |
pipe = FluxFillPipeline.from_pretrained("black-forest-labs/FLUX.1-Fill-dev", torch_dtype=torch.bfloat16).to("cuda")
|
24 |
|
|
|
25 |
with open("lora_models.json", "r") as f:
|
26 |
lora_models = json.load(f)
|
27 |
|
@@ -39,7 +36,7 @@ for model_name, model_path in lora_models.items():
|
|
39 |
|
40 |
lora_models["None"] = None
|
41 |
|
42 |
-
def calculate_optimal_dimensions(image: Image.Image):
|
43 |
# Extract the original dimensions
|
44 |
original_width, original_height = image.size
|
45 |
|
@@ -59,6 +56,10 @@ def calculate_optimal_dimensions(image: Image.Image):
|
|
59 |
height = FIXED_DIMENSION
|
60 |
width = round(FIXED_DIMENSION * original_aspect_ratio)
|
61 |
|
|
|
|
|
|
|
|
|
62 |
# Ensure dimensions are multiples of 8
|
63 |
width = (width // 8) * 8
|
64 |
height = (height // 8) * 8
|
@@ -71,19 +72,27 @@ def calculate_optimal_dimensions(image: Image.Image):
|
|
71 |
height = (width / MIN_ASPECT_RATIO // 8) * 8
|
72 |
|
73 |
# Ensure width and height remain above the minimum dimensions
|
74 |
-
width = max(width, 576)
|
75 |
-
height = max(height, 576)
|
|
|
|
|
|
|
|
|
76 |
|
77 |
return width, height
|
78 |
|
79 |
@spaces.GPU(durations=300)
|
80 |
-
def infer(edit_images, prompt, lora_model, strength, seed=42, randomize_seed=False,
|
81 |
-
|
|
|
82 |
gr.Info("Infering")
|
83 |
|
|
|
84 |
if lora_model != "None":
|
85 |
pipe.load_lora_weights(lora_models[lora_model])
|
86 |
pipe.enable_lora()
|
|
|
|
|
87 |
|
88 |
gr.Info("starting checks")
|
89 |
|
@@ -94,34 +103,50 @@ def infer(edit_images, prompt, lora_model, strength, seed=42, randomize_seed=Fal
|
|
94 |
gr.Info("Please upload an image.")
|
95 |
return None, None
|
96 |
|
97 |
-
|
98 |
-
width, height = calculate_optimal_dimensions(image)
|
|
|
99 |
if randomize_seed:
|
100 |
seed = random.randint(0, MAX_SEED)
|
101 |
|
102 |
-
#
|
103 |
-
gr.Info("
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
output_image_jpg.save("output.jpg", "JPEG")
|
122 |
|
123 |
return output_image_jpg, seed
|
124 |
-
# return image, seed
|
125 |
|
126 |
def download_image(image):
|
127 |
if isinstance(image, np.ndarray):
|
@@ -129,7 +154,8 @@ def download_image(image):
|
|
129 |
image.save("output.png", "PNG")
|
130 |
return "output.png"
|
131 |
|
132 |
-
def save_details(result, edit_image, prompt, lora_model, strength, seed, guidance_scale,
|
|
|
133 |
image = edit_image["background"]
|
134 |
mask = edit_image["layers"][0]
|
135 |
|
@@ -146,11 +172,16 @@ def save_details(result, edit_image, prompt, lora_model, strength, seed, guidanc
|
|
146 |
|
147 |
details = {
|
148 |
"prompt": prompt,
|
|
|
149 |
"lora_model": lora_model,
|
|
|
150 |
"strength": strength,
|
151 |
"seed": seed,
|
152 |
"guidance_scale": guidance_scale,
|
153 |
-
"num_inference_steps": num_inference_steps
|
|
|
|
|
|
|
154 |
}
|
155 |
|
156 |
with open("details.json", "w") as f:
|
@@ -168,16 +199,8 @@ def save_details(result, edit_image, prompt, lora_model, strength, seed, guidanc
|
|
168 |
def set_image_as_inpaint(image):
|
169 |
return image
|
170 |
|
171 |
-
# def generate_mask(image, click_x, click_y):
|
172 |
-
# text_prompt = "face"
|
173 |
-
# mask = sam.predict(image, text_prompt, box_threshold=0.24, text_threshold=0.24)
|
174 |
-
# return mask
|
175 |
-
|
176 |
examples = [
|
177 |
-
"photography of a young woman,
|
178 |
-
# "a tiny astronaut hatching from an egg on the moon",
|
179 |
-
# "a cat holding a sign that says hello world",
|
180 |
-
# "an anime illustration of a wiener schnitzel",
|
181 |
]
|
182 |
|
183 |
css="""
|
@@ -190,8 +213,7 @@ css="""
|
|
190 |
with gr.Blocks(css=css) as demo:
|
191 |
|
192 |
with gr.Column(elem_id="col-container"):
|
193 |
-
gr.Markdown(f"""# FLUX.1 [dev]
|
194 |
-
""")
|
195 |
with gr.Row():
|
196 |
with gr.Column():
|
197 |
edit_image = gr.ImageEditor(
|
@@ -201,16 +223,15 @@ with gr.Blocks(css=css) as demo:
|
|
201 |
image_mode='RGB',
|
202 |
layers=False,
|
203 |
brush=gr.Brush(colors=["#FFFFFF"]),
|
204 |
-
# height=600
|
205 |
)
|
206 |
prompt = gr.Text(
|
207 |
label="Prompt",
|
208 |
-
show_label=
|
209 |
max_lines=2,
|
210 |
placeholder="Enter your prompt",
|
211 |
-
container=
|
212 |
)
|
213 |
-
|
214 |
lora_model = gr.Dropdown(
|
215 |
label="Select LoRA Model",
|
216 |
choices=list(lora_models.keys()),
|
@@ -222,25 +243,23 @@ with gr.Blocks(css=css) as demo:
|
|
222 |
result = gr.Image(label="Result", show_label=False)
|
223 |
|
224 |
with gr.Accordion("Advanced Settings", open=False):
|
225 |
-
|
226 |
-
seed = gr.Slider(
|
227 |
-
label="Seed",
|
228 |
-
minimum=0,
|
229 |
-
maximum=MAX_SEED,
|
230 |
-
step=1,
|
231 |
-
value=0,
|
232 |
-
)
|
233 |
-
|
234 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
235 |
-
|
236 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
237 |
|
|
|
238 |
guidance_scale = gr.Slider(
|
239 |
label="Guidance Scale",
|
240 |
minimum=1,
|
241 |
maximum=30,
|
242 |
step=0.5,
|
243 |
-
value=
|
244 |
)
|
245 |
|
246 |
num_inference_steps = gr.Slider(
|
@@ -252,7 +271,6 @@ with gr.Blocks(css=css) as demo:
|
|
252 |
)
|
253 |
|
254 |
with gr.Row():
|
255 |
-
|
256 |
strength = gr.Slider(
|
257 |
label="Strength",
|
258 |
minimum=0,
|
@@ -260,28 +278,32 @@ with gr.Blocks(css=css) as demo:
|
|
260 |
step=0.01,
|
261 |
value=0.85,
|
262 |
)
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
|
|
|
|
|
|
279 |
|
280 |
gr.on(
|
281 |
triggers=[run_button.click, prompt.submit],
|
282 |
-
fn
|
283 |
-
inputs
|
284 |
-
|
|
|
285 |
)
|
286 |
|
287 |
download_button = gr.Button("Download Image as PNG")
|
@@ -302,26 +324,21 @@ with gr.Blocks(css=css) as demo:
|
|
302 |
|
303 |
save_button.click(
|
304 |
fn=save_details,
|
305 |
-
inputs=[result, edit_image, prompt, lora_model, strength, seed, guidance_scale,
|
|
|
306 |
outputs=gr.File(label="Download/Save Status")
|
307 |
)
|
308 |
|
309 |
-
|
310 |
-
# fn=generate_mask,
|
311 |
-
# inputs=[edit_image, gr.Number(), gr.Number()],
|
312 |
-
# outputs=[edit_image]
|
313 |
-
# )
|
314 |
-
|
315 |
-
# demo.launch()
|
316 |
PASSWORD = os.getenv("GRADIO_PASSWORD")
|
317 |
USERNAME = os.getenv("GRADIO_USERNAME")
|
318 |
-
|
|
|
319 |
def authenticate(username, password):
|
320 |
if username == USERNAME and password == PASSWORD:
|
321 |
return True
|
322 |
-
|
323 |
else:
|
324 |
return False
|
325 |
-
# Launch the app with authentication
|
326 |
|
327 |
-
|
|
|
|
1 |
import spaces
|
|
|
2 |
import gradio as gr
|
3 |
import numpy as np
|
4 |
import os
|
|
|
11 |
|
12 |
from diffusers import FluxFillPipeline, AutoencoderKL
|
13 |
from PIL import Image
|
|
|
14 |
|
15 |
MAX_SEED = np.iinfo(np.int32).max
|
16 |
MAX_IMAGE_SIZE = 2048
|
17 |
|
18 |
+
# Initialize the pipeline
|
|
|
|
|
19 |
pipe = FluxFillPipeline.from_pretrained("black-forest-labs/FLUX.1-Fill-dev", torch_dtype=torch.bfloat16).to("cuda")
|
20 |
|
21 |
+
# Load LoRA models from JSON
|
22 |
with open("lora_models.json", "r") as f:
|
23 |
lora_models = json.load(f)
|
24 |
|
|
|
36 |
|
37 |
lora_models["None"] = None
|
38 |
|
39 |
+
def calculate_optimal_dimensions(image: Image.Image, scale_factor=1.0):
|
40 |
# Extract the original dimensions
|
41 |
original_width, original_height = image.size
|
42 |
|
|
|
56 |
height = FIXED_DIMENSION
|
57 |
width = round(FIXED_DIMENSION * original_aspect_ratio)
|
58 |
|
59 |
+
# Apply scaling factor
|
60 |
+
width = round(width * scale_factor)
|
61 |
+
height = round(height * scale_factor)
|
62 |
+
|
63 |
# Ensure dimensions are multiples of 8
|
64 |
width = (width // 8) * 8
|
65 |
height = (height // 8) * 8
|
|
|
72 |
height = (width / MIN_ASPECT_RATIO // 8) * 8
|
73 |
|
74 |
# Ensure width and height remain above the minimum dimensions
|
75 |
+
width = max(width, 576)
|
76 |
+
height = max(height, 576)
|
77 |
+
|
78 |
+
# Ensure dimensions don't exceed maximum
|
79 |
+
width = min(width, MAX_IMAGE_SIZE)
|
80 |
+
height = min(height, MAX_IMAGE_SIZE)
|
81 |
|
82 |
return width, height
|
83 |
|
84 |
@spaces.GPU(durations=300)
|
85 |
+
def infer(edit_images, prompt, lora_model, strength, seed=42, randomize_seed=False,
|
86 |
+
guidance_scale=3.5, num_inference_steps=28, lora_scale=0.75,
|
87 |
+
scale_factor=1.0, progress=gr.Progress(track_tqdm=True)):
|
88 |
gr.Info("Infering")
|
89 |
|
90 |
+
# Load and enable LoRA if selected
|
91 |
if lora_model != "None":
|
92 |
pipe.load_lora_weights(lora_models[lora_model])
|
93 |
pipe.enable_lora()
|
94 |
+
else:
|
95 |
+
pipe.disable_lora()
|
96 |
|
97 |
gr.Info("starting checks")
|
98 |
|
|
|
103 |
gr.Info("Please upload an image.")
|
104 |
return None, None
|
105 |
|
106 |
+
# Calculate dimensions with scale factor
|
107 |
+
width, height = calculate_optimal_dimensions(image, scale_factor)
|
108 |
+
|
109 |
if randomize_seed:
|
110 |
seed = random.randint(0, MAX_SEED)
|
111 |
|
112 |
+
# Generate image
|
113 |
+
gr.Info(f"Generating image at {width}x{height}")
|
114 |
+
generator = torch.Generator(device='cuda').manual_seed(seed)
|
115 |
+
|
116 |
+
# Configure pipeline parameters
|
117 |
+
pipeline_kwargs = {
|
118 |
+
"prompt": prompt,
|
119 |
+
"prompt_2": prompt,
|
120 |
+
"negative_prompt": negative_prompt,
|
121 |
+
"image": image,
|
122 |
+
"mask_image": mask,
|
123 |
+
"height": height,
|
124 |
+
"width": width,
|
125 |
+
"guidance_scale": guidance_scale,
|
126 |
+
"strength": strength,
|
127 |
+
"num_inference_steps": num_inference_steps,
|
128 |
+
"generator": generator,
|
129 |
+
}
|
130 |
+
|
131 |
+
# Add LoRA scale if model supports it
|
132 |
+
if lora_model != "None":
|
133 |
+
try:
|
134 |
+
pipeline_kwargs["cross_attention_kwargs"] = {"scale": lora_scale}
|
135 |
+
except:
|
136 |
+
gr.Info("LoRA scale not supported in this model version - using default scaling")
|
137 |
+
|
138 |
+
# Run the pipeline
|
139 |
+
try:
|
140 |
+
output = pipe(**pipeline_kwargs)
|
141 |
+
result_image = output.images[0]
|
142 |
+
except Exception as e:
|
143 |
+
gr.Error(f"Error during generation: {str(e)}")
|
144 |
+
return None, seed
|
145 |
+
|
146 |
+
output_image_jpg = result_image.convert("RGB")
|
147 |
output_image_jpg.save("output.jpg", "JPEG")
|
148 |
|
149 |
return output_image_jpg, seed
|
|
|
150 |
|
151 |
def download_image(image):
|
152 |
if isinstance(image, np.ndarray):
|
|
|
154 |
image.save("output.png", "PNG")
|
155 |
return "output.png"
|
156 |
|
157 |
+
def save_details(result, edit_image, prompt, lora_model, strength, seed, guidance_scale,
|
158 |
+
num_inference_steps, lora_scale, scale_factor):
|
159 |
image = edit_image["background"]
|
160 |
mask = edit_image["layers"][0]
|
161 |
|
|
|
172 |
|
173 |
details = {
|
174 |
"prompt": prompt,
|
175 |
+
"negative_prompt": negative_prompt,
|
176 |
"lora_model": lora_model,
|
177 |
+
"lora_scale": lora_scale,
|
178 |
"strength": strength,
|
179 |
"seed": seed,
|
180 |
"guidance_scale": guidance_scale,
|
181 |
+
"num_inference_steps": num_inference_steps,
|
182 |
+
"scale_factor": scale_factor,
|
183 |
+
"width": result.width,
|
184 |
+
"height": result.height
|
185 |
}
|
186 |
|
187 |
with open("details.json", "w") as f:
|
|
|
199 |
def set_image_as_inpaint(image):
|
200 |
return image
|
201 |
|
|
|
|
|
|
|
|
|
|
|
202 |
examples = [
|
203 |
+
"photography of a young woman, accent lighting, (front view:1.4)",
|
|
|
|
|
|
|
204 |
]
|
205 |
|
206 |
css="""
|
|
|
213 |
with gr.Blocks(css=css) as demo:
|
214 |
|
215 |
with gr.Column(elem_id="col-container"):
|
216 |
+
gr.Markdown(f"""# FLUX.1 [dev] Inpainting Tool""")
|
|
|
217 |
with gr.Row():
|
218 |
with gr.Column():
|
219 |
edit_image = gr.ImageEditor(
|
|
|
223 |
image_mode='RGB',
|
224 |
layers=False,
|
225 |
brush=gr.Brush(colors=["#FFFFFF"]),
|
|
|
226 |
)
|
227 |
prompt = gr.Text(
|
228 |
label="Prompt",
|
229 |
+
show_label=True,
|
230 |
max_lines=2,
|
231 |
placeholder="Enter your prompt",
|
232 |
+
container=True,
|
233 |
)
|
234 |
+
|
235 |
lora_model = gr.Dropdown(
|
236 |
label="Select LoRA Model",
|
237 |
choices=list(lora_models.keys()),
|
|
|
243 |
result = gr.Image(label="Result", show_label=False)
|
244 |
|
245 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
246 |
with gr.Row():
|
247 |
+
seed = gr.Slider(
|
248 |
+
label="Seed",
|
249 |
+
minimum=0,
|
250 |
+
maximum=MAX_SEED,
|
251 |
+
step=1,
|
252 |
+
value=0,
|
253 |
+
)
|
254 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
255 |
|
256 |
+
with gr.Row():
|
257 |
guidance_scale = gr.Slider(
|
258 |
label="Guidance Scale",
|
259 |
minimum=1,
|
260 |
maximum=30,
|
261 |
step=0.5,
|
262 |
+
value=3.5,
|
263 |
)
|
264 |
|
265 |
num_inference_steps = gr.Slider(
|
|
|
271 |
)
|
272 |
|
273 |
with gr.Row():
|
|
|
274 |
strength = gr.Slider(
|
275 |
label="Strength",
|
276 |
minimum=0,
|
|
|
278 |
step=0.01,
|
279 |
value=0.85,
|
280 |
)
|
281 |
+
|
282 |
+
lora_scale = gr.Slider(
|
283 |
+
label="LoRA Scale",
|
284 |
+
minimum=0,
|
285 |
+
maximum=2,
|
286 |
+
step=0.05,
|
287 |
+
value=0.75,
|
288 |
+
info="Controls the influence of the LoRA model"
|
289 |
+
)
|
290 |
+
|
291 |
+
with gr.Row():
|
292 |
+
scale_factor = gr.Slider(
|
293 |
+
label="Image Scale Factor",
|
294 |
+
minimum=0.5,
|
295 |
+
maximum=2.0,
|
296 |
+
step=0.1,
|
297 |
+
value=1.0,
|
298 |
+
info="Scale factor for image dimensions (1.0 = original, 2.0 = double size)"
|
299 |
+
)
|
300 |
|
301 |
gr.on(
|
302 |
triggers=[run_button.click, prompt.submit],
|
303 |
+
fn=infer,
|
304 |
+
inputs=[edit_image, prompt, lora_model, strength, seed, randomize_seed,
|
305 |
+
guidance_scale, num_inference_steps, lora_scale, scale_factor],
|
306 |
+
outputs=[result, seed]
|
307 |
)
|
308 |
|
309 |
download_button = gr.Button("Download Image as PNG")
|
|
|
324 |
|
325 |
save_button.click(
|
326 |
fn=save_details,
|
327 |
+
inputs=[result, edit_image, prompt, lora_model, strength, seed, guidance_scale,
|
328 |
+
num_inference_steps, lora_scale, scale_factor],
|
329 |
outputs=gr.File(label="Download/Save Status")
|
330 |
)
|
331 |
|
332 |
+
# Get authentication credentials
|
|
|
|
|
|
|
|
|
|
|
|
|
333 |
PASSWORD = os.getenv("GRADIO_PASSWORD")
|
334 |
USERNAME = os.getenv("GRADIO_USERNAME")
|
335 |
+
|
336 |
+
# Create an authentication function
|
337 |
def authenticate(username, password):
|
338 |
if username == USERNAME and password == PASSWORD:
|
339 |
return True
|
|
|
340 |
else:
|
341 |
return False
|
|
|
342 |
|
343 |
+
# Launch the app with authentication
|
344 |
+
demo.launch(debug=True, auth=authenticate)
|