update the cv modal inference proxy server with optimization
This commit is contained in:
@@ -3,16 +3,34 @@ import json
|
||||
from typing import Any
|
||||
import numpy as np
|
||||
import requests
|
||||
from requests.adapters import HTTPAdapter
|
||||
from urllib3.util.retry import Retry
|
||||
|
||||
|
||||
class TritonAdapter:
|
||||
def __init__(self, endpoint_url: str, timeout: float = 60.0):
|
||||
self.endpoint_url = endpoint_url.rstrip("/")
|
||||
self.timeout = timeout
|
||||
self._session = self._build_session()
|
||||
|
||||
@staticmethod
|
||||
def _build_session() -> requests.Session:
|
||||
session = requests.Session()
|
||||
retry = Retry(
|
||||
total=0,
|
||||
connect=0,
|
||||
read=0,
|
||||
redirect=0,
|
||||
status=0,
|
||||
raise_on_status=False,
|
||||
)
|
||||
adapter = HTTPAdapter(max_retries=retry, pool_connections=20, pool_maxsize=50)
|
||||
session.mount("http://", adapter)
|
||||
session.mount("https://", adapter)
|
||||
return session
|
||||
|
||||
async def close(self):
|
||||
pass
|
||||
await asyncio.to_thread(self._session.close)
|
||||
|
||||
|
||||
async def infer(
|
||||
@@ -61,7 +79,7 @@ class TritonAdapter:
|
||||
}
|
||||
|
||||
url = f"{self.endpoint_url}/v2/models/{model_name}/infer"
|
||||
response = requests.post(url, data=body, headers=headers, timeout=self.timeout)
|
||||
response = self._session.post(url, data=body, headers=headers, timeout=self.timeout)
|
||||
response.raise_for_status()
|
||||
return self._parse_binary_response(response.headers, response.content)
|
||||
|
||||
@@ -91,7 +109,7 @@ class TritonAdapter:
|
||||
|
||||
def _model_ready_sync(self, model_name: str) -> bool:
|
||||
url = f"{self.endpoint_url}/v2/models/{model_name}"
|
||||
response = requests.get(url, timeout=self.timeout)
|
||||
response = self._session.get(url, timeout=self.timeout)
|
||||
if response.status_code == 404:
|
||||
return False
|
||||
response.raise_for_status()
|
||||
@@ -113,7 +131,7 @@ class TritonAdapter:
|
||||
url = f"{self.endpoint_url}/v2/repository/index"
|
||||
|
||||
# 2. Change requests.get to requests.post with an empty json payload {}
|
||||
response = requests.post(url, json={}, timeout=self.timeout)
|
||||
response = self._session.post(url, json={}, timeout=self.timeout)
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import asyncio
|
||||
import base64
|
||||
import io
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from PIL import Image
|
||||
|
||||
from backend.implementation.postprocessing.calibration import calibration_config_from_params
|
||||
from backend.services.cv_inference_service import CvInferenceOptions, run_single
|
||||
from backend.services.celery_app import celery_app
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _decode_base64_to_pil(b64_str: str) -> Image.Image:
|
||||
raw = base64.b64decode(b64_str)
|
||||
return Image.open(io.BytesIO(raw)).convert("RGB")
|
||||
|
||||
|
||||
@celery_app.task(bind=True, name="cv_inference.run_chunk", max_retries=2, default_retry_delay=10)
|
||||
def run_cv_chunk(self, chunk_payload: dict) -> list[dict]:
|
||||
images_b64 = chunk_payload.get("images_b64", [])
|
||||
frame_ids = chunk_payload.get("frame_ids", [])
|
||||
calibration_params = chunk_payload.get("calibration", {})
|
||||
model_versions = chunk_payload.get("model_versions")
|
||||
|
||||
if not images_b64:
|
||||
return []
|
||||
|
||||
images = [_decode_base64_to_pil(b64) for b64 in images_b64]
|
||||
calibration = calibration_config_from_params(calibration_params)
|
||||
options = CvInferenceOptions(
|
||||
calibration=calibration,
|
||||
model_versions=model_versions,
|
||||
use_cache=False,
|
||||
)
|
||||
|
||||
async def _run():
|
||||
return await asyncio.gather(*[
|
||||
run_single(img, frame_id=fid, options=options)
|
||||
for img, fid in zip(images, frame_ids)
|
||||
])
|
||||
|
||||
try:
|
||||
results = asyncio.run(_run())
|
||||
return results
|
||||
except Exception as exc:
|
||||
logger.exception("Celery chunk task failed: %s", exc)
|
||||
raise self.retry(exc=exc, countdown=10, max_retries=2)
|
||||
Reference in New Issue
Block a user