LogicGoInfotechSpaces commited on
Commit
9587da3
·
1 Parent(s): 42ba2b6

Update response formats for /colorize and /health endpoints - /colorize: Add filename, caption, change to PNG format, api_download_url - /health: Add model_type and provider fields

Browse files
Files changed (1) hide show
  1. app/main.py +56 -10
app/main.py CHANGED
@@ -190,7 +190,20 @@ async def shutdown_event():
190
  # -------------------------------------------------
191
  @app.get("/health")
192
  def health_check(request: Request):
193
- response = {"status": "healthy", "model_loaded": True}
 
 
 
 
 
 
 
 
 
 
 
 
 
194
 
195
  # Log API call
196
  log_api_call(
@@ -408,19 +421,27 @@ async def colorize(
408
 
409
  processing_time = time.time() - start_time
410
 
411
- result_id = f"{uuid.uuid4()}.jpg"
412
  output_path = os.path.join(RESULTS_DIR, result_id)
413
- output_img.save(output_path, "JPEG", quality=95)
414
 
415
  base_url = "https://logicgoinfotechspaces-text-guided-image-colorization.hf.space"
416
 
417
- result_id_clean = result_id.replace(".jpg", "")
 
 
 
 
 
 
418
 
419
  response_data = {
420
  "success": True,
421
  "result_id": result_id_clean,
422
  "download_url": f"{base_url}/results/{result_id}",
423
- "api_download": f"{base_url}/download/{result_id_clean}"
 
 
424
  }
425
 
426
  # Log to MongoDB (colorization_db -> colorizations)
@@ -489,8 +510,25 @@ def download_result(
489
 
490
  ip_address = request.client.host if request.client else None
491
 
492
- filename = f"{file_id}.jpg"
493
- path = os.path.join(RESULTS_DIR, filename)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
494
 
495
  if not os.path.exists(path):
496
  log_api_call(
@@ -510,7 +548,7 @@ def download_result(
510
  ip_address=ip_address
511
  )
512
 
513
- return FileResponse(path, media_type="image/jpeg")
514
 
515
  # -------------------------------------------------
516
  # 🌐 Public Result File
@@ -530,6 +568,14 @@ def get_result(request: Request, filename: str):
530
  )
531
  raise HTTPException(status_code=404, detail="Result not found")
532
 
 
 
 
 
 
 
 
 
533
  log_api_call(
534
  endpoint=f"/results/{filename}",
535
  method="GET",
@@ -538,7 +584,7 @@ def get_result(request: Request, filename: str):
538
  ip_address=ip_address
539
  )
540
 
541
- return FileResponse(path, media_type="image/jpeg")
542
 
543
  # -------------------------------------------------
544
  # 🌐 Public Uploaded File
@@ -566,4 +612,4 @@ def get_upload(request: Request, filename: str):
566
  ip_address=ip_address
567
  )
568
 
569
- return FileResponse(path, media_type="image/jpeg")
 
190
  # -------------------------------------------------
191
  @app.get("/health")
192
  def health_check(request: Request):
193
+ # Determine model type based on what's available
194
+ if CCO_AVAILABLE:
195
+ model_type = "cco-eccv16"
196
+ provider = "local"
197
+ else:
198
+ model_type = "gan"
199
+ provider = "local"
200
+
201
+ response = {
202
+ "status": "healthy",
203
+ "model_loaded": True,
204
+ "model_type": model_type,
205
+ "provider": provider
206
+ }
207
 
208
  # Log API call
209
  log_api_call(
 
421
 
422
  processing_time = time.time() - start_time
423
 
424
+ result_id = f"{uuid.uuid4()}.png"
425
  output_path = os.path.join(RESULTS_DIR, result_id)
426
+ output_img.save(output_path, "PNG")
427
 
428
  base_url = "https://logicgoinfotechspaces-text-guided-image-colorization.hf.space"
429
 
430
+ result_id_clean = result_id.replace(".png", "")
431
+
432
+ # Generate caption based on model used
433
+ if model_type == "cco":
434
+ caption = f"Colorized using CCO {cco_model} model with vibrant natural colors, high quality"
435
+ else:
436
+ caption = "Colorized using GAN model with vibrant natural colors, high quality"
437
 
438
  response_data = {
439
  "success": True,
440
  "result_id": result_id_clean,
441
  "download_url": f"{base_url}/results/{result_id}",
442
+ "api_download_url": f"{base_url}/download/{result_id_clean}",
443
+ "filename": result_id,
444
+ "caption": caption
445
  }
446
 
447
  # Log to MongoDB (colorization_db -> colorizations)
 
510
 
511
  ip_address = request.client.host if request.client else None
512
 
513
+ # Try PNG first, then JPG for backward compatibility
514
+ filename_png = f"{file_id}.png"
515
+ filename_jpg = f"{file_id}.jpg"
516
+ path_png = os.path.join(RESULTS_DIR, filename_png)
517
+ path_jpg = os.path.join(RESULTS_DIR, filename_jpg)
518
+
519
+ # Check which file exists
520
+ if os.path.exists(path_png):
521
+ filename = filename_png
522
+ path = path_png
523
+ media_type = "image/png"
524
+ elif os.path.exists(path_jpg):
525
+ filename = filename_jpg
526
+ path = path_jpg
527
+ media_type = "image/jpeg"
528
+ else:
529
+ filename = filename_png
530
+ path = path_png
531
+ media_type = "image/png"
532
 
533
  if not os.path.exists(path):
534
  log_api_call(
 
548
  ip_address=ip_address
549
  )
550
 
551
+ return FileResponse(path, media_type=media_type)
552
 
553
  # -------------------------------------------------
554
  # 🌐 Public Result File
 
568
  )
569
  raise HTTPException(status_code=404, detail="Result not found")
570
 
571
+ # Determine media type based on file extension
572
+ if filename.lower().endswith('.png'):
573
+ media_type = "image/png"
574
+ elif filename.lower().endswith('.jpg') or filename.lower().endswith('.jpeg'):
575
+ media_type = "image/jpeg"
576
+ else:
577
+ media_type = "image/png" # Default to PNG
578
+
579
  log_api_call(
580
  endpoint=f"/results/{filename}",
581
  method="GET",
 
584
  ip_address=ip_address
585
  )
586
 
587
+ return FileResponse(path, media_type=media_type)
588
 
589
  # -------------------------------------------------
590
  # 🌐 Public Uploaded File
 
612
  ip_address=ip_address
613
  )
614
 
615
+ return FileResponse(path, media_type=media_type)