Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -4,7 +4,7 @@ import base64
|
|
| 4 |
import io
|
| 5 |
import os
|
| 6 |
import logging
|
| 7 |
-
from PIL import Image
|
| 8 |
import torch
|
| 9 |
import asyncio
|
| 10 |
from utils import (
|
|
@@ -149,7 +149,7 @@ async def process(image_input: Image.Image, box_threshold: float, iou_threshold:
|
|
| 149 |
)
|
| 150 |
except Exception as e:
|
| 151 |
logger.error(f"Error in process function: {e}")
|
| 152 |
-
raise
|
| 153 |
|
| 154 |
|
| 155 |
# Define the process_image endpoint
|
|
@@ -162,19 +162,25 @@ async def process_image(
|
|
| 162 |
try:
|
| 163 |
# Read the image file
|
| 164 |
contents = await image_file.read()
|
| 165 |
-
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
# Create a task for processing
|
| 168 |
task = asyncio.create_task(process(image_input, box_threshold, iou_threshold))
|
| 169 |
-
|
| 170 |
# Add the task to the queue
|
| 171 |
await request_queue.put(task)
|
| 172 |
logger.info(f"Task added to queue. Current queue size: {request_queue.qsize()}")
|
| 173 |
-
|
| 174 |
# Wait for the task to complete
|
| 175 |
response = await task
|
| 176 |
|
| 177 |
return response
|
|
|
|
|
|
|
| 178 |
except Exception as e:
|
| 179 |
logger.error(f"Error processing image: {e}")
|
| 180 |
-
raise HTTPException(status_code=500, detail=
|
|
|
|
| 4 |
import io
|
| 5 |
import os
|
| 6 |
import logging
|
| 7 |
+
from PIL import Image, UnidentifiedImageError
|
| 8 |
import torch
|
| 9 |
import asyncio
|
| 10 |
from utils import (
|
|
|
|
| 149 |
)
|
| 150 |
except Exception as e:
|
| 151 |
logger.error(f"Error in process function: {e}")
|
| 152 |
+
raise HTTPException(status_code=500, detail=f"Failed to process the image: {e}")
|
| 153 |
|
| 154 |
|
| 155 |
# Define the process_image endpoint
|
|
|
|
| 162 |
try:
|
| 163 |
# Read the image file
|
| 164 |
contents = await image_file.read()
|
| 165 |
+
try:
|
| 166 |
+
image_input = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 167 |
+
except UnidentifiedImageError as e:
|
| 168 |
+
logger.error(f"Unsupported image format: {e}")
|
| 169 |
+
raise HTTPException(status_code=400, detail="Unsupported image format.")
|
| 170 |
+
|
| 171 |
# Create a task for processing
|
| 172 |
task = asyncio.create_task(process(image_input, box_threshold, iou_threshold))
|
| 173 |
+
|
| 174 |
# Add the task to the queue
|
| 175 |
await request_queue.put(task)
|
| 176 |
logger.info(f"Task added to queue. Current queue size: {request_queue.qsize()}")
|
| 177 |
+
|
| 178 |
# Wait for the task to complete
|
| 179 |
response = await task
|
| 180 |
|
| 181 |
return response
|
| 182 |
+
except HTTPException as he:
|
| 183 |
+
raise he
|
| 184 |
except Exception as e:
|
| 185 |
logger.error(f"Error processing image: {e}")
|
| 186 |
+
raise HTTPException(status_code=500, detail=f"Internal server error: {e}")
|