Spaces:
Runtime error
Runtime error
VikramSingh178
commited on
Commit
β’
c220c76
1
Parent(s):
6c850b1
chore: Update Inpainting model name in config.py
Browse filesFormer-commit-id: 22fa0b9f756523fb282ef6ae1b1d691a810b21ca [formerly 98fe0a0084679efccb8eb55bc9b061f439126cce]
Former-commit-id: e181cfba0759e658521596a5031cce2e1a5b1f5b
- .vscode/settings.json +3 -0
- api/__pycache__/endpoints.cpython-310.pyc +0 -0
- api/endpoints.py +1 -0
- api/routers/__pycache__/painting.cpython-310.pyc +0 -0
- api/routers/__pycache__/sdxl_text_to_image.cpython-310.pyc +0 -0
- api/routers/batch_painting.py +16 -0
- api/routers/painting.py +39 -130
- api/routers/sdxl_text_to_image.py +1 -1
- scripts/__pycache__/inpainting_pipeline.cpython-310.pyc +0 -0
- scripts/config.py +1 -1
- scripts/inpainting_pipeline.py +1 -1
.vscode/settings.json
CHANGED
@@ -2,5 +2,8 @@
|
|
2 |
"python.REPL.enableREPLSmartSend": false,
|
3 |
"circleci.persistedProjectSelection": [
|
4 |
"circleci/BQyaZDyai3ejufsymJgXtx/G7GFeA574Ga7r2VtdHXBbu"
|
|
|
|
|
|
|
5 |
]
|
6 |
}
|
|
|
2 |
"python.REPL.enableREPLSmartSend": false,
|
3 |
"circleci.persistedProjectSelection": [
|
4 |
"circleci/BQyaZDyai3ejufsymJgXtx/G7GFeA574Ga7r2VtdHXBbu"
|
5 |
+
],
|
6 |
+
"python.analysis.extraPaths": [
|
7 |
+
"./scripts"
|
8 |
]
|
9 |
}
|
api/__pycache__/endpoints.cpython-310.pyc
CHANGED
Binary files a/api/__pycache__/endpoints.cpython-310.pyc and b/api/__pycache__/endpoints.cpython-310.pyc differ
|
|
api/endpoints.py
CHANGED
@@ -53,3 +53,4 @@ def check_health():
|
|
53 |
|
54 |
|
55 |
|
|
|
|
53 |
|
54 |
|
55 |
|
56 |
+
#uvicorn.run(app, host='127.0.0.1', port=8000)
|
api/routers/__pycache__/painting.cpython-310.pyc
CHANGED
Binary files a/api/routers/__pycache__/painting.cpython-310.pyc and b/api/routers/__pycache__/painting.cpython-310.pyc differ
|
|
api/routers/__pycache__/sdxl_text_to_image.cpython-310.pyc
CHANGED
Binary files a/api/routers/__pycache__/sdxl_text_to_image.cpython-310.pyc and b/api/routers/__pycache__/sdxl_text_to_image.cpython-310.pyc differ
|
|
api/routers/batch_painting.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sys
|
2 |
+
sys.path.append('../scripts')
|
3 |
+
import os
|
4 |
+
import uuid
|
5 |
+
from typing import List, Tuple, Any
|
6 |
+
from fastapi import APIRouter, File, UploadFile, HTTPException, Form
|
7 |
+
from PIL import Image
|
8 |
+
import lightning.pytorch as pl
|
9 |
+
from utils import pil_to_s3_json, pil_to_b64_json, ImageAugmentation, accelerator
|
10 |
+
from inpainting_pipeline import load_pipeline
|
11 |
+
from async_batcher.batcher import AsyncBatcher
|
12 |
+
from config import INPAINTING_MODEL_NAME
|
13 |
+
router = APIRouter()
|
14 |
+
|
15 |
+
|
16 |
+
inpainting_pipeline = load_pipeline(model_name = INPAINTING_MODEL_NAME,enable_)
|
api/routers/painting.py
CHANGED
@@ -1,25 +1,32 @@
|
|
1 |
-
from fastapi import APIRouter, File, UploadFile, HTTPException, Form
|
2 |
-
from PIL import Image
|
3 |
import sys
|
4 |
-
sys.path.append(
|
|
|
5 |
import uuid
|
|
|
|
|
|
|
6 |
import lightning.pytorch as pl
|
7 |
-
from typing import List
|
8 |
from utils import pil_to_s3_json, pil_to_b64_json, ImageAugmentation, accelerator
|
9 |
from inpainting_pipeline import AutoPaintingPipeline, load_pipeline
|
10 |
from hydra import compose, initialize
|
11 |
-
from pydantic import BaseModel
|
12 |
from async_batcher.batcher import AsyncBatcher
|
13 |
-
from typing import Dict
|
14 |
-
|
15 |
|
16 |
-
router = APIRouter()
|
17 |
pl.seed_everything(42)
|
|
|
18 |
|
|
|
19 |
with initialize(version_base=None, config_path="../../configs"):
|
20 |
cfg = compose(config_name="inpainting")
|
21 |
inpainting_pipeline = load_pipeline(cfg.model, accelerator(), enable_compile=True)
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
def augment_image(image_path, target_width, target_height, roi_scale, segmentation_model_name, detection_model_name):
|
24 |
image = Image.open(image_path)
|
25 |
image_augmentation = ImageAugmentation(target_width, target_height, roi_scale)
|
@@ -28,13 +35,26 @@ def augment_image(image_path, target_width, target_height, roi_scale, segmentati
|
|
28 |
inverted_mask = image_augmentation.invert_mask(mask)
|
29 |
return image, inverted_mask
|
30 |
|
31 |
-
def run_inference(cfg
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
painting_pipeline = AutoPaintingPipeline(
|
40 |
pipeline=inpainting_pipeline,
|
@@ -55,58 +75,7 @@ def run_inference(cfg: dict, image_path: str, prompt: str, negative_prompt: str,
|
|
55 |
else:
|
56 |
raise ValueError("Invalid mode. Supported modes are 'b64_json' and 's3_json'.")
|
57 |
|
58 |
-
class InpaintingRequest(BaseModel):
|
59 |
-
prompt: str
|
60 |
-
negative_prompt: str
|
61 |
-
num_inference_steps: int
|
62 |
-
strength: float
|
63 |
-
guidance_scale: float
|
64 |
-
num_images: int = 1
|
65 |
-
|
66 |
-
class InpaintingBatcher(AsyncBatcher[List[Dict], dict]):
|
67 |
-
def __init__(self, pipeline, cfg):
|
68 |
-
self.pipeline = pipeline
|
69 |
-
self.cfg = cfg
|
70 |
-
|
71 |
-
def process_batch(self, batch: List[Dict], image_paths: List[str]) -> List[dict]:
|
72 |
-
results = []
|
73 |
-
for data, image_path in zip(batch, image_paths):
|
74 |
-
try:
|
75 |
-
image, mask_image = augment_image(
|
76 |
-
image_path,
|
77 |
-
self.cfg['target_width'],
|
78 |
-
self.cfg['target_height'],
|
79 |
-
self.cfg['roi_scale'],
|
80 |
-
self.cfg['segmentation_model'],
|
81 |
-
self.cfg['detection_model']
|
82 |
-
)
|
83 |
-
|
84 |
-
pipeline = AutoPaintingPipeline(
|
85 |
-
image=image,
|
86 |
-
mask_image=mask_image,
|
87 |
-
target_height=self.cfg['target_height'],
|
88 |
-
target_width=self.cfg['target_width']
|
89 |
-
)
|
90 |
-
output = pipeline.run_inference(
|
91 |
-
prompt=data['prompt'],
|
92 |
-
negative_prompt=data['negative_prompt'],
|
93 |
-
num_inference_steps=data['num_inference_steps'],
|
94 |
-
strength=data['strength'],
|
95 |
-
guidance_scale=data['guidance_scale']
|
96 |
-
)
|
97 |
|
98 |
-
if data['mode'] == "s3_json":
|
99 |
-
result = pil_to_s3_json(output, 'inpainting_image')
|
100 |
-
elif data['mode'] == "b64_json":
|
101 |
-
result = pil_to_b64_json(output)
|
102 |
-
else:
|
103 |
-
raise ValueError("Invalid mode. Supported modes are 'b64_json' and 's3_json'.")
|
104 |
-
|
105 |
-
results.append(result)
|
106 |
-
except Exception as e:
|
107 |
-
print(f"Error in process_batch: {e}")
|
108 |
-
raise HTTPException(status_code=500, detail="Batch inference failed")
|
109 |
-
return results
|
110 |
|
111 |
@router.post("/inpainting")
|
112 |
async def inpainting_inference(
|
@@ -117,74 +86,14 @@ async def inpainting_inference(
|
|
117 |
strength: float = Form(...),
|
118 |
guidance_scale: float = Form(...),
|
119 |
mode: str = Form(...),
|
120 |
-
num_images: int = Form(1)
|
|
|
121 |
):
|
122 |
-
"""
|
123 |
-
Run the inpainting/outpainting inference pipeline.
|
124 |
-
|
125 |
-
Parameters:
|
126 |
-
- image: UploadFile - The image file to be used for inpainting/outpainting.
|
127 |
-
- prompt: str - The prompt text for guiding the inpainting/outpainting process.
|
128 |
-
- negative_prompt: str - The negative prompt text for guiding the inpainting/outpainting process.
|
129 |
-
- num_inference_steps: int - The number of inference steps to perform during the inpainting/outpainting process.
|
130 |
-
- strength: float - The strength parameter for controlling the inpainting/outpainting process.
|
131 |
-
- guidance_scale: float - The guidance scale parameter for controlling the inpainting/outpainting process.
|
132 |
-
- mode: str - The output mode, either "s3_json" or "b64_json".
|
133 |
-
- num_images: int - The number of images to generate.
|
134 |
-
|
135 |
-
Returns:
|
136 |
-
- result: The result of the inpainting/outpainting process.
|
137 |
-
|
138 |
-
Raises:
|
139 |
-
- HTTPException: If an error occurs during the inpainting/outpainting process.
|
140 |
-
"""
|
141 |
try:
|
142 |
-
|
143 |
-
|
144 |
-
with open(image_path, "wb") as f:
|
145 |
-
f.write(image_bytes)
|
146 |
-
|
147 |
-
result = run_inference(
|
148 |
-
cfg, image_path, prompt, negative_prompt, num_inference_steps, strength, guidance_scale, mode, num_images
|
149 |
-
)
|
150 |
-
|
151 |
return result
|
152 |
except Exception as e:
|
153 |
raise HTTPException(status_code=500, detail=str(e))
|
154 |
|
155 |
-
@router.post("/inpainting_batch")
|
156 |
-
async def inpainting_batch_inference(
|
157 |
-
batch: List[dict],
|
158 |
-
images: List[UploadFile] = File(...)
|
159 |
-
):
|
160 |
-
"""
|
161 |
-
Run batch inpainting/outpainting inference pipeline.
|
162 |
-
|
163 |
-
Parameters:
|
164 |
-
- batch: List[dict] - The batch of requests containing parameters for the inpainting/outpainting process.
|
165 |
-
- images: List[UploadFile] - The list of image files to be used for inpainting/outpainting.
|
166 |
-
|
167 |
-
Returns:
|
168 |
-
- results: The results of the inpainting/outpainting process for each request.
|
169 |
-
|
170 |
-
Raises:
|
171 |
-
- HTTPException: If an error occurs during the inpainting/outpainting process.
|
172 |
-
"""
|
173 |
-
try:
|
174 |
-
image_paths = []
|
175 |
-
for image in images:
|
176 |
-
image_bytes = await image.read()
|
177 |
-
image_path = f"/tmp/{uuid.uuid4()}.png"
|
178 |
-
with open(image_path, "wb") as f:
|
179 |
-
f.write(image_bytes)
|
180 |
-
image_paths.append(image_path)
|
181 |
-
|
182 |
-
batcher = InpaintingBatcher(pipeline, cfg)
|
183 |
-
results = batcher.process_batch(batch, image_paths)
|
184 |
-
|
185 |
-
return results
|
186 |
-
except Exception as e:
|
187 |
-
raise HTTPException(status_code=500, detail=str(e))
|
188 |
-
|
189 |
-
|
190 |
|
|
|
|
|
|
|
1 |
import sys
|
2 |
+
sys.path.append('../scripts')
|
3 |
+
import os
|
4 |
import uuid
|
5 |
+
from typing import List, Tuple, Any
|
6 |
+
from fastapi import APIRouter, File, UploadFile, HTTPException, Form
|
7 |
+
from PIL import Image
|
8 |
import lightning.pytorch as pl
|
|
|
9 |
from utils import pil_to_s3_json, pil_to_b64_json, ImageAugmentation, accelerator
|
10 |
from inpainting_pipeline import AutoPaintingPipeline, load_pipeline
|
11 |
from hydra import compose, initialize
|
|
|
12 |
from async_batcher.batcher import AsyncBatcher
|
|
|
|
|
13 |
|
|
|
14 |
pl.seed_everything(42)
|
15 |
+
router = APIRouter()
|
16 |
|
17 |
+
# Initialize the configuration and pipeline
|
18 |
with initialize(version_base=None, config_path="../../configs"):
|
19 |
cfg = compose(config_name="inpainting")
|
20 |
inpainting_pipeline = load_pipeline(cfg.model, accelerator(), enable_compile=True)
|
21 |
|
22 |
+
async def save_image(image: UploadFile) -> str:
|
23 |
+
"""Save an uploaded image to a temporary file and return the file path."""
|
24 |
+
file_name = f"{uuid.uuid4()}.png"
|
25 |
+
file_path = os.path.join("/tmp", file_name)
|
26 |
+
with open(file_path, "wb") as f:
|
27 |
+
f.write(await image.read())
|
28 |
+
return file_path
|
29 |
+
|
30 |
def augment_image(image_path, target_width, target_height, roi_scale, segmentation_model_name, detection_model_name):
|
31 |
image = Image.open(image_path)
|
32 |
image_augmentation = ImageAugmentation(target_width, target_height, roi_scale)
|
|
|
35 |
inverted_mask = image_augmentation.invert_mask(mask)
|
36 |
return image, inverted_mask
|
37 |
|
38 |
+
def run_inference(cfg,
|
39 |
+
image_path: str,
|
40 |
+
prompt: str,
|
41 |
+
negative_prompt: str,
|
42 |
+
num_inference_steps: int,
|
43 |
+
strength: float,
|
44 |
+
guidance_scale: float,
|
45 |
+
mode: str,
|
46 |
+
num_images: int,
|
47 |
+
use_augmentation: bool):
|
48 |
+
if use_augmentation:
|
49 |
+
image, mask_image = augment_image(image_path,
|
50 |
+
cfg['target_width'],
|
51 |
+
cfg['target_height'],
|
52 |
+
cfg['roi_scale'],
|
53 |
+
cfg['segmentation_model'],
|
54 |
+
cfg['detection_model'])
|
55 |
+
else:
|
56 |
+
image = Image.open(image_path)
|
57 |
+
mask_image = None # Assume mask_image is provided or generated separately
|
58 |
|
59 |
painting_pipeline = AutoPaintingPipeline(
|
60 |
pipeline=inpainting_pipeline,
|
|
|
75 |
else:
|
76 |
raise ValueError("Invalid mode. Supported modes are 'b64_json' and 's3_json'.")
|
77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
@router.post("/inpainting")
|
81 |
async def inpainting_inference(
|
|
|
86 |
strength: float = Form(...),
|
87 |
guidance_scale: float = Form(...),
|
88 |
mode: str = Form(...),
|
89 |
+
num_images: int = Form(1),
|
90 |
+
|
91 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
try:
|
93 |
+
image_path = await save_image(image)
|
94 |
+
result = run_inference(cfg, image_path, prompt, negative_prompt, num_inference_steps, strength, guidance_scale, mode, num_images, use_augmentation=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
return result
|
96 |
except Exception as e:
|
97 |
raise HTTPException(status_code=500, detail=str(e))
|
98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
api/routers/sdxl_text_to_image.py
CHANGED
@@ -184,7 +184,7 @@ async def sdxl_v0_lora_inference(data: InputFormat):
|
|
184 |
async def sdxl_v0_lora_inference_batch(data: List[InputFormat]):
|
185 |
batcher = SDXLLoraBatcher(max_batch_size=64)
|
186 |
try:
|
187 |
-
predictions = batcher.process_batch(data)
|
188 |
return predictions
|
189 |
except Exception as e:
|
190 |
print(f"Error in /sdxl_v0_lora_inference/batch: {e}")
|
|
|
184 |
async def sdxl_v0_lora_inference_batch(data: List[InputFormat]):
|
185 |
batcher = SDXLLoraBatcher(max_batch_size=64)
|
186 |
try:
|
187 |
+
predictions = await batcher.process_batch(data)
|
188 |
return predictions
|
189 |
except Exception as e:
|
190 |
print(f"Error in /sdxl_v0_lora_inference/batch: {e}")
|
scripts/__pycache__/inpainting_pipeline.cpython-310.pyc
CHANGED
Binary files a/scripts/__pycache__/inpainting_pipeline.cpython-310.pyc and b/scripts/__pycache__/inpainting_pipeline.cpython-310.pyc differ
|
|
scripts/config.py
CHANGED
@@ -9,7 +9,7 @@ CAPTIONING_MODEL_NAME = "Salesforce/blip-image-captioning-base"
|
|
9 |
SEGMENTATION_MODEL_NAME = "facebook/sam-vit-large"
|
10 |
DETECTION_MODEL_NAME = "yolov8l"
|
11 |
ENABLE_COMPILE = False
|
12 |
-
INPAINTING_MODEL_NAME = ''
|
13 |
|
14 |
|
15 |
|
|
|
9 |
SEGMENTATION_MODEL_NAME = "facebook/sam-vit-large"
|
10 |
DETECTION_MODEL_NAME = "yolov8l"
|
11 |
ENABLE_COMPILE = False
|
12 |
+
INPAINTING_MODEL_NAME = 'kandinskyv2.2'
|
13 |
|
14 |
|
15 |
|
scripts/inpainting_pipeline.py
CHANGED
@@ -8,7 +8,7 @@ from PIL import Image
|
|
8 |
from functools import lru_cache
|
9 |
|
10 |
@lru_cache(maxsize=1)
|
11 |
-
def load_pipeline(model_name: str, device, enable_compile: bool =
|
12 |
pipeline = AutoPipelineForInpainting.from_pretrained(model_name, torch_dtype=torch.float16)
|
13 |
if enable_compile:
|
14 |
pipeline.unet.to(memory_format=torch.channels_last)
|
|
|
8 |
from functools import lru_cache
|
9 |
|
10 |
@lru_cache(maxsize=1)
|
11 |
+
def load_pipeline(model_name: str, device, enable_compile: bool = False):
|
12 |
pipeline = AutoPipelineForInpainting.from_pretrained(model_name, torch_dtype=torch.float16)
|
13 |
if enable_compile:
|
14 |
pipeline.unet.to(memory_format=torch.channels_last)
|