saskarkhadka's picture
restructure and add telemtry
9766e10
Raw History Blame Contribute Delete
1.62 kB
from fastapi import FastAPI, HTTPException
import uvicorn
from app.middleware.auth import auth_middleware
from app.middleware.request_tag import request_id_middleware
from starlette.exceptions import HTTPException
from fastapi.exceptions import RequestValidationError
from app.core.exception import (
http_exception_handler,
request_validation_error_handler,
global_exception_handler
)
from app.models.models import ClassificationRequest, ClassificationResponse
from app.inference.inference import inference_service
from app.core.config import settings
from app.core.logging import setup_logging
from app.router.router import api_router
import nltk
nltk.download('stopwords')
setup_logging()
app = FastAPI()
app.add_exception_handler(HTTPException, http_exception_handler)
app.add_exception_handler(RequestValidationError, request_validation_error_handler)
app.add_exception_handler(Exception, global_exception_handler)
# app.middleware("http")(auth_middleware)
app.middleware("http")(request_id_middleware)
@app.get('/')
def home():
return {'message': 'pong'}
@app.get('/err')
def err():
return ClassificationResponse()
@app.post('/classify/nepali-news', status_code=200, response_model=ClassificationResponse)
def classify_nepali_news(body: ClassificationRequest) -> ClassificationResponse:
result: ClassificationResponse = inference_service.run_inference(
model=body.model,
text=body.text,
)
return result
app.include_router(api_router, prefix='/api')
if __name__ == "__main__":
port = int(settings.PORT)
uvicorn.run(app, host="0.0.0.0", port=port)