Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,12 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
-
#import random
|
4 |
-
import spaces #[uncomment to use ZeroGPU]
|
5 |
-
#from diffusers import DiffusionPipeline
|
6 |
import torch
|
7 |
|
8 |
-
|
9 |
-
|
10 |
#Flux
|
11 |
from diffusers import FluxFillPipeline
|
12 |
|
13 |
-
#import cv2
|
14 |
-
#import matplotlib.pyplot as plt
|
15 |
from PIL import Image
|
16 |
import os
|
17 |
import gc
|
@@ -20,74 +15,18 @@ import glob
|
|
20 |
|
21 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
22 |
|
23 |
-
# GDINO_MODEL_NAME="IDEA-Research/grounding-dino-tiny"
|
24 |
-
# SAM_MODEL_NAME="facebook/sam-vit-base"
|
25 |
-
|
26 |
-
# GDINO=pipeline(model=GDINO_MODEL_NAME, task="zero-shot-object-detection", device=DEVICE)
|
27 |
-
# SAM=AutoModelForMaskGeneration.from_pretrained(SAM_MODEL_NAME).to(DEVICE)
|
28 |
-
# SAM_PROCESSOR=AutoProcessor.from_pretrained(SAM_MODEL_NAME)
|
29 |
-
|
30 |
-
|
31 |
SD_PIPLINE = FluxFillPipeline.from_pretrained("black-forest-labs/FLUX.1-Fill-dev", torch_dtype=torch.bfloat16).to("cuda")
|
32 |
|
33 |
-
|
34 |
-
#except Exception as ex:
|
35 |
-
#print(ex)
|
36 |
-
|
37 |
-
|
38 |
-
# SD_MODEL="black-forest-labs/FLUX.1-dev" #"diffusers/stable-diffusion-xl-1.0-inpainting-0.1"
|
39 |
-
# SD_PIPLINE = AutoPipelineForInpainting.from_pretrained(SD_MODEL, torch_dtype=torch.float16).to(DEVICE)
|
40 |
-
# IP_ADAPTER='XLabs-AI/flux-ip-adapter-v2' #"h94/IP-Adapter"
|
41 |
-
# #SUB_FOLDER="sdxl_models"
|
42 |
-
# IP_WEIGHT_NAME='ip_adapter.safetensors' #"ip-adapter_sdxl.bin"
|
43 |
-
# SD_PIPLINE.load_ip_adapter(IP_ADAPTER, weight_name=IP_WEIGHT_NAME) #subfolder=SUB_FOLDER, weight_name=IP_WEIGHT_NAME)
|
44 |
-
# IP_SCALE=0.6
|
45 |
-
# SD_PIPLINE.set_ip_adapter_scale(IP_SCALE)
|
46 |
-
|
47 |
-
GEN_STEPS=40 #20
|
48 |
-
|
49 |
-
|
50 |
-
def refine_masks(masks: torch.BoolTensor)->np.array:
|
51 |
-
masks = masks.permute(0, 2, 3, 1)
|
52 |
-
masks = masks.float()#.mean(axis=-1)
|
53 |
-
return masks.cpu().numpy()
|
54 |
-
|
55 |
-
|
56 |
-
def get_boxes(detections:list)-> list:
|
57 |
-
boxes = []
|
58 |
-
for det in detections:
|
59 |
-
boxes.append([det['box']['xmin'], det['box']['ymin'],
|
60 |
-
det['box']['xmax'], det['box']['ymax']])
|
61 |
-
return [boxes]
|
62 |
-
|
63 |
-
|
64 |
-
def get_mask(img:Image, prompt:str, d_model:pipeline, s_model:AutoModelForMaskGeneration,
|
65 |
-
s_processor:AutoProcessor, device:str, threshold:float = 0.3)-> np.array:
|
66 |
-
|
67 |
-
labels = [label if label.endswith(".") else label+"." for label in ['face', prompt]]
|
68 |
-
dets=d_model(img, candidate_labels=labels, threshold=threshold)
|
69 |
-
|
70 |
-
boxes = get_boxes(dets)
|
71 |
-
inputs=s_processor(images=img, input_boxes=boxes, return_tensors="pt").to(DEVICE)
|
72 |
-
outputs = s_model(**inputs)
|
73 |
-
|
74 |
-
masks = s_processor.post_process_masks(
|
75 |
-
masks=outputs.pred_masks,
|
76 |
-
original_sizes=inputs.original_sizes,
|
77 |
-
reshaped_input_sizes=inputs.reshaped_input_sizes
|
78 |
-
)[0]
|
79 |
-
|
80 |
-
return refine_masks(masks)
|
81 |
|
82 |
|
83 |
-
def generate_result(model_img:
|
84 |
-
cloth_mask
|
85 |
-
prompt:str, sd_pipline
|
86 |
n_steps:int=100)->Image:
|
87 |
|
88 |
-
width, height = model_img.size
|
89 |
|
90 |
-
#cloth_mask=Image.fromarray(masks[1].astype(np.uint8)) #np.array(masks[1],dtype=np.float32)
|
91 |
generator = torch.Generator(device="cpu").manual_seed(4)
|
92 |
|
93 |
images = sd_pipline(
|
@@ -104,9 +43,8 @@ def generate_result(model_img:str, #cloth_img:str,
|
|
104 |
|
105 |
|
106 |
@spaces.GPU
|
107 |
-
def run(model_img:Image,mask:Image,
|
108 |
-
|
109 |
-
result = generate_result(model_img,mask, close_description, SD_PIPLINE, GEN_STEPS) #cloth_img, masks, close_description, SD_PIPLINE, GEN_STEPS)
|
110 |
gc.collect()
|
111 |
torch.cuda.empty_cache()
|
112 |
return result
|
@@ -114,12 +52,12 @@ def run(model_img:Image,mask:Image, cloth_class:str, close_description:str)->Ima
|
|
114 |
|
115 |
gr.Interface(
|
116 |
run,
|
117 |
-
title = '
|
118 |
inputs=[
|
119 |
gr.Image(sources = 'upload', label='Model image', type = 'pil'),
|
120 |
gr.Image(sources = 'upload', label='Mask', type = 'pil'),
|
121 |
-
gr.Textbox(label = 'Cloth class'),
|
122 |
-
gr.Textbox(label = '
|
123 |
],
|
124 |
outputs = [
|
125 |
gr.Image()
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
|
|
|
|
|
|
3 |
import torch
|
4 |
|
5 |
+
import spaces
|
6 |
+
|
7 |
#Flux
|
8 |
from diffusers import FluxFillPipeline
|
9 |
|
|
|
|
|
10 |
from PIL import Image
|
11 |
import os
|
12 |
import gc
|
|
|
15 |
|
16 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
SD_PIPLINE = FluxFillPipeline.from_pretrained("black-forest-labs/FLUX.1-Fill-dev", torch_dtype=torch.bfloat16).to("cuda")
|
19 |
|
20 |
+
GEN_STEPS=35
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
|
23 |
+
def generate_result(model_img:Image,
|
24 |
+
cloth_mask:Image,
|
25 |
+
prompt:str, sd_pipline: FluxFillPipeline,
|
26 |
n_steps:int=100)->Image:
|
27 |
|
28 |
+
width, height = model_img.size
|
29 |
|
|
|
30 |
generator = torch.Generator(device="cpu").manual_seed(4)
|
31 |
|
32 |
images = sd_pipline(
|
|
|
43 |
|
44 |
|
45 |
@spaces.GPU
|
46 |
+
def run(model_img:Image, mask:Image, prompt:str)->Image:
|
47 |
+
result = generate_result(model_img, mask, prompt, SD_PIPLINE, GEN_STEPS)
|
|
|
48 |
gc.collect()
|
49 |
torch.cuda.empty_cache()
|
50 |
return result
|
|
|
52 |
|
53 |
gr.Interface(
|
54 |
run,
|
55 |
+
title = 'Try-On Flux',
|
56 |
inputs=[
|
57 |
gr.Image(sources = 'upload', label='Model image', type = 'pil'),
|
58 |
gr.Image(sources = 'upload', label='Mask', type = 'pil'),
|
59 |
+
#gr.Textbox(label = 'Cloth class'),
|
60 |
+
gr.Textbox(label = 'Prompt')
|
61 |
],
|
62 |
outputs = [
|
63 |
gr.Image()
|