LogicGoInfotechSpaces commited on
Commit
2ebd872
·
1 Parent(s): c56d2a1

Add success/failure logging to colorizations collection

Browse files

- Update log_colorization to accept status and error fields
- Log both successful and failed API calls to colorization_db -> colorizations
- Add status and error fields only when provided (preserves existing documents)
- Update all colorize endpoints to log failures with status='failed'
- All API calls now logged to colorizations collection regardless of success/failure

Files changed (4) hide show
  1. app/database.py +22 -5
  2. app/main.py +84 -47
  3. app/main_fastai.py +15 -2
  4. app/main_sdxl.py +16 -2
app/database.py CHANGED
@@ -260,25 +260,30 @@ def log_image_upload(
260
  return False
261
 
262
  def log_colorization(
263
- result_id: str,
264
  image_id: Optional[str] = None,
265
  prompt: Optional[str] = None,
266
  model_type: Optional[str] = None,
267
  processing_time: Optional[float] = None,
268
  user_id: Optional[str] = None,
269
- ip_address: Optional[str] = None
 
 
270
  ) -> bool:
271
  """
272
- Log colorization request to MongoDB
 
273
 
274
  Args:
275
- result_id: Unique result identifier
276
  image_id: Original image identifier
277
  prompt: Text prompt used (if any)
278
  model_type: Model type used (fastai, pytorch, sdxl, etc.)
279
  processing_time: Time taken to process in seconds
280
  user_id: User ID if authenticated
281
  ip_address: Client IP address
 
 
282
 
283
  Returns:
284
  True if logged successfully, False otherwise
@@ -291,6 +296,11 @@ def log_colorization(
291
 
292
  collection = db["colorizations"]
293
 
 
 
 
 
 
294
  log_entry = {
295
  "result_id": result_id,
296
  "image_id": image_id,
@@ -302,8 +312,15 @@ def log_colorization(
302
  "ip_address": ip_address
303
  }
304
 
 
 
 
 
 
 
 
305
  result = collection.insert_one(log_entry)
306
- logger.info("Colorization logged to MongoDB: %s", result.inserted_id)
307
  return True
308
  except Exception as e:
309
  logger.error("Failed to log colorization to MongoDB: %s", str(e))
 
260
  return False
261
 
262
  def log_colorization(
263
+ result_id: Optional[str] = None,
264
  image_id: Optional[str] = None,
265
  prompt: Optional[str] = None,
266
  model_type: Optional[str] = None,
267
  processing_time: Optional[float] = None,
268
  user_id: Optional[str] = None,
269
+ ip_address: Optional[str] = None,
270
+ status: str = "success",
271
+ error: Optional[str] = None
272
  ) -> bool:
273
  """
274
+ Log colorization request to MongoDB (colorization_db -> colorizations collection)
275
+ Logs both successful and failed API calls.
276
 
277
  Args:
278
+ result_id: Unique result identifier (None for failed requests)
279
  image_id: Original image identifier
280
  prompt: Text prompt used (if any)
281
  model_type: Model type used (fastai, pytorch, sdxl, etc.)
282
  processing_time: Time taken to process in seconds
283
  user_id: User ID if authenticated
284
  ip_address: Client IP address
285
+ status: Status of the request ("success" or "failed")
286
+ error: Error message if status is "failed"
287
 
288
  Returns:
289
  True if logged successfully, False otherwise
 
296
 
297
  collection = db["colorizations"]
298
 
299
+ # Generate result_id if not provided (for failed requests)
300
+ if not result_id:
301
+ import uuid
302
+ result_id = str(uuid.uuid4())
303
+
304
  log_entry = {
305
  "result_id": result_id,
306
  "image_id": image_id,
 
312
  "ip_address": ip_address
313
  }
314
 
315
+ # Add status and error fields only if provided (for new documents)
316
+ # Existing documents won't have these fields, which is fine
317
+ if status:
318
+ log_entry["status"] = status
319
+ if error:
320
+ log_entry["error"] = error
321
+
322
  result = collection.insert_one(log_entry)
323
+ logger.info("Colorization logged to MongoDB (status: %s): %s", status, result.inserted_id)
324
  return True
325
  except Exception as e:
326
  logger.error("Failed to log colorization to MongoDB: %s", str(e))
app/main.py CHANGED
@@ -238,62 +238,99 @@ async def colorize(
238
  effective_category_id = None
239
 
240
  if not file.content_type.startswith("image/"):
 
241
  log_api_call(
242
  endpoint="/colorize",
243
  method="POST",
244
  status_code=400,
245
- error="Invalid file type",
246
  ip_address=ip_address
247
  )
248
- raise HTTPException(status_code=400, detail="Invalid file type")
249
-
250
- img = Image.open(io.BytesIO(await file.read()))
251
- output_img = colorize_image(img)
252
-
253
- processing_time = time.time() - start_time
254
-
255
- result_id = f"{uuid.uuid4()}.jpg"
256
- output_path = os.path.join(RESULTS_DIR, result_id)
257
- output_img.save(output_path)
258
-
259
- base_url = "https://logicgoinfotechspaces-text-guided-image-colorization.hf.space"
260
 
261
- result_id_clean = result_id.replace(".jpg", "")
262
-
263
- response_data = {
264
- "success": True,
265
- "result_id": result_id_clean,
266
- "download_url": f"{base_url}/results/{result_id}",
267
- "api_download": f"{base_url}/download/{result_id_clean}"
268
- }
269
-
270
- # Log to MongoDB
271
- log_colorization(
272
- result_id=result_id_clean,
273
- model_type="gan",
274
- processing_time=processing_time,
275
- user_id=effective_user_id,
276
- ip_address=ip_address
277
- )
278
-
279
- log_api_call(
280
- endpoint="/colorize",
281
- method="POST",
282
- status_code=200,
283
- request_data={"filename": file.filename, "content_type": file.content_type},
284
- response_data=response_data,
285
- user_id=effective_user_id,
286
- ip_address=ip_address
287
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
288
 
289
- log_media_click(
290
- user_id=effective_user_id,
291
- category_id=effective_category_id,
292
- endpoint_path=str(request.url.path),
293
- default_category_id=MEDIA_CLICK_DEFAULT_CATEGORY,
294
- )
295
 
296
- return response_data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297
 
298
  # -------------------------------------------------
299
  # ⬇️ Download via API (Secure)
 
238
  effective_category_id = None
239
 
240
  if not file.content_type.startswith("image/"):
241
+ error_msg = "Invalid file type"
242
  log_api_call(
243
  endpoint="/colorize",
244
  method="POST",
245
  status_code=400,
246
+ error=error_msg,
247
  ip_address=ip_address
248
  )
249
+ # Log failed colorization
250
+ log_colorization(
251
+ result_id=None,
252
+ model_type="gan",
253
+ processing_time=None,
254
+ user_id=effective_user_id,
255
+ ip_address=ip_address,
256
+ status="failed",
257
+ error=error_msg
258
+ )
259
+ raise HTTPException(status_code=400, detail=error_msg)
 
260
 
261
+ try:
262
+ img = Image.open(io.BytesIO(await file.read()))
263
+ output_img = colorize_image(img)
264
+
265
+ processing_time = time.time() - start_time
266
+
267
+ result_id = f"{uuid.uuid4()}.jpg"
268
+ output_path = os.path.join(RESULTS_DIR, result_id)
269
+ output_img.save(output_path)
270
+
271
+ base_url = "https://logicgoinfotechspaces-text-guided-image-colorization.hf.space"
272
+
273
+ result_id_clean = result_id.replace(".jpg", "")
274
+
275
+ response_data = {
276
+ "success": True,
277
+ "result_id": result_id_clean,
278
+ "download_url": f"{base_url}/results/{result_id}",
279
+ "api_download": f"{base_url}/download/{result_id_clean}"
280
+ }
281
+
282
+ # Log to MongoDB (colorization_db -> colorizations)
283
+ log_colorization(
284
+ result_id=result_id_clean,
285
+ model_type="gan",
286
+ processing_time=processing_time,
287
+ user_id=effective_user_id,
288
+ ip_address=ip_address,
289
+ status="success"
290
+ )
291
+
292
+ log_api_call(
293
+ endpoint="/colorize",
294
+ method="POST",
295
+ status_code=200,
296
+ request_data={"filename": file.filename, "content_type": file.content_type},
297
+ response_data=response_data,
298
+ user_id=effective_user_id,
299
+ ip_address=ip_address
300
+ )
301
 
302
+ log_media_click(
303
+ user_id=effective_user_id,
304
+ category_id=effective_category_id,
305
+ endpoint_path=str(request.url.path),
306
+ default_category_id=MEDIA_CLICK_DEFAULT_CATEGORY,
307
+ )
308
 
309
+ return response_data
310
+ except Exception as e:
311
+ error_msg = str(e)
312
+ logger.error("Error colorizing image: %s", error_msg)
313
+
314
+ # Log failed colorization to colorizations collection
315
+ log_colorization(
316
+ result_id=None,
317
+ model_type="gan",
318
+ processing_time=None,
319
+ user_id=effective_user_id,
320
+ ip_address=ip_address,
321
+ status="failed",
322
+ error=error_msg
323
+ )
324
+
325
+ log_api_call(
326
+ endpoint="/colorize",
327
+ method="POST",
328
+ status_code=500,
329
+ error=error_msg,
330
+ user_id=effective_user_id,
331
+ ip_address=ip_address
332
+ )
333
+ raise HTTPException(status_code=500, detail=f"Error colorizing image: {error_msg}")
334
 
335
  # -------------------------------------------------
336
  # ⬇️ Download via API (Secure)
app/main_fastai.py CHANGED
@@ -424,13 +424,14 @@ async def colorize_api(
424
 
425
  result_id = output_filename.replace(".png", "")
426
 
427
- # Log to MongoDB
428
  log_colorization(
429
  result_id=result_id,
430
  model_type=model_type,
431
  processing_time=processing_time,
432
  user_id=effective_user_id,
433
- ip_address=ip_address
 
434
  )
435
 
436
  log_api_call(
@@ -460,6 +461,18 @@ async def colorize_api(
460
  except Exception as e:
461
  error_msg = str(e)
462
  logger.error("Error colorizing image: %s", error_msg)
 
 
 
 
 
 
 
 
 
 
 
 
463
  log_api_call(
464
  endpoint="/colorize",
465
  method="POST",
 
424
 
425
  result_id = output_filename.replace(".png", "")
426
 
427
+ # Log to MongoDB (colorization_db -> colorizations)
428
  log_colorization(
429
  result_id=result_id,
430
  model_type=model_type,
431
  processing_time=processing_time,
432
  user_id=effective_user_id,
433
+ ip_address=ip_address,
434
+ status="success"
435
  )
436
 
437
  log_api_call(
 
461
  except Exception as e:
462
  error_msg = str(e)
463
  logger.error("Error colorizing image: %s", error_msg)
464
+
465
+ # Log failed colorization to colorizations collection
466
+ log_colorization(
467
+ result_id=None,
468
+ model_type=model_type,
469
+ processing_time=None,
470
+ user_id=effective_user_id,
471
+ ip_address=ip_address,
472
+ status="failed",
473
+ error=error_msg
474
+ )
475
+
476
  log_api_call(
477
  endpoint="/colorize",
478
  method="POST",
app/main_sdxl.py CHANGED
@@ -827,14 +827,15 @@ async def colorize_api(
827
  "caption": caption
828
  }
829
 
830
- # Log to MongoDB
831
  log_colorization(
832
  result_id=result_id,
833
  prompt=positive_prompt,
834
  model_type="sdxl",
835
  processing_time=processing_time,
836
  user_id=effective_user_id,
837
- ip_address=ip_address
 
838
  )
839
 
840
  log_api_call(
@@ -865,6 +866,19 @@ async def colorize_api(
865
  except Exception as e:
866
  error_msg = str(e)
867
  logger.error("Error colorizing image: %s", error_msg)
 
 
 
 
 
 
 
 
 
 
 
 
 
868
  log_api_call(
869
  endpoint="/colorize",
870
  method="POST",
 
827
  "caption": caption
828
  }
829
 
830
+ # Log to MongoDB (colorization_db -> colorizations)
831
  log_colorization(
832
  result_id=result_id,
833
  prompt=positive_prompt,
834
  model_type="sdxl",
835
  processing_time=processing_time,
836
  user_id=effective_user_id,
837
+ ip_address=ip_address,
838
+ status="success"
839
  )
840
 
841
  log_api_call(
 
866
  except Exception as e:
867
  error_msg = str(e)
868
  logger.error("Error colorizing image: %s", error_msg)
869
+
870
+ # Log failed colorization to colorizations collection
871
+ log_colorization(
872
+ result_id=None,
873
+ prompt=positive_prompt,
874
+ model_type="sdxl",
875
+ processing_time=None,
876
+ user_id=effective_user_id,
877
+ ip_address=ip_address,
878
+ status="failed",
879
+ error=error_msg
880
+ )
881
+
882
  log_api_call(
883
  endpoint="/colorize",
884
  method="POST",