Soumik-404 commited on
Commit
d6a3dd3
·
1 Parent(s): b70e359
Files changed (1) hide show
  1. app/api/v1/embeddings.py +9 -1
app/api/v1/embeddings.py CHANGED
@@ -27,8 +27,13 @@ _MAX_IMAGE_BYTES = 15 * 1024 * 1024
27
 
28
 
29
  def _validate_image(raw: bytes, source: str) -> Image.Image:
 
 
 
 
30
  if len(raw) > _MAX_IMAGE_BYTES:
31
  raise ValueError(f"Image {source} exceeds 15 MB limit")
 
32
  try:
33
  img = Image.open(io.BytesIO(raw))
34
  img.load()
@@ -149,7 +154,10 @@ async def create_vision_embeddings_file(
149
  for f in files:
150
  t0 = time.perf_counter()
151
  try:
 
 
152
  raw = await f.read()
 
153
  img = await asyncio.get_running_loop().run_in_executor(_thread_pool, _validate_image, raw, f.filename or "unknown")
154
  images.append(img)
155
  except Exception as exc:
@@ -299,4 +307,4 @@ async def create_vision_embeddings_url(
299
  success_count=success_count,
300
  failed_count=failed_count,
301
  results=item_results,
302
- )
 
27
 
28
 
29
  def _validate_image(raw: bytes, source: str) -> Image.Image:
30
+ # FIX: Check if the file is completely empty (0 bytes)
31
+ if not raw:
32
+ raise ValueError(f"File {source} is empty (0 bytes).")
33
+
34
  if len(raw) > _MAX_IMAGE_BYTES:
35
  raise ValueError(f"Image {source} exceeds 15 MB limit")
36
+
37
  try:
38
  img = Image.open(io.BytesIO(raw))
39
  img.load()
 
154
  for f in files:
155
  t0 = time.perf_counter()
156
  try:
157
+ # FIX: Guarantee the file cursor is at the beginning before reading!
158
+ await f.seek(0)
159
  raw = await f.read()
160
+
161
  img = await asyncio.get_running_loop().run_in_executor(_thread_pool, _validate_image, raw, f.filename or "unknown")
162
  images.append(img)
163
  except Exception as exc:
 
307
  success_count=success_count,
308
  failed_count=failed_count,
309
  results=item_results,
310
+ )