zeyadcode commited on
Commit
ba77147
·
verified ·
1 Parent(s): 6223ff1

Sync from GitHub via hub-sync

Browse files
Files changed (3) hide show
  1. main.py +3 -1
  2. schemas/inpaint_request.py +2 -1
  3. tasks.py +24 -5
main.py CHANGED
@@ -58,7 +58,8 @@ def upload(img: UploadFile = File(...)):
58
  shutil.copyfileobj(img.file, buffer)
59
 
60
  return {"job_id": job_id}
61
-
 
62
  @app.get("/job_status/{job_id}")
63
  def get_job_status(job_id: str):
64
  result = AsyncResult(job_id, app=tasks.celery_app)
@@ -92,6 +93,7 @@ async def inpaint(job_id: str, data: InpaintRequest):
92
  job_id,
93
  data.mode,
94
  data.positive_prompt,
 
95
  data.strength,
96
  data.num_inference_steps,
97
  ],
 
58
  shutil.copyfileobj(img.file, buffer)
59
 
60
  return {"job_id": job_id}
61
+
62
+
63
  @app.get("/job_status/{job_id}")
64
  def get_job_status(job_id: str):
65
  result = AsyncResult(job_id, app=tasks.celery_app)
 
93
  job_id,
94
  data.mode,
95
  data.positive_prompt,
96
+ data.mask_grid,
97
  data.strength,
98
  data.num_inference_steps,
99
  ],
schemas/inpaint_request.py CHANGED
@@ -6,5 +6,6 @@ from pydantic import BaseModel, Field
6
  class InpaintRequest(BaseModel):
7
  mode: Literal["blur", "remove", "diffusion"] = "blur"
8
  positive_prompt: str = ""
 
9
  strength: float = Field(default=0.7, ge=0.0, le=1.0)
10
- num_inference_steps: int = Field(default=2, ge=1, le=4)
 
6
  class InpaintRequest(BaseModel):
7
  mode: Literal["blur", "remove", "diffusion"] = "blur"
8
  positive_prompt: str = ""
9
+ mask_grid: list[list[int]] = Field(default_factory=list)
10
  strength: float = Field(default=0.7, ge=0.0, le=1.0)
11
+ num_inference_steps: int = Field(default=2, ge=1, le=4)
tasks.py CHANGED
@@ -9,6 +9,8 @@ import numpy as np
9
  import json
10
  from typing import Any
11
  from services import model_manager
 
 
12
 
13
  MODELS: dict[str, Any] = {
14
  "detector": None,
@@ -96,12 +98,25 @@ def segment_objects(self, job_id: str, bboxes=None, points=None, point_labels=No
96
  return True
97
 
98
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  @celery_app.task(bind=True)
100
  def inpaint(
101
  self,
102
  job_id: str,
103
  mode: str = "blur",
104
  positive_prompt: str = "",
 
105
  strength=0.7,
106
  num_inference_steps: int = 4,
107
  ) -> bool:
@@ -111,7 +126,11 @@ def inpaint(
111
  save_path.unlink()
112
 
113
  orig_img = Image.open(job_path / config.INPUT_IMG_NAME).convert("RGB")
114
- mask_img = Image.open(job_path / config.SEGMENTOR_OUT_BIN_PATH).convert("L")
 
 
 
 
115
 
116
  if mode == "diffusion":
117
  model = model_manager.get_inpaintor_model(MODELS)
@@ -121,7 +140,7 @@ def inpaint(
121
  image=orig_img.resize(model_size),
122
  mask_image=mask_img.resize(model_size, Image.Resampling.NEAREST),
123
  num_inference_steps=num_inference_steps,
124
- strength= strength,
125
  guidance_scale=0,
126
  ).images[0]
127
  result = generated.resize(orig_img.size)
@@ -137,10 +156,10 @@ def inpaint(
137
  raise ValueError(f"Unsupported inpaint mode: {mode}")
138
 
139
  result.convert("RGB").save(save_path)
140
-
141
  if self.request.id is None:
142
  return result
143
-
144
  return True
145
 
146
 
@@ -164,7 +183,7 @@ if __name__ == "__main__":
164
  mode="diffusion",
165
  positive_prompt="red hat high quality",
166
  num_inference_steps=4,
167
- ) # type: ignore
168
 
169
  # %%
170
  img = segment_res.plot()
 
9
  import json
10
  from typing import Any
11
  from services import model_manager
12
+ import base64
13
+ import io
14
 
15
  MODELS: dict[str, Any] = {
16
  "detector": None,
 
98
  return True
99
 
100
 
101
+ def _mask_grid_to_image(mask_grid):
102
+ if not mask_grid:
103
+ return None
104
+
105
+ arr = np.array(mask_grid, dtype=np.uint8)
106
+ if arr.ndim != 2:
107
+ raise ValueError("mask_grid must be a 2D array")
108
+
109
+ arr = (arr > 0).astype(np.uint8) * 255
110
+ return Image.fromarray(arr, mode="L")
111
+
112
+
113
  @celery_app.task(bind=True)
114
  def inpaint(
115
  self,
116
  job_id: str,
117
  mode: str = "blur",
118
  positive_prompt: str = "",
119
+ mask_grid=None,
120
  strength=0.7,
121
  num_inference_steps: int = 4,
122
  ) -> bool:
 
126
  save_path.unlink()
127
 
128
  orig_img = Image.open(job_path / config.INPUT_IMG_NAME).convert("RGB")
129
+
130
+ mask_img = _mask_grid_to_image(mask_grid)
131
+ if mask_img is None:
132
+ # fallback for legacy auto-mask path
133
+ mask_img = Image.open(job_path / config.SEGMENTOR_OUT_BIN_PATH).convert("L")
134
 
135
  if mode == "diffusion":
136
  model = model_manager.get_inpaintor_model(MODELS)
 
140
  image=orig_img.resize(model_size),
141
  mask_image=mask_img.resize(model_size, Image.Resampling.NEAREST),
142
  num_inference_steps=num_inference_steps,
143
+ strength=strength,
144
  guidance_scale=0,
145
  ).images[0]
146
  result = generated.resize(orig_img.size)
 
156
  raise ValueError(f"Unsupported inpaint mode: {mode}")
157
 
158
  result.convert("RGB").save(save_path)
159
+
160
  if self.request.id is None:
161
  return result
162
+
163
  return True
164
 
165
 
 
183
  mode="diffusion",
184
  positive_prompt="red hat high quality",
185
  num_inference_steps=4,
186
+ ) # type: ignore
187
 
188
  # %%
189
  img = segment_res.plot()