Spaces:
Running
on
Zero
Running
on
Zero
Himanshu-AT
commited on
Commit
·
ef5b708
1
Parent(s):
92cb4ff
add lora
Browse files- app.py +366 -175
- lora_models.json +3 -2
- readme.md +1 -1
app.py
CHANGED
@@ -1,149 +1,61 @@
|
|
1 |
-
|
2 |
import gradio as gr
|
3 |
import numpy as np
|
4 |
-
import
|
|
|
5 |
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
from PIL import Image
|
7 |
-
import cv2
|
8 |
-
import spaces
|
9 |
-
import os
|
10 |
|
11 |
-
# ------------------ Inpainting Pipeline Setup ------------------ #
|
12 |
-
from diffusers import FluxFillPipeline
|
13 |
|
14 |
MAX_SEED = np.iinfo(np.int32).max
|
15 |
MAX_IMAGE_SIZE = 2048
|
16 |
|
17 |
-
pipe = FluxFillPipeline.from_pretrained(
|
18 |
-
|
19 |
-
)
|
20 |
-
pipe.load_lora_weights("alvdansen/flux-koda")
|
21 |
-
pipe.enable_lora()
|
22 |
-
|
23 |
-
def calculate_optimal_dimensions(image: Image.Image):
|
24 |
-
# Extract the original dimensions
|
25 |
-
original_width, original_height = image.size
|
26 |
-
|
27 |
-
# Set constants
|
28 |
-
MIN_ASPECT_RATIO = 9 / 16
|
29 |
-
MAX_ASPECT_RATIO = 16 / 9
|
30 |
-
FIXED_DIMENSION = 1024
|
31 |
-
|
32 |
-
# Calculate the aspect ratio of the original image
|
33 |
-
original_aspect_ratio = original_width / original_height
|
34 |
-
|
35 |
-
# Determine which dimension to fix
|
36 |
-
if original_aspect_ratio > 1: # Wider than tall
|
37 |
-
width = FIXED_DIMENSION
|
38 |
-
height = round(FIXED_DIMENSION / original_aspect_ratio)
|
39 |
-
else: # Taller than wide
|
40 |
-
height = FIXED_DIMENSION
|
41 |
-
width = round(FIXED_DIMENSION * original_aspect_ratio)
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
height = (height // 8) * 8
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
# Ensure minimum dimensions are met
|
55 |
-
width = max(width, 576) if width == FIXED_DIMENSION else width
|
56 |
-
height = max(height, 576) if height == FIXED_DIMENSION else height
|
57 |
-
|
58 |
-
return width, height
|
59 |
|
60 |
-
#
|
61 |
-
|
|
|
62 |
|
63 |
-
|
64 |
-
sam_model = SamModel.from_pretrained("facebook/sam-vit-base")
|
65 |
-
sam_processor = SamProcessor.from_pretrained("facebook/sam-vit-base")
|
66 |
|
67 |
@spaces.GPU(durations=300)
|
68 |
-
def
|
69 |
-
|
70 |
-
Generate a segmentation mask using SAM (via Hugging Face Transformers).
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
The function converts the coordinate into the proper input format for SAM and returns a binary mask.
|
76 |
-
"""
|
77 |
-
if mask_prompt.strip() == "":
|
78 |
-
raise ValueError("No mask prompt provided.")
|
79 |
-
|
80 |
-
try:
|
81 |
-
# Parse the mask_prompt into a coordinate
|
82 |
-
coords = [int(x.strip()) for x in mask_prompt.split(",")]
|
83 |
-
if len(coords) != 2:
|
84 |
-
raise ValueError("Expected two comma-separated integers (x,y).")
|
85 |
-
except Exception as e:
|
86 |
-
raise ValueError("Invalid mask prompt. Please provide coordinates as 'x,y'. Error: " + str(e))
|
87 |
-
|
88 |
-
# The SAM processor expects a list of input points.
|
89 |
-
# Format the point as a list of lists; here we assume one point per image.
|
90 |
-
# (The Transformers SAM expects the points in [x, y] order.)
|
91 |
-
input_points = [coords] # e.g. [[450,600]]
|
92 |
-
# Optionally, you can supply input_labels (1 for foreground, 0 for background)
|
93 |
-
input_labels = [1]
|
94 |
-
|
95 |
-
# Prepare the inputs for the SAM processor.
|
96 |
-
inputs = sam_processor(images=image,
|
97 |
-
input_points=[input_points],
|
98 |
-
input_labels=[input_labels],
|
99 |
-
return_tensors="pt")
|
100 |
-
|
101 |
-
# Move tensors to the same device as the model.
|
102 |
-
device = next(sam_model.parameters()).device
|
103 |
-
inputs = {k: v.to(device) for k, v in inputs.items()}
|
104 |
-
|
105 |
-
# Forward pass through SAM.
|
106 |
-
with torch.no_grad():
|
107 |
-
outputs = sam_model(**inputs)
|
108 |
-
|
109 |
-
# The output contains predicted masks; we take the first mask from the first prompt.
|
110 |
-
# (Assuming outputs.pred_masks is of shape (batch_size, num_masks, H, W))
|
111 |
-
pred_masks = outputs.pred_masks # Tensor of shape (1, num_masks, H, W)
|
112 |
-
mask = pred_masks[0][0].detach().cpu().numpy()
|
113 |
-
|
114 |
-
# Convert the mask to binary (0 or 255) using a threshold.
|
115 |
-
mask_bin = (mask > 0.5).astype(np.uint8) * 255
|
116 |
-
mask_pil = Image.fromarray(mask_bin)
|
117 |
-
return mask_pil
|
118 |
|
119 |
-
# ------------------ Inference Function ------------------ #
|
120 |
-
@spaces.GPU(durations=300)
|
121 |
-
def infer(edit_images, prompt, mask_prompt,
|
122 |
-
seed=42, randomize_seed=False, width=1024, height=1024,
|
123 |
-
guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
|
124 |
-
# Get the base image from the "background" layer.
|
125 |
image = edit_images["background"]
|
126 |
-
width, height = calculate_optimal_dimensions(image)
|
127 |
-
|
128 |
-
# If a mask prompt is provided, use the SAM-based mask generator.
|
129 |
-
if mask_prompt and mask_prompt.strip() != "":
|
130 |
-
try:
|
131 |
-
mask = generate_mask_with_sam(image, mask_prompt)
|
132 |
-
except Exception as e:
|
133 |
-
raise ValueError("Error generating mask from prompt: " + str(e))
|
134 |
-
else:
|
135 |
-
# Fall back to using a manually drawn mask (from the first layer).
|
136 |
-
try:
|
137 |
-
mask = edit_images["layers"][0]
|
138 |
-
except (TypeError, IndexError):
|
139 |
-
raise ValueError("No mask provided. Please either draw a mask or supply a mask prompt.")
|
140 |
-
|
141 |
if randomize_seed:
|
142 |
seed = random.randint(0, MAX_SEED)
|
143 |
|
144 |
-
#
|
145 |
-
|
|
|
146 |
prompt=prompt,
|
|
|
147 |
image=image,
|
148 |
mask_image=mask,
|
149 |
height=height,
|
@@ -151,14 +63,23 @@ def infer(edit_images, prompt, mask_prompt,
|
|
151 |
guidance_scale=guidance_scale,
|
152 |
num_inference_steps=num_inference_steps,
|
153 |
generator=torch.Generator(device='cuda').manual_seed(seed),
|
|
|
154 |
).images[0]
|
155 |
|
156 |
-
output_image_jpg =
|
157 |
output_image_jpg.save("output.jpg", "JPEG")
|
|
|
158 |
return output_image_jpg, seed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
|
160 |
-
|
161 |
-
css = """
|
162 |
#col-container {
|
163 |
margin: 0 auto;
|
164 |
max-width: 1000px;
|
@@ -166,51 +87,41 @@ css = """
|
|
166 |
"""
|
167 |
|
168 |
with gr.Blocks(css=css) as demo:
|
|
|
169 |
with gr.Column(elem_id="col-container"):
|
170 |
-
gr.Markdown("# FLUX.1 [dev]
|
|
|
171 |
with gr.Row():
|
172 |
with gr.Column():
|
173 |
-
# The image editor now allows you to optionally draw a mask.
|
174 |
edit_image = gr.ImageEditor(
|
175 |
-
label='Upload
|
176 |
type='pil',
|
177 |
sources=["upload", "webcam"],
|
178 |
image_mode='RGB',
|
179 |
-
layers=False,
|
180 |
brush=gr.Brush(colors=["#FFFFFF"]),
|
|
|
181 |
)
|
182 |
prompt = gr.Text(
|
183 |
-
label="
|
184 |
show_label=False,
|
185 |
max_lines=2,
|
186 |
-
placeholder="Enter your
|
187 |
container=False,
|
188 |
)
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
)
|
195 |
-
|
196 |
-
mask_preview = gr.Image(label="Mask Preview", show_label=True)
|
197 |
run_button = gr.Button("Run")
|
|
|
198 |
result = gr.Image(label="Result", show_label=False)
|
199 |
-
|
200 |
-
# Button to preview the generated mask.
|
201 |
-
def on_generate_mask(image, mask_prompt):
|
202 |
-
if image is None or mask_prompt.strip() == "":
|
203 |
-
return None
|
204 |
-
mask = generate_mask_with_sam(image, mask_prompt)
|
205 |
-
return mask
|
206 |
-
|
207 |
-
generate_mask_btn.click(
|
208 |
-
fn=on_generate_mask,
|
209 |
-
inputs=[edit_image, mask_prompt],
|
210 |
-
outputs=[mask_preview]
|
211 |
-
)
|
212 |
|
213 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
214 |
seed = gr.Slider(
|
215 |
label="Seed",
|
216 |
minimum=0,
|
@@ -218,45 +129,50 @@ with gr.Blocks(css=css) as demo:
|
|
218 |
step=1,
|
219 |
value=0,
|
220 |
)
|
|
|
221 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
|
|
222 |
with gr.Row():
|
223 |
-
|
224 |
-
label="Width",
|
225 |
-
minimum=256,
|
226 |
-
maximum=MAX_IMAGE_SIZE,
|
227 |
-
step=32,
|
228 |
-
value=1024,
|
229 |
-
visible=False
|
230 |
-
)
|
231 |
-
height = gr.Slider(
|
232 |
-
label="Height",
|
233 |
-
minimum=256,
|
234 |
-
maximum=MAX_IMAGE_SIZE,
|
235 |
-
step=32,
|
236 |
-
value=1024,
|
237 |
-
visible=False
|
238 |
-
)
|
239 |
-
with gr.Row():
|
240 |
guidance_scale = gr.Slider(
|
241 |
label="Guidance Scale",
|
242 |
minimum=1,
|
243 |
maximum=30,
|
244 |
step=0.5,
|
245 |
-
value=
|
246 |
)
|
|
|
247 |
num_inference_steps = gr.Slider(
|
248 |
-
label="Number of
|
249 |
minimum=1,
|
250 |
maximum=50,
|
251 |
step=1,
|
252 |
value=28,
|
253 |
)
|
254 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
255 |
gr.on(
|
256 |
triggers=[run_button.click, prompt.submit],
|
257 |
-
fn=infer,
|
258 |
-
inputs=[edit_image, prompt,
|
259 |
-
outputs=[result, seed]
|
260 |
)
|
261 |
|
262 |
# demo.launch()
|
@@ -272,3 +188,278 @@ def authenticate(username, password):
|
|
272 |
# Launch the app with authentication
|
273 |
|
274 |
demo.launch(auth=authenticate)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
+
import os
|
4 |
+
import spaces
|
5 |
import random
|
6 |
+
import json
|
7 |
+
# from image_gen_aux import DepthPreprocessor
|
8 |
+
from PIL import Image
|
9 |
+
import torch
|
10 |
+
from torchvision import transforms
|
11 |
+
|
12 |
+
from diffusers import FluxFillPipeline, AutoencoderKL
|
13 |
from PIL import Image
|
|
|
|
|
|
|
14 |
|
|
|
|
|
15 |
|
16 |
MAX_SEED = np.iinfo(np.int32).max
|
17 |
MAX_IMAGE_SIZE = 2048
|
18 |
|
19 |
+
pipe = FluxFillPipeline.from_pretrained("black-forest-labs/FLUX.1-Fill-dev", torch_dtype=torch.bfloat16).to("cuda")
|
20 |
+
# pipe.load_lora_weights("Himanshu806/testLora")
|
21 |
+
# pipe.enable_lora()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
with open("lora_models.json", "r") as f:
|
24 |
+
lora_models = json.load(f)
|
|
|
25 |
|
26 |
+
def download_model(model_name, model_path):
|
27 |
+
print(f"Downloading model: {model_name} from {model_path}")
|
28 |
+
try:
|
29 |
+
pipe.load_lora_weights(model_path)
|
30 |
+
print(f"Successfully downloaded model: {model_name}")
|
31 |
+
except Exception as e:
|
32 |
+
print(f"Failed to download model: {model_name}. Error: {e}")
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
# Iterate through the models and download each one
|
35 |
+
for model_name, model_path in lora_models.items():
|
36 |
+
download_model(model_name, model_path)
|
37 |
|
38 |
+
lora_models["None"] = None
|
|
|
|
|
39 |
|
40 |
@spaces.GPU(durations=300)
|
41 |
+
def infer(edit_images, prompt, width, height, lora_model, seed=42, randomize_seed=False, guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
|
42 |
+
# pipe.enable_xformers_memory_efficient_attention()
|
|
|
43 |
|
44 |
+
if lora_model != "None":
|
45 |
+
pipe.load_lora_weights(lora_models[lora_model])
|
46 |
+
pipe.enable_lora()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
image = edit_images["background"]
|
49 |
+
# width, height = calculate_optimal_dimensions(image)
|
50 |
+
mask = edit_images["layers"][0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
if randomize_seed:
|
52 |
seed = random.randint(0, MAX_SEED)
|
53 |
|
54 |
+
# controlImage = processor(image)
|
55 |
+
image = pipe(
|
56 |
+
# mask_image_latent=vae.encode(controlImage),
|
57 |
prompt=prompt,
|
58 |
+
prompt_2=prompt,
|
59 |
image=image,
|
60 |
mask_image=mask,
|
61 |
height=height,
|
|
|
63 |
guidance_scale=guidance_scale,
|
64 |
num_inference_steps=num_inference_steps,
|
65 |
generator=torch.Generator(device='cuda').manual_seed(seed),
|
66 |
+
# lora_scale=0.75 // not supported in this version
|
67 |
).images[0]
|
68 |
|
69 |
+
output_image_jpg = image.convert("RGB")
|
70 |
output_image_jpg.save("output.jpg", "JPEG")
|
71 |
+
|
72 |
return output_image_jpg, seed
|
73 |
+
# return image, seed
|
74 |
+
|
75 |
+
examples = [
|
76 |
+
"photography of a young woman, accent lighting, (front view:1.4), "
|
77 |
+
# "a tiny astronaut hatching from an egg on the moon",
|
78 |
+
# "a cat holding a sign that says hello world",
|
79 |
+
# "an anime illustration of a wiener schnitzel",
|
80 |
+
]
|
81 |
|
82 |
+
css="""
|
|
|
83 |
#col-container {
|
84 |
margin: 0 auto;
|
85 |
max-width: 1000px;
|
|
|
87 |
"""
|
88 |
|
89 |
with gr.Blocks(css=css) as demo:
|
90 |
+
|
91 |
with gr.Column(elem_id="col-container"):
|
92 |
+
gr.Markdown(f"""# FLUX.1 [dev]
|
93 |
+
""")
|
94 |
with gr.Row():
|
95 |
with gr.Column():
|
|
|
96 |
edit_image = gr.ImageEditor(
|
97 |
+
label='Upload and draw mask for inpainting',
|
98 |
type='pil',
|
99 |
sources=["upload", "webcam"],
|
100 |
image_mode='RGB',
|
101 |
+
layers=False,
|
102 |
brush=gr.Brush(colors=["#FFFFFF"]),
|
103 |
+
# height=600
|
104 |
)
|
105 |
prompt = gr.Text(
|
106 |
+
label="Prompt",
|
107 |
show_label=False,
|
108 |
max_lines=2,
|
109 |
+
placeholder="Enter your prompt",
|
110 |
container=False,
|
111 |
)
|
112 |
+
|
113 |
+
lora_model = gr.Dropdown(
|
114 |
+
label="Select LoRA Model",
|
115 |
+
choices=list(lora_models.keys()),
|
116 |
+
value="None",
|
117 |
)
|
118 |
+
|
|
|
119 |
run_button = gr.Button("Run")
|
120 |
+
|
121 |
result = gr.Image(label="Result", show_label=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
with gr.Accordion("Advanced Settings", open=False):
|
124 |
+
|
125 |
seed = gr.Slider(
|
126 |
label="Seed",
|
127 |
minimum=0,
|
|
|
129 |
step=1,
|
130 |
value=0,
|
131 |
)
|
132 |
+
|
133 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
134 |
+
|
135 |
with gr.Row():
|
136 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
guidance_scale = gr.Slider(
|
138 |
label="Guidance Scale",
|
139 |
minimum=1,
|
140 |
maximum=30,
|
141 |
step=0.5,
|
142 |
+
value=50,
|
143 |
)
|
144 |
+
|
145 |
num_inference_steps = gr.Slider(
|
146 |
+
label="Number of inference steps",
|
147 |
minimum=1,
|
148 |
maximum=50,
|
149 |
step=1,
|
150 |
value=28,
|
151 |
)
|
152 |
|
153 |
+
with gr.Row():
|
154 |
+
|
155 |
+
width = gr.Slider(
|
156 |
+
label="width",
|
157 |
+
minimum=512,
|
158 |
+
maximum=3072,
|
159 |
+
step=1,
|
160 |
+
value=1024,
|
161 |
+
)
|
162 |
+
|
163 |
+
height = gr.Slider(
|
164 |
+
label="height",
|
165 |
+
minimum=512,
|
166 |
+
maximum=3072,
|
167 |
+
step=1,
|
168 |
+
value=1024,
|
169 |
+
)
|
170 |
+
|
171 |
gr.on(
|
172 |
triggers=[run_button.click, prompt.submit],
|
173 |
+
fn = infer,
|
174 |
+
inputs = [edit_image, prompt, width, height, lora_model, seed, randomize_seed, guidance_scale, num_inference_steps],
|
175 |
+
outputs = [result, seed]
|
176 |
)
|
177 |
|
178 |
# demo.launch()
|
|
|
188 |
# Launch the app with authentication
|
189 |
|
190 |
demo.launch(auth=authenticate)
|
191 |
+
|
192 |
+
|
193 |
+
# import gradio as gr
|
194 |
+
# import numpy as np
|
195 |
+
# import torch
|
196 |
+
# import random
|
197 |
+
# from PIL import Image
|
198 |
+
# import cv2
|
199 |
+
# import spaces
|
200 |
+
# import os
|
201 |
+
|
202 |
+
# # ------------------ Inpainting Pipeline Setup ------------------ #
|
203 |
+
# from diffusers import FluxFillPipeline
|
204 |
+
|
205 |
+
# MAX_SEED = np.iinfo(np.int32).max
|
206 |
+
# MAX_IMAGE_SIZE = 2048
|
207 |
+
|
208 |
+
# pipe = FluxFillPipeline.from_pretrained(
|
209 |
+
# "black-forest-labs/FLUX.1-Fill-dev", torch_dtype=torch.bfloat16
|
210 |
+
# )
|
211 |
+
# pipe.load_lora_weights("alvdansen/flux-koda")
|
212 |
+
# pipe.enable_lora()
|
213 |
+
|
214 |
+
# def calculate_optimal_dimensions(image: Image.Image):
|
215 |
+
# # Extract the original dimensions
|
216 |
+
# original_width, original_height = image.size
|
217 |
+
|
218 |
+
# # Set constants
|
219 |
+
# MIN_ASPECT_RATIO = 9 / 16
|
220 |
+
# MAX_ASPECT_RATIO = 16 / 9
|
221 |
+
# FIXED_DIMENSION = 1024
|
222 |
+
|
223 |
+
# # Calculate the aspect ratio of the original image
|
224 |
+
# original_aspect_ratio = original_width / original_height
|
225 |
+
|
226 |
+
# # Determine which dimension to fix
|
227 |
+
# if original_aspect_ratio > 1: # Wider than tall
|
228 |
+
# width = FIXED_DIMENSION
|
229 |
+
# height = round(FIXED_DIMENSION / original_aspect_ratio)
|
230 |
+
# else: # Taller than wide
|
231 |
+
# height = FIXED_DIMENSION
|
232 |
+
# width = round(FIXED_DIMENSION * original_aspect_ratio)
|
233 |
+
|
234 |
+
# # Ensure dimensions are multiples of 8
|
235 |
+
# width = (width // 8) * 8
|
236 |
+
# height = (height // 8) * 8
|
237 |
+
|
238 |
+
# # Enforce aspect ratio limits
|
239 |
+
# calculated_aspect_ratio = width / height
|
240 |
+
# if calculated_aspect_ratio > MAX_ASPECT_RATIO:
|
241 |
+
# width = (height * MAX_ASPECT_RATIO // 8) * 8
|
242 |
+
# elif calculated_aspect_ratio < MIN_ASPECT_RATIO:
|
243 |
+
# height = (width / MIN_ASPECT_RATIO // 8) * 8
|
244 |
+
|
245 |
+
# # Ensure minimum dimensions are met
|
246 |
+
# width = max(width, 576) if width == FIXED_DIMENSION else width
|
247 |
+
# height = max(height, 576) if height == FIXED_DIMENSION else height
|
248 |
+
|
249 |
+
# return width, height
|
250 |
+
|
251 |
+
# # ------------------ SAM (Transformers) Imports and Initialization ------------------ #
|
252 |
+
# from transformers import SamModel, SamProcessor
|
253 |
+
|
254 |
+
# # Load the model and processor from Hugging Face.
|
255 |
+
# sam_model = SamModel.from_pretrained("facebook/sam-vit-base")
|
256 |
+
# sam_processor = SamProcessor.from_pretrained("facebook/sam-vit-base")
|
257 |
+
|
258 |
+
# @spaces.GPU(durations=300)
|
259 |
+
# def generate_mask_with_sam(image: Image.Image, mask_prompt: str):
|
260 |
+
# """
|
261 |
+
# Generate a segmentation mask using SAM (via Hugging Face Transformers).
|
262 |
+
|
263 |
+
# The mask_prompt is expected to be a comma-separated string of two integers,
|
264 |
+
# e.g. "450,600" representing an (x,y) coordinate in the image.
|
265 |
+
|
266 |
+
# The function converts the coordinate into the proper input format for SAM and returns a binary mask.
|
267 |
+
# """
|
268 |
+
# if mask_prompt.strip() == "":
|
269 |
+
# raise ValueError("No mask prompt provided.")
|
270 |
+
|
271 |
+
# try:
|
272 |
+
# # Parse the mask_prompt into a coordinate
|
273 |
+
# coords = [int(x.strip()) for x in mask_prompt.split(",")]
|
274 |
+
# if len(coords) != 2:
|
275 |
+
# raise ValueError("Expected two comma-separated integers (x,y).")
|
276 |
+
# except Exception as e:
|
277 |
+
# raise ValueError("Invalid mask prompt. Please provide coordinates as 'x,y'. Error: " + str(e))
|
278 |
+
|
279 |
+
# # The SAM processor expects a list of input points.
|
280 |
+
# # Format the point as a list of lists; here we assume one point per image.
|
281 |
+
# # (The Transformers SAM expects the points in [x, y] order.)
|
282 |
+
# input_points = [coords] # e.g. [[450,600]]
|
283 |
+
# # Optionally, you can supply input_labels (1 for foreground, 0 for background)
|
284 |
+
# input_labels = [1]
|
285 |
+
|
286 |
+
# # Prepare the inputs for the SAM processor.
|
287 |
+
# inputs = sam_processor(images=image,
|
288 |
+
# input_points=[input_points],
|
289 |
+
# input_labels=[input_labels],
|
290 |
+
# return_tensors="pt")
|
291 |
+
|
292 |
+
# # Move tensors to the same device as the model.
|
293 |
+
# device = next(sam_model.parameters()).device
|
294 |
+
# inputs = {k: v.to(device) for k, v in inputs.items()}
|
295 |
+
|
296 |
+
# # Forward pass through SAM.
|
297 |
+
# with torch.no_grad():
|
298 |
+
# outputs = sam_model(**inputs)
|
299 |
+
|
300 |
+
# # The output contains predicted masks; we take the first mask from the first prompt.
|
301 |
+
# # (Assuming outputs.pred_masks is of shape (batch_size, num_masks, H, W))
|
302 |
+
# pred_masks = outputs.pred_masks # Tensor of shape (1, num_masks, H, W)
|
303 |
+
# mask = pred_masks[0][0].detach().cpu().numpy()
|
304 |
+
|
305 |
+
# # Convert the mask to binary (0 or 255) using a threshold.
|
306 |
+
# mask_bin = (mask > 0.5).astype(np.uint8) * 255
|
307 |
+
# mask_pil = Image.fromarray(mask_bin)
|
308 |
+
# return mask_pil
|
309 |
+
|
310 |
+
# # ------------------ Inference Function ------------------ #
|
311 |
+
# @spaces.GPU(durations=300)
|
312 |
+
# def infer(edit_images, prompt, mask_prompt,
|
313 |
+
# seed=42, randomize_seed=False, width=1024, height=1024,
|
314 |
+
# guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
|
315 |
+
# # Get the base image from the "background" layer.
|
316 |
+
# image = edit_images["background"]
|
317 |
+
# width, height = calculate_optimal_dimensions(image)
|
318 |
+
|
319 |
+
# # If a mask prompt is provided, use the SAM-based mask generator.
|
320 |
+
# if mask_prompt and mask_prompt.strip() != "":
|
321 |
+
# try:
|
322 |
+
# mask = generate_mask_with_sam(image, mask_prompt)
|
323 |
+
# except Exception as e:
|
324 |
+
# raise ValueError("Error generating mask from prompt: " + str(e))
|
325 |
+
# else:
|
326 |
+
# # Fall back to using a manually drawn mask (from the first layer).
|
327 |
+
# try:
|
328 |
+
# mask = edit_images["layers"][0]
|
329 |
+
# except (TypeError, IndexError):
|
330 |
+
# raise ValueError("No mask provided. Please either draw a mask or supply a mask prompt.")
|
331 |
+
|
332 |
+
# if randomize_seed:
|
333 |
+
# seed = random.randint(0, MAX_SEED)
|
334 |
+
|
335 |
+
# # Run the inpainting diffusion pipeline with the provided prompt and mask.
|
336 |
+
# image_out = pipe(
|
337 |
+
# prompt=prompt,
|
338 |
+
# image=image,
|
339 |
+
# mask_image=mask,
|
340 |
+
# height=height,
|
341 |
+
# width=width,
|
342 |
+
# guidance_scale=guidance_scale,
|
343 |
+
# num_inference_steps=num_inference_steps,
|
344 |
+
# generator=torch.Generator(device='cuda').manual_seed(seed),
|
345 |
+
# ).images[0]
|
346 |
+
|
347 |
+
# output_image_jpg = image_out.convert("RGB")
|
348 |
+
# output_image_jpg.save("output.jpg", "JPEG")
|
349 |
+
# return output_image_jpg, seed
|
350 |
+
|
351 |
+
# # ------------------ Gradio UI ------------------ #
|
352 |
+
# css = """
|
353 |
+
# #col-container {
|
354 |
+
# margin: 0 auto;
|
355 |
+
# max-width: 1000px;
|
356 |
+
# }
|
357 |
+
# """
|
358 |
+
|
359 |
+
# with gr.Blocks(css=css) as demo:
|
360 |
+
# with gr.Column(elem_id="col-container"):
|
361 |
+
# gr.Markdown("# FLUX.1 [dev] with SAM (Transformers) Mask Generation")
|
362 |
+
# with gr.Row():
|
363 |
+
# with gr.Column():
|
364 |
+
# # The image editor now allows you to optionally draw a mask.
|
365 |
+
# edit_image = gr.ImageEditor(
|
366 |
+
# label='Upload Image (and optionally draw a mask)',
|
367 |
+
# type='pil',
|
368 |
+
# sources=["upload", "webcam"],
|
369 |
+
# image_mode='RGB',
|
370 |
+
# layers=False, # We will generate a mask automatically if needed.
|
371 |
+
# brush=gr.Brush(colors=["#FFFFFF"]),
|
372 |
+
# )
|
373 |
+
# prompt = gr.Text(
|
374 |
+
# label="Inpainting Prompt",
|
375 |
+
# show_label=False,
|
376 |
+
# max_lines=2,
|
377 |
+
# placeholder="Enter your inpainting prompt",
|
378 |
+
# container=False,
|
379 |
+
# )
|
380 |
+
# mask_prompt = gr.Text(
|
381 |
+
# label="Mask Prompt (enter a coordinate as 'x,y')",
|
382 |
+
# show_label=True,
|
383 |
+
# placeholder="E.g. 450,600",
|
384 |
+
# container=True,
|
385 |
+
# )
|
386 |
+
# generate_mask_btn = gr.Button("Generate Mask")
|
387 |
+
# mask_preview = gr.Image(label="Mask Preview", show_label=True)
|
388 |
+
# run_button = gr.Button("Run")
|
389 |
+
# result = gr.Image(label="Result", show_label=False)
|
390 |
+
|
391 |
+
# # Button to preview the generated mask.
|
392 |
+
# def on_generate_mask(image, mask_prompt):
|
393 |
+
# if image is None or mask_prompt.strip() == "":
|
394 |
+
# return None
|
395 |
+
# mask = generate_mask_with_sam(image, mask_prompt)
|
396 |
+
# return mask
|
397 |
+
|
398 |
+
# generate_mask_btn.click(
|
399 |
+
# fn=on_generate_mask,
|
400 |
+
# inputs=[edit_image, mask_prompt],
|
401 |
+
# outputs=[mask_preview]
|
402 |
+
# )
|
403 |
+
|
404 |
+
# with gr.Accordion("Advanced Settings", open=False):
|
405 |
+
# seed = gr.Slider(
|
406 |
+
# label="Seed",
|
407 |
+
# minimum=0,
|
408 |
+
# maximum=MAX_SEED,
|
409 |
+
# step=1,
|
410 |
+
# value=0,
|
411 |
+
# )
|
412 |
+
# randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
413 |
+
# with gr.Row():
|
414 |
+
# width = gr.Slider(
|
415 |
+
# label="Width",
|
416 |
+
# minimum=256,
|
417 |
+
# maximum=MAX_IMAGE_SIZE,
|
418 |
+
# step=32,
|
419 |
+
# value=1024,
|
420 |
+
# visible=False
|
421 |
+
# )
|
422 |
+
# height = gr.Slider(
|
423 |
+
# label="Height",
|
424 |
+
# minimum=256,
|
425 |
+
# maximum=MAX_IMAGE_SIZE,
|
426 |
+
# step=32,
|
427 |
+
# value=1024,
|
428 |
+
# visible=False
|
429 |
+
# )
|
430 |
+
# with gr.Row():
|
431 |
+
# guidance_scale = gr.Slider(
|
432 |
+
# label="Guidance Scale",
|
433 |
+
# minimum=1,
|
434 |
+
# maximum=30,
|
435 |
+
# step=0.5,
|
436 |
+
# value=3.5,
|
437 |
+
# )
|
438 |
+
# num_inference_steps = gr.Slider(
|
439 |
+
# label="Number of Inference Steps",
|
440 |
+
# minimum=1,
|
441 |
+
# maximum=50,
|
442 |
+
# step=1,
|
443 |
+
# value=28,
|
444 |
+
# )
|
445 |
+
|
446 |
+
# gr.on(
|
447 |
+
# triggers=[run_button.click, prompt.submit],
|
448 |
+
# fn=infer,
|
449 |
+
# inputs=[edit_image, prompt, mask_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
450 |
+
# outputs=[result, seed]
|
451 |
+
# )
|
452 |
+
|
453 |
+
# # demo.launch()
|
454 |
+
# PASSWORD = os.getenv("GRADIO_PASSWORD")
|
455 |
+
# USERNAME = os.getenv("GRADIO_USERNAME")
|
456 |
+
# # Create an authentication object
|
457 |
+
# def authenticate(username, password):
|
458 |
+
# if username == USERNAME and password == PASSWORD:
|
459 |
+
# return True
|
460 |
+
|
461 |
+
# else:
|
462 |
+
# return False
|
463 |
+
# # Launch the app with authentication
|
464 |
+
|
465 |
+
# demo.launch(auth=authenticate)
|
lora_models.json
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
{
|
2 |
-
"RahulFineTuned": "Himanshu806/testLora",
|
3 |
-
"KodaRealistic": "alvdansen/flux-koda"
|
|
|
4 |
}
|
|
|
1 |
{
|
2 |
+
"RahulFineTuned (qwertyui)": "Himanshu806/testLora",
|
3 |
+
"KodaRealistic (fmlft style)": "alvdansen/flux-koda",
|
4 |
+
"femaleIndian (indmodelf)": "Himanshu806/ind-f-model"
|
5 |
}
|
readme.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
---
|
2 |
-
title: Inpainting
|
3 |
emoji: 🏆
|
4 |
colorFrom: blue
|
5 |
colorTo: purple
|
|
|
1 |
---
|
2 |
+
title: FLUX.1 Dev Inpainting Model Beta GPU
|
3 |
emoji: 🏆
|
4 |
colorFrom: blue
|
5 |
colorTo: purple
|