Update api/main.py
Browse files- api/main.py +33 -2
api/main.py
CHANGED
|
@@ -11,7 +11,20 @@ from pydantic import BaseModel
|
|
| 11 |
from PIL import Image
|
| 12 |
|
| 13 |
from src.core import process_inpaint
|
|
|
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# Directories (use writable space on HF Spaces)
|
| 16 |
BASE_DIR = os.environ.get("DATA_DIR", "/data")
|
| 17 |
if not os.path.isdir(BASE_DIR):
|
|
@@ -137,7 +150,7 @@ def inpaint(req: InpaintRequest, _: None = Depends(bearer_auth)) -> Dict[str, st
|
|
| 137 |
raise HTTPException(status_code=404, detail="mask_id not found")
|
| 138 |
|
| 139 |
img_rgba = _load_rgba_image(file_store[req.image_id]["path"])
|
| 140 |
-
mask_img = Image.open(file_store[req.mask_id]["path"])
|
| 141 |
mask_rgba = _load_rgba_mask_from_image(mask_img)
|
| 142 |
|
| 143 |
result = process_inpaint(np.array(img_rgba), mask_rgba)
|
|
@@ -146,6 +159,15 @@ def inpaint(req: InpaintRequest, _: None = Depends(bearer_auth)) -> Dict[str, st
|
|
| 146 |
Image.fromarray(result).save(result_path)
|
| 147 |
|
| 148 |
logs.append({"result": result_name, "timestamp": datetime.utcnow().isoformat()})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
return {"result": result_name}
|
| 150 |
|
| 151 |
|
|
@@ -178,7 +200,6 @@ def inpaint_multipart(
|
|
| 178 |
request: Request = None,
|
| 179 |
_: None = Depends(bearer_auth),
|
| 180 |
) -> Dict[str, str]:
|
| 181 |
-
# Load in-memory
|
| 182 |
img = Image.open(image.file).convert("RGBA")
|
| 183 |
m = Image.open(mask.file)
|
| 184 |
mask_rgba = _load_rgba_mask_from_image(m)
|
|
@@ -199,6 +220,16 @@ def inpaint_multipart(
|
|
| 199 |
if url:
|
| 200 |
entry["url"] = url
|
| 201 |
logs.append(entry)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
resp: Dict[str, str] = {"result": result_name}
|
| 203 |
if url:
|
| 204 |
resp["url"] = url
|
|
|
|
| 11 |
from PIL import Image
|
| 12 |
|
| 13 |
from src.core import process_inpaint
|
| 14 |
+
from pymongo import MongoClient
|
| 15 |
|
| 16 |
+
# Connect to MongoDB (URI must be in HF env variables)
|
| 17 |
+
MONGO_URI = os.environ.get("MONGO_URI")
|
| 18 |
+
mongo_client = None
|
| 19 |
+
mongo_collection = None
|
| 20 |
+
|
| 21 |
+
if MONGO_URI:
|
| 22 |
+
try:
|
| 23 |
+
mongo_client = MongoClient(MONGO_URI)
|
| 24 |
+
mongo_db = mongo_client["object-remove-logs"]
|
| 25 |
+
mongo_collection = mongo_db["api_logs"]
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print("Mongo connection failed:", e)
|
| 28 |
# Directories (use writable space on HF Spaces)
|
| 29 |
BASE_DIR = os.environ.get("DATA_DIR", "/data")
|
| 30 |
if not os.path.isdir(BASE_DIR):
|
|
|
|
| 150 |
raise HTTPException(status_code=404, detail="mask_id not found")
|
| 151 |
|
| 152 |
img_rgba = _load_rgba_image(file_store[req.image_id]["path"])
|
| 153 |
+
mask_img = Image.open(file_store[req.mask_id]["path"])
|
| 154 |
mask_rgba = _load_rgba_mask_from_image(mask_img)
|
| 155 |
|
| 156 |
result = process_inpaint(np.array(img_rgba), mask_rgba)
|
|
|
|
| 159 |
Image.fromarray(result).save(result_path)
|
| 160 |
|
| 161 |
logs.append({"result": result_name, "timestamp": datetime.utcnow().isoformat()})
|
| 162 |
+
|
| 163 |
+
# Mongo logging
|
| 164 |
+
if mongo_collection:
|
| 165 |
+
mongo_collection.insert_one({
|
| 166 |
+
"endpoint": "/inpaint",
|
| 167 |
+
"result": result_name,
|
| 168 |
+
"timestamp": datetime.utcnow().isoformat()
|
| 169 |
+
})
|
| 170 |
+
|
| 171 |
return {"result": result_name}
|
| 172 |
|
| 173 |
|
|
|
|
| 200 |
request: Request = None,
|
| 201 |
_: None = Depends(bearer_auth),
|
| 202 |
) -> Dict[str, str]:
|
|
|
|
| 203 |
img = Image.open(image.file).convert("RGBA")
|
| 204 |
m = Image.open(mask.file)
|
| 205 |
mask_rgba = _load_rgba_mask_from_image(m)
|
|
|
|
| 220 |
if url:
|
| 221 |
entry["url"] = url
|
| 222 |
logs.append(entry)
|
| 223 |
+
|
| 224 |
+
# Mongo logging
|
| 225 |
+
if mongo_collection:
|
| 226 |
+
mongo_collection.insert_one({
|
| 227 |
+
"endpoint": "/inpaint-multipart",
|
| 228 |
+
"result": result_name,
|
| 229 |
+
"url": url,
|
| 230 |
+
"timestamp": datetime.utcnow().isoformat()
|
| 231 |
+
})
|
| 232 |
+
|
| 233 |
resp: Dict[str, str] = {"result": result_name}
|
| 234 |
if url:
|
| 235 |
resp["url"] = url
|