VikramSingh178
commited on
Commit
β’
6d3950c
1
Parent(s):
5626570
commit
Browse filesFormer-commit-id: f8d758c6f68359f5fdbc1b785e785a0ce52b6266 [formerly f8c7fca9dd834b15af237f662e7cfa9918d93d11]
Former-commit-id: f88f8582db9a8490047b431ba319a532692c80e5
outputs/mask.jpg
ADDED
outputs/output.jpg
CHANGED
product_diffusion_api/routers/painting.py
CHANGED
@@ -1,7 +1,103 @@
|
|
1 |
-
from fastapi import APIRouter,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
|
|
|
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
|
|
|
|
5 |
|
6 |
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, UploadFile, File,APIRouter,HTTPException
|
2 |
+
from fastapi.responses import FileResponse
|
3 |
+
from pydantic import BaseModel
|
4 |
+
from typing import Optional
|
5 |
+
from PIL import Image
|
6 |
+
import torch
|
7 |
+
from diffusers import AutoPipelineForInpainting
|
8 |
+
from diffusers.utils import load_image
|
9 |
+
from utils import (accelerator, ImageAugmentation, clear_memory)
|
10 |
+
import hydra
|
11 |
+
from omegaconf import DictConfig
|
12 |
+
import lightning.pytorch as pl
|
13 |
+
import io
|
14 |
|
15 |
+
# Define FastAPI app
|
16 |
+
router = APIRouter()
|
17 |
|
18 |
+
class InpaintingRequest(BaseModel):
|
19 |
+
|
20 |
+
prompt: str
|
21 |
+
negative_prompt: Optional[str] = None
|
22 |
+
num_inference_steps: int
|
23 |
+
strength: float
|
24 |
+
guidance_scale: float
|
25 |
+
target_width: int
|
26 |
+
target_height: int
|
27 |
|
28 |
+
class InpaintingBatchRequest(BaseModel):
|
29 |
+
batch_input: List[InpaintingRequest]
|
30 |
|
31 |
|
32 |
+
|
33 |
+
|
34 |
+
def pil_to_s3_json(image: Image.Image, file_name: str):
|
35 |
+
image_id = str(uuid.uuid4())
|
36 |
+
s3_uploader = S3ManagerService()
|
37 |
+
image_bytes = io.BytesIO()
|
38 |
+
image.save(image_bytes, format="PNG")
|
39 |
+
image_bytes.seek(0)
|
40 |
+
|
41 |
+
unique_file_name = s3_uploader.generate_unique_file_name(file_name)
|
42 |
+
s3_uploader.upload_file(image_bytes, unique_file_name)
|
43 |
+
signed_url = s3_uploader.generate_signed_url(unique_file_name, exp=43200) # 12 hours
|
44 |
+
return {"image_id": image_id, "url": signed_url}
|
45 |
+
|
46 |
+
class AutoPaintingPipeline:
|
47 |
+
def __init__(self, model_name: str, image: Image.Image, mask_image: Image.Image, target_width: int, target_height: int):
|
48 |
+
self.model_name = model_name
|
49 |
+
self.device = accelerator()
|
50 |
+
self.pipeline = AutoPipelineForInpainting.from_pretrained(self.model_name, torch_dtype=torch.float16)
|
51 |
+
self.image = load_image(image)
|
52 |
+
self.mask_image = load_image(mask_image)
|
53 |
+
self.target_width = target_width
|
54 |
+
self.target_height = target_height
|
55 |
+
self.pipeline.to(self.device)
|
56 |
+
|
57 |
+
def run_inference(self, prompt: str, negative_prompt: Optional[str], num_inference_steps: int, strength: float, guidance_scale: float):
|
58 |
+
clear_memory()
|
59 |
+
image = load_image(self.image)
|
60 |
+
mask_image = load_image(self.mask_image)
|
61 |
+
output = self.pipeline(
|
62 |
+
prompt=prompt,
|
63 |
+
negative_prompt=negative_prompt,
|
64 |
+
image=image,
|
65 |
+
mask_image=mask_image,
|
66 |
+
num_inference_steps=num_inference_steps,
|
67 |
+
strength=strength,
|
68 |
+
guidance_scale=guidance_scale,
|
69 |
+
height=self.target_height,
|
70 |
+
width=self.target_width
|
71 |
+
).images[0]
|
72 |
+
return output
|
73 |
+
|
74 |
+
@app.post("/inpaint/")
|
75 |
+
async def inpaint(
|
76 |
+
file: UploadFile = File(...),
|
77 |
+
request: InpaintingRequest
|
78 |
+
):
|
79 |
+
image = Image.open(file.file)
|
80 |
+
augmenter = ImageAugmentation(target_width=request.target_width, target_height=request.target_height) # Use fixed size or set dynamically
|
81 |
+
extended_image = augmenter.extend_image(image)
|
82 |
+
mask_image = augmenter.generate_mask_from_bbox(extended_image, 'segmentation_model', 'detection_model')
|
83 |
+
mask_image = augmenter.invert_mask(mask_image)
|
84 |
+
|
85 |
+
pipeline = AutoPaintingPipeline(
|
86 |
+
model_name="model_name",
|
87 |
+
image=extended_image,
|
88 |
+
mask_image=mask_image,
|
89 |
+
target_width=request.target_width,
|
90 |
+
target_height=request.target_height
|
91 |
+
)
|
92 |
+
output_image = pipeline.run_inference(
|
93 |
+
prompt=request.prompt,
|
94 |
+
negative_prompt=request.negative_prompt,
|
95 |
+
num_inference_steps=request.num_inference_steps,
|
96 |
+
strength=request.strength,
|
97 |
+
guidance_scale=request.guidance_scale,
|
98 |
+
|
99 |
+
)
|
100 |
+
|
101 |
+
|
102 |
+
result = pil_to_s3_json(output_image, "output_image.png")
|
103 |
+
return result
|
scripts/__pycache__/utils.cpython-310.pyc
CHANGED
Binary files a/scripts/__pycache__/utils.cpython-310.pyc and b/scripts/__pycache__/utils.cpython-310.pyc differ
|
|
scripts/inpainting-pipeline.py
DELETED
File without changes
|
scripts/{pipeline.py β inpainting_pipeline.py}
RENAMED
@@ -1,4 +1,4 @@
|
|
1 |
-
import torch
|
2 |
from diffusers import AutoPipelineForInpainting
|
3 |
from diffusers.utils import load_image
|
4 |
from utils import (accelerator, ImageAugmentation, clear_memory)
|
|
|
1 |
+
# import torch
|
2 |
from diffusers import AutoPipelineForInpainting
|
3 |
from diffusers.utils import load_image
|
4 |
from utils import (accelerator, ImageAugmentation, clear_memory)
|
scripts/load_pipeline.py
CHANGED
@@ -6,6 +6,10 @@ from config import PROJECT_NAME
|
|
6 |
autolog(init=dict(project=PROJECT_NAME))
|
7 |
|
8 |
|
|
|
|
|
|
|
|
|
9 |
def load_pipeline(model_name, adapter_name):
|
10 |
pipe = DiffusionPipeline.from_pretrained(model_name, torch_dtype=torch.float16).to(
|
11 |
"cuda"
|
|
|
6 |
autolog(init=dict(project=PROJECT_NAME))
|
7 |
|
8 |
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
def load_pipeline(model_name, adapter_name):
|
14 |
pipe = DiffusionPipeline.from_pretrained(model_name, torch_dtype=torch.float16).to(
|
15 |
"cuda"
|