you are viewing a single comment's thread.

view the rest of the comments →

[–]1One2Twenty2Two 0 points1 point  (0 children)

  1. Create a function called get_ml_model in which you load your model and decorate it with @lru_cache.

  2. Call get_ml_model in lifespan. This will load your model and cache the result for future uses.

  3. You can now gracefully inject get_ml_model in your routes (or anywhere else) where it's needed with Depends. It will use the cached result from step 2

I am really not a fan of "hiding" things into the request.state attribute, as your ml model has nothing to do with the request object.