Spaces:
Running
on
Zero
Running
on
Zero
init commit
Browse files
app.py
CHANGED
|
@@ -22,6 +22,7 @@ import json
|
|
| 22 |
from utils.florence import load_florence_model, run_florence_inference, \
|
| 23 |
FLORENCE_OPEN_VOCABULARY_DETECTION_TASK
|
| 24 |
from utils.sam import load_sam_image_model, run_sam_inference
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
|
|
@@ -41,6 +42,10 @@ taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).
|
|
| 41 |
good_vae = AutoencoderKL.from_pretrained(base_model, subfolder="vae", torch_dtype=dtype).to(device)
|
| 42 |
pipe = FluxInpaintPipeline.from_pretrained(base_model, torch_dtype=dtype, vae=taef1).to(device)
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
class calculateDuration:
|
| 45 |
def __init__(self, activity_name=""):
|
| 46 |
self.activity_name = activity_name
|
|
@@ -129,7 +134,6 @@ def upload_image_to_r2(image, account_id, access_key, secret_key, bucket_name):
|
|
| 129 |
return image_file
|
| 130 |
|
| 131 |
|
| 132 |
-
@spaces.GPU(duration=50)
|
| 133 |
def run_flux(
|
| 134 |
image: Image.Image,
|
| 135 |
mask: Image.Image,
|
|
@@ -154,28 +158,74 @@ def run_flux(
|
|
| 154 |
seed_slicer = random.randint(0, MAX_SEED)
|
| 155 |
generator = torch.Generator().manual_seed(seed_slicer)
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
| 172 |
def genearte_mask(image: Image.Image, masking_prompt_text: str) -> Image.Image:
|
| 173 |
# generate mask by florence & sam
|
| 174 |
print("Generating mask...")
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
|
|
|
|
| 179 |
def process(
|
| 180 |
image_url: str,
|
| 181 |
inpainting_prompt_text: str,
|
|
@@ -199,31 +249,32 @@ def process(
|
|
| 199 |
if not image_url:
|
| 200 |
gr.Info("please enter image url for inpaiting")
|
| 201 |
result["message"] = "invalid image url"
|
| 202 |
-
return
|
| 203 |
|
| 204 |
if not inpainting_prompt_text:
|
| 205 |
gr.Info("Please enter inpainting text prompt.")
|
| 206 |
result["message"] = "invalid inpainting prompt"
|
| 207 |
-
return
|
| 208 |
|
| 209 |
if not masking_prompt_text:
|
| 210 |
gr.Info("Please enter masking_prompt_text.")
|
| 211 |
result["message"] = "invalid masking prompt"
|
| 212 |
-
return
|
| 213 |
-
|
| 214 |
|
| 215 |
-
|
|
|
|
|
|
|
| 216 |
mask = genearte_mask(image, masking_prompt_text)
|
| 217 |
|
| 218 |
if not image:
|
| 219 |
gr.Info("Please upload an image.")
|
| 220 |
result["message"] = "can not load image"
|
| 221 |
-
return
|
| 222 |
|
| 223 |
if is_mask_empty(mask):
|
| 224 |
gr.Info("Please draw a mask or enter a masking prompt.")
|
| 225 |
result["message"] = "can not generate mask"
|
| 226 |
-
return
|
| 227 |
|
| 228 |
# generate
|
| 229 |
width, height = calculate_image_dimensions_for_flux(original_resolution_wh=image.size)
|
|
@@ -243,14 +294,14 @@ def process(
|
|
| 243 |
num_inference_steps_slider=num_inference_steps_slider,
|
| 244 |
resolution_wh=(width, height)
|
| 245 |
)
|
|
|
|
| 246 |
if upload_to_r2:
|
| 247 |
url = upload_image_to_r2(image, account_id, access_key, secret_key, bucket)
|
| 248 |
result = {"status": "success", "url": url}
|
| 249 |
else:
|
| 250 |
result = {"status": "success", "message": "Image generated but not uploaded"}
|
| 251 |
|
| 252 |
-
return
|
| 253 |
-
|
| 254 |
|
| 255 |
|
| 256 |
with gr.Blocks() as demo:
|
|
@@ -309,11 +360,8 @@ with gr.Blocks() as demo:
|
|
| 309 |
value=0.9,
|
| 310 |
)
|
| 311 |
|
| 312 |
-
|
| 313 |
-
|
| 314 |
with gr.Accordion("Advanced Settings", open=False):
|
| 315 |
|
| 316 |
-
|
| 317 |
with gr.Row():
|
| 318 |
mask_inflation_slider_component = gr.Slider(
|
| 319 |
label="Mask inflation",
|
|
@@ -370,23 +418,17 @@ with gr.Blocks() as demo:
|
|
| 370 |
)
|
| 371 |
|
| 372 |
upload_to_r2 = gr.Checkbox(label="Upload to R2", value=False)
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
|
|
|
|
|
|
|
|
|
| 377 |
|
| 378 |
|
| 379 |
with gr.Column():
|
| 380 |
|
| 381 |
-
output_image_component = gr.Image(
|
| 382 |
-
type='pil', image_mode='RGB', label='Generated image', format="png")
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
with gr.Accordion("Debug", open=False):
|
| 387 |
-
output_mask_component = gr.Image(
|
| 388 |
-
type='pil', image_mode='RGB', label='Input mask', format="png")
|
| 389 |
-
|
| 390 |
output_json_component = gr.Textbox()
|
| 391 |
|
| 392 |
submit_button_component.click(
|
|
@@ -411,8 +453,6 @@ with gr.Blocks() as demo:
|
|
| 411 |
bucket
|
| 412 |
],
|
| 413 |
outputs=[
|
| 414 |
-
output_image_component,
|
| 415 |
-
output_mask_component,
|
| 416 |
output_json_component
|
| 417 |
]
|
| 418 |
)
|
|
|
|
| 22 |
from utils.florence import load_florence_model, run_florence_inference, \
|
| 23 |
FLORENCE_OPEN_VOCABULARY_DETECTION_TASK
|
| 24 |
from utils.sam import load_sam_image_model, run_sam_inference
|
| 25 |
+
import supervision as sv
|
| 26 |
|
| 27 |
|
| 28 |
|
|
|
|
| 42 |
good_vae = AutoencoderKL.from_pretrained(base_model, subfolder="vae", torch_dtype=dtype).to(device)
|
| 43 |
pipe = FluxInpaintPipeline.from_pretrained(base_model, torch_dtype=dtype, vae=taef1).to(device)
|
| 44 |
|
| 45 |
+
FLORENCE_MODEL, FLORENCE_PROCESSOR = load_florence_model(device=device)
|
| 46 |
+
SAM_IMAGE_MODEL = load_sam_image_model(device=device)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
class calculateDuration:
|
| 50 |
def __init__(self, activity_name=""):
|
| 51 |
self.activity_name = activity_name
|
|
|
|
| 134 |
return image_file
|
| 135 |
|
| 136 |
|
|
|
|
| 137 |
def run_flux(
|
| 138 |
image: Image.Image,
|
| 139 |
mask: Image.Image,
|
|
|
|
| 158 |
seed_slicer = random.randint(0, MAX_SEED)
|
| 159 |
generator = torch.Generator().manual_seed(seed_slicer)
|
| 160 |
|
| 161 |
+
with calculateDuration("run pipe"):
|
| 162 |
+
genearte_image = PIPE(
|
| 163 |
+
prompt=prompt,
|
| 164 |
+
image=image,
|
| 165 |
+
mask_image=mask,
|
| 166 |
+
width=width,
|
| 167 |
+
height=height,
|
| 168 |
+
strength=strength_slider,
|
| 169 |
+
generator=generator,
|
| 170 |
+
num_inference_steps=num_inference_steps_slider,
|
| 171 |
+
max_sequence_length=256,
|
| 172 |
+
joint_attention_kwargs={"scale": lora_scale},
|
| 173 |
+
).images[0]
|
| 174 |
+
|
| 175 |
+
return genearte_image
|
| 176 |
+
|
| 177 |
+
|
| 178 |
def genearte_mask(image: Image.Image, masking_prompt_text: str) -> Image.Image:
|
| 179 |
# generate mask by florence & sam
|
| 180 |
print("Generating mask...")
|
| 181 |
+
task_prompt = "<CAPTION_TO_PHRASE_GROUNDING>"
|
| 182 |
+
|
| 183 |
+
with calculateDuration("FLORENCE"):
|
| 184 |
+
print(task_prompt, masking_prompt_text)
|
| 185 |
+
_, result = run_florence_inference(
|
| 186 |
+
model=FLORENCE_MODEL,
|
| 187 |
+
processor=FLORENCE_PROCESSOR,
|
| 188 |
+
device=device,
|
| 189 |
+
image=image,
|
| 190 |
+
task=task_prompt,
|
| 191 |
+
text=masking_prompt_text
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
with calculateDuration("sv.Detections"):
|
| 195 |
+
# start to dectect
|
| 196 |
+
detections = sv.Detections.from_lmm(
|
| 197 |
+
lmm=sv.LMM.FLORENCE_2,
|
| 198 |
+
result=result,
|
| 199 |
+
resolution_wh=image_input.size
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
images = []
|
| 203 |
+
|
| 204 |
+
with calculateDuration("generate segmenet mask"):
|
| 205 |
+
# using sam generate segments images
|
| 206 |
+
detections = run_sam_inference(SAM_IMAGE_MODEL, image, detections)
|
| 207 |
+
if len(detections) == 0:
|
| 208 |
+
gr.Info("No objects detected.")
|
| 209 |
+
return None
|
| 210 |
+
print("mask generated:", len(detections.mask))
|
| 211 |
+
kernel_size = dilate
|
| 212 |
+
kernel = np.ones((kernel_size, kernel_size), np.uint8)
|
| 213 |
+
|
| 214 |
+
for i in range(len(detections.mask)):
|
| 215 |
+
mask = detections.mask[i].astype(np.uint8) * 255
|
| 216 |
+
images.append(mask)
|
| 217 |
+
|
| 218 |
+
# merge mark into on image
|
| 219 |
+
merged_mask = np.zeros_like(images[0], dtype=np.uint8)
|
| 220 |
+
for mask in images:
|
| 221 |
+
merged_mask = cv2.bitwise_or(merged_mask, mask)
|
| 222 |
+
|
| 223 |
+
images = [merged_mask]
|
| 224 |
+
|
| 225 |
+
return images[0]
|
| 226 |
|
| 227 |
|
| 228 |
+
@spaces.GPU(duration=120)
|
| 229 |
def process(
|
| 230 |
image_url: str,
|
| 231 |
inpainting_prompt_text: str,
|
|
|
|
| 249 |
if not image_url:
|
| 250 |
gr.Info("please enter image url for inpaiting")
|
| 251 |
result["message"] = "invalid image url"
|
| 252 |
+
return json.dumps(result)
|
| 253 |
|
| 254 |
if not inpainting_prompt_text:
|
| 255 |
gr.Info("Please enter inpainting text prompt.")
|
| 256 |
result["message"] = "invalid inpainting prompt"
|
| 257 |
+
return json.dumps(result)
|
| 258 |
|
| 259 |
if not masking_prompt_text:
|
| 260 |
gr.Info("Please enter masking_prompt_text.")
|
| 261 |
result["message"] = "invalid masking prompt"
|
| 262 |
+
return json.dumps(result)
|
|
|
|
| 263 |
|
| 264 |
+
with calculateDuration("load image"):
|
| 265 |
+
image = load_image(image_url)
|
| 266 |
+
|
| 267 |
mask = genearte_mask(image, masking_prompt_text)
|
| 268 |
|
| 269 |
if not image:
|
| 270 |
gr.Info("Please upload an image.")
|
| 271 |
result["message"] = "can not load image"
|
| 272 |
+
return json.dumps(result)
|
| 273 |
|
| 274 |
if is_mask_empty(mask):
|
| 275 |
gr.Info("Please draw a mask or enter a masking prompt.")
|
| 276 |
result["message"] = "can not generate mask"
|
| 277 |
+
return json.dumps(result)
|
| 278 |
|
| 279 |
# generate
|
| 280 |
width, height = calculate_image_dimensions_for_flux(original_resolution_wh=image.size)
|
|
|
|
| 294 |
num_inference_steps_slider=num_inference_steps_slider,
|
| 295 |
resolution_wh=(width, height)
|
| 296 |
)
|
| 297 |
+
|
| 298 |
if upload_to_r2:
|
| 299 |
url = upload_image_to_r2(image, account_id, access_key, secret_key, bucket)
|
| 300 |
result = {"status": "success", "url": url}
|
| 301 |
else:
|
| 302 |
result = {"status": "success", "message": "Image generated but not uploaded"}
|
| 303 |
|
| 304 |
+
return json.dumps(result)
|
|
|
|
| 305 |
|
| 306 |
|
| 307 |
with gr.Blocks() as demo:
|
|
|
|
| 360 |
value=0.9,
|
| 361 |
)
|
| 362 |
|
|
|
|
|
|
|
| 363 |
with gr.Accordion("Advanced Settings", open=False):
|
| 364 |
|
|
|
|
| 365 |
with gr.Row():
|
| 366 |
mask_inflation_slider_component = gr.Slider(
|
| 367 |
label="Mask inflation",
|
|
|
|
| 418 |
)
|
| 419 |
|
| 420 |
upload_to_r2 = gr.Checkbox(label="Upload to R2", value=False)
|
| 421 |
+
with gr.Row():
|
| 422 |
+
account_id = gr.Textbox(label="Account Id", placeholder="Enter R2 account id")
|
| 423 |
+
bucket = gr.Textbox(label="Bucket Name", placeholder="Enter R2 bucket name here")
|
| 424 |
+
|
| 425 |
+
with gr.Row():
|
| 426 |
+
access_key = gr.Textbox(label="Access Key", placeholder="Enter R2 access key here")
|
| 427 |
+
secret_key = gr.Textbox(label="Secret Key", placeholder="Enter R2 secret key here")
|
| 428 |
|
| 429 |
|
| 430 |
with gr.Column():
|
| 431 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
output_json_component = gr.Textbox()
|
| 433 |
|
| 434 |
submit_button_component.click(
|
|
|
|
| 453 |
bucket
|
| 454 |
],
|
| 455 |
outputs=[
|
|
|
|
|
|
|
| 456 |
output_json_component
|
| 457 |
]
|
| 458 |
)
|