LogicGoInfotechSpaces commited on
Commit
7984e65
·
verified ·
1 Parent(s): 2eb209a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -2
app.py CHANGED
@@ -17,7 +17,7 @@ from fastapi.concurrency import run_in_threadpool
17
  import shutil
18
  import firebase_admin
19
  from firebase_admin import credentials, auth
20
-
21
  from huggingface_hub import InferenceClient
22
 
23
  # ---------------------------------------------------------------------
@@ -116,6 +116,41 @@ def prepare_image(file_bytes: bytes) -> Image.Image:
116
  img = resize_image_if_needed(img, max_size=(1024, 1024))
117
  return img
118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  # ---------------------------------------------------------------------
120
  # Endpoints
121
  # ---------------------------------------------------------------------
@@ -358,6 +393,26 @@ async def generate(
358
  "user_email": user.get("email"),
359
  }
360
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
361
 
362
  response_time_ms = round((time.time() - start_time) * 1000)
363
 
@@ -378,7 +433,10 @@ async def generate(
378
  return JSONResponse({
379
  "output_image_id": str(out_id),
380
  "user": user.get("email"),
381
- "response_time_ms": response_time_ms
 
 
 
382
  })
383
  ####################---------------------------------------------------------------------------------------------------###
384
  ###-----OLD CODE--------------####
 
17
  import shutil
18
  import firebase_admin
19
  from firebase_admin import credentials, auth
20
+ from PIL import Image
21
  from huggingface_hub import InferenceClient
22
 
23
  # ---------------------------------------------------------------------
 
116
  img = resize_image_if_needed(img, max_size=(1024, 1024))
117
  return img
118
 
119
+ MAX_COMPRESSED_SIZE = 2 * 1024 * 1024 # 2 MB
120
+ def compress_pil_image_to_2mb(
121
+ pil_img: Image.Image,
122
+ max_dim: int = 1280
123
+ ) -> bytes:
124
+ """
125
+ Resize + compress PIL image to <= 2MB.
126
+ Returns JPEG bytes.
127
+ """
128
+ img = pil_img.convert("RGB")
129
+
130
+ # Resize (maintain aspect ratio)
131
+ img.thumbnail((max_dim, max_dim), Image.LANCZOS)
132
+
133
+ quality = 85
134
+ buffer = io.BytesIO()
135
+
136
+ while quality >= 40:
137
+ buffer.seek(0)
138
+ buffer.truncate()
139
+
140
+ img.save(
141
+ buffer,
142
+ format="JPEG",
143
+ quality=quality,
144
+ optimize=True,
145
+ progressive=True
146
+ )
147
+
148
+ if buffer.tell() <= MAX_COMPRESSED_SIZE:
149
+ break
150
+
151
+ quality -= 5
152
+
153
+ return buffer.getvalue()
154
  # ---------------------------------------------------------------------
155
  # Endpoints
156
  # ---------------------------------------------------------------------
 
393
  "user_email": user.get("email"),
394
  }
395
  )
396
+ # -------------------------
397
+ # 5b. SAVE COMPRESSED IMAGE
398
+ # -------------------------
399
+ compressed_bytes = compress_pil_image_to_2mb(
400
+ pil_output,
401
+ max_dim=1280
402
+ )
403
+
404
+ compressed_id = fs.put(
405
+ compressed_bytes,
406
+ filename=f"result_{input1_id}_compressed.jpg",
407
+ contentType="image/jpeg",
408
+ metadata={
409
+ "role": "output_compressed",
410
+ "original_output_id": str(out_id),
411
+ "prompt": prompt,
412
+ "user_email": user.get("email")
413
+ }
414
+ )
415
+
416
 
417
  response_time_ms = round((time.time() - start_time) * 1000)
418
 
 
433
  return JSONResponse({
434
  "output_image_id": str(out_id),
435
  "user": user.get("email"),
436
+ "response_time_ms": response_time_ms,
437
+ "Compressed_Image_URL": (
438
+ f"https://logicgoinfotechspaces-polaroidimage.hf.space/image/{compressed_id}"
439
+ )
440
  })
441
  ####################---------------------------------------------------------------------------------------------------###
442
  ###-----OLD CODE--------------####