Spaces:
Sleeping
Sleeping
File size: 2,761 Bytes
07793cb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | """
services/codemix/model_codemix.py
ββββββββββββββββββββββββββββββββββ
Service layer for the CodeMix model (text classification).
This module is responsible for:
- Building the HTTP request to the Hugging Face Inference API
- Attaching the correct Bearer token for the CodeMix model
- Returning a clean result dict (or raising a descriptive exception)
We use `httpx.AsyncClient` so the server can handle other requests while
waiting for the (potentially slow) HF API to respond.
"""
import logging
import httpx
from app.utils.config import settings
logger = logging.getLogger(__name__)
# How long (seconds) to wait for HF to respond before giving up.
REQUEST_TIMEOUT = 30.0
async def model_codemix_service(payload: dict) -> dict:
"""
Call Hugging Face CodeMix model with *payload* and return the parsed JSON response.
Args:
payload: A dict that will be serialised to JSON and sent to the HF API.
For text models this is typically {"inputs": "<your text>"}.
Returns:
A dict with the key "result" containing the raw HF API response.
Raises:
httpx.HTTPStatusError: When HF returns a 4xx / 5xx response.
RuntimeError: On connection errors or timeouts.
"""
url = settings.hf_codemix_url
token = settings.hf_codemix_token
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
}
logger.info("model_codemix_service β calling %s", url)
try:
async with httpx.AsyncClient(timeout=REQUEST_TIMEOUT) as client:
response = await client.post(url, json=payload, headers=headers)
response.raise_for_status() # raises HTTPStatusError on 4xx/5xx
except httpx.TimeoutException as exc:
logger.error("model_codemix_service β request timed out: %s", exc)
raise RuntimeError("CodeMix request timed out. Please try again.") from exc
except httpx.HTTPStatusError as exc:
logger.error(
"model_codemix_service β HF returned %s: %s",
exc.response.status_code,
exc.response.text,
)
raise # re-raise so the route layer can map it to an HTTP 502
except httpx.RequestError as exc:
logger.error("model_codemix_service β connection error: %s", exc)
raise RuntimeError(f"Could not reach CodeMix endpoint: {exc}") from exc
result = response.json()
logger.info("model_codemix_service β success, response keys: %s", list(result) if isinstance(result, dict) else type(result).__name__)
return {"result": result}
|