|
1 | 1 | from fastapi import APIRouter, HTTPException, UploadFile, File, Query, Depends, Body |
2 | 2 | from typing import List, Optional, Dict, Any |
| 3 | +import asyncio |
3 | 4 | import io |
4 | 5 | import base64 |
5 | 6 | from PIL import Image |
@@ -95,10 +96,13 @@ async def detect_objects( |
95 | 96 | threshold = confidence_threshold or default_threshold |
96 | 97 | logger.debug(f"Using confidence threshold: {threshold}") |
97 | 98 |
|
98 | | - # Perform detection |
| 99 | + # Perform detection (run in thread pool to avoid blocking the event loop |
| 100 | + # so health probes can still respond during long-running inference). |
99 | 101 | start_time = time.time() |
100 | 102 | try: |
101 | | - detections = detector.detect(processed_image, confidence_threshold=threshold) |
| 103 | + detections = await asyncio.to_thread( |
| 104 | + detector.detect, processed_image, confidence_threshold=threshold |
| 105 | + ) |
102 | 106 | detection_time = time.time() - start_time |
103 | 107 | logger.info(f"Detection completed: {len(detections)} objects found in {detection_time*1000:.1f}ms") |
104 | 108 |
|
@@ -228,7 +232,9 @@ async def describe_image( |
228 | 232 |
|
229 | 233 | start_time = time.time() |
230 | 234 | try: |
231 | | - description = detector.describe(processed_image, length=length) |
| 235 | + description = await asyncio.to_thread( |
| 236 | + detector.describe, processed_image, length=length |
| 237 | + ) |
232 | 238 | process_time = time.time() - start_time |
233 | 239 | logger.info(f"Describe completed in {process_time*1000:.1f}ms") |
234 | 240 | logger.info(f"Description result: {description}") |
@@ -291,7 +297,9 @@ async def query_image( |
291 | 297 |
|
292 | 298 | start_time = time.time() |
293 | 299 | try: |
294 | | - answer = detector.query(processed_image, question=question) |
| 300 | + answer = await asyncio.to_thread( |
| 301 | + detector.query, processed_image, question=question |
| 302 | + ) |
295 | 303 | process_time = time.time() - start_time |
296 | 304 | logger.info(f"Query completed in {process_time*1000:.1f}ms") |
297 | 305 | logger.info(f"Query answer: {answer}") |
|
0 commit comments