saskarkhadka's picture
restructure and add telemtry
9766e10
Raw History Blame Contribute Delete
2.71 kB
from app.models.models import ClassificationModel, MLModel, InferenceResult
from app.inference.mbert import MBert
from app.inference.mlp import MLP
from app.inference.bilistm import BiLstm
from app.inference.ml import MachineLearning
from app.core.context import request_id_ctx
import logging
import time
logger = logging.getLogger(__name__)
class InferenceService:
def __init__(self):
self.models: dict[
ClassificationModel,
MBert | MLP | BiLstm | MachineLearning
] = {
ClassificationModel.bert: MBert(),
ClassificationModel.bilstm: BiLstm(),
ClassificationModel.mlp: MLP(),
ClassificationModel.naivebayes: MachineLearning(model=MLModel.naivebayes),
ClassificationModel.gradientboost: MachineLearning(model=MLModel.gradientboost),
ClassificationModel.xgboost: MachineLearning(model=MLModel.xgboost),
}
def run_inference(
self,
model: ClassificationModel,
text: str,
) -> str:
request_id = request_id_ctx.get('not-set')
logger.info(
"starting_inference_run",
extra={
"request_id": request_id,
"model": model,
"text_length": len(text.split()),
}
)
start_time = time.perf_counter()
instance = self.models.get(model)
if instance is None:
logger.error(
"inference_model_not_found",
extra={
"request_id": request_id,
"model": model,
}
)
raise ValueError(
f"Unknown model: {model}"
)
try:
prediction: str = instance.predict(text=text)
logger.info(
"inference_completed_successfully",
extra={
"request_id": request_id,
"model": model,
"duration": round((time.perf_counter() - start_time) * 1000),
"words_processed": min(instance.MAX_SEQUENCE_LENGTH, len(text.split()))
}
)
return InferenceResult(
prediction = prediction
)
except Exception as e:
logger.info(
"inference_failed",
extra={
"request_id": request_id,
"model": model,
"duration": round((time.perf_counter() - start_time) * 1000),
"error": str(e),
}
)
raise e
inference_service = InferenceService()