r/FastAPI • • Nov 16 '24

Question ML model deployment

15 Upvotes

Hello everybody! I am a Software Engineer doing a personal project in which to implement a number of CI/CD and MLOps techniques.

Every week new data is obtained and a new model is published in MLFlow. Currently that model is very simple (a linear regressor and a one hot encoder in pickle, few KBs), and I make it 4available in a FastAPI app.

Right now, when I start the server (main.py) I do this:

classifier.model = mlflow.sklearn.load_model(

“models:/oracle-model-production/latest”

)

With this I load it in an object that is accessible thanks to a classifier.py file that contains at the beginning this

classifier = None

ohe = None

I understand that this solution leaves the model loaded in memory and allows that when a request arrives, the backend only needs to make the inference. I would like to ask you a few brief questions:

  1. Is there a standard design pattern for this?

  2. With my current implementation, How can I refresh the model that is loaded in memory in the backend once a week? (I would need to refresh the whole server, or should I define some CRON in order tu reload it, which is better)

  3. If a follow an implementation like this, where a service is created and model is called with Depends, is it loading the model everytime a request is done? When is this better?

class PredictionService:
def __init__(self):
self.model = joblib.load(settings.MODEL_PATH)

def predict(self, input_data: PredictionInput):
df = pd.DataFrame([input_data.features])
return self.model.predict(df)

.post("/predict")
async def predict(input_data: PredictionInput, service: PredictionService = Depends()):

4. If my model were a very large neural network, I understand that such an implementation would not make sense. If I don't want to use any services that auto-deploy the model and make its inference available, like MLFlow or Sagemaker, what alternatives are there?

Thanks, you guys are great!


r/FastAPI • • Oct 08 '24

Hosting and deployment HTTPS with FastAPI - could idea this work?

15 Upvotes

I am reorganizing our app with now FastAPI as backend. I have it running in a container on our server, currently only in HTTP mode, port 8000.

I need to enable HTTPS for it.

My idea. I am using the same production server as for our old version and will keep it running until it is phased out. The old version has HTTP and HTTPS running through a Apache instance. Now I am thinking to create a `https://fastapi.myapp.com` subdomain that routes to Apache 443. Apache in turn forwards that subdomain to the new fastapi container running on port 8000.

Valid solution here? Double checking the idea before I commit to it.

Are there more elegant / better approaches how to implement HTTPS with FastAPI? I do not like having Apache running forever since it eats up resources + is another process that needs maintenance, upgrades, possible security risk.
Thanks!


r/FastAPI • • Oct 06 '24

Question How do I get started with Open Source projects?

15 Upvotes

Hi everyone, I just got my first backend job. I’m working on a fastapi project rn, but I’d love to get started with some fastapi open source projects to become a better backend engineer! Does anyone have any advice on how to get started with open source projects? Thanks in advance :)


r/FastAPI • • Jul 02 '24

Question Need help with FastAPI

15 Upvotes

Hello,

I just started learning FastAPI because I picked a school project to build an app using FastAPI and MongoDB, back and front combined. Can someone provide me with a good course, like implementing authorization using JWT tokens and CRUD in fastapi and mongo, or some example projects that are built on those technologies. Anything would be helpful.

Thanks!


r/FastAPI • • Oct 25 '23

Question Comparison with LiteStar and Sanic?

15 Upvotes

Does anyone have experience of developing apps in any of the "competing" ASGI frameworks?

.FastAPILiteStarSanic
Websitehttps://fastapi.tiangolo.com/https://litestar.dev/https://sanic.dev/en/
Significant Contributors (50+ commits)157
Open Issues296874
Open PRs5311529
First Release201820212016
Current version0.104.02.2.123.6.0
Github Stars63.8k3.2k17.4k
Used By248k0.2k*13.5k

I'm thinking of starting a new project in FastAPI, but the small bus-factor really scares me.

At the same time, FastAPI is crealy the most popular ASGI framework for python.


r/FastAPI • • May 23 '23

pip package Propan 0.1.2 - new way to interact Kafka from Python

15 Upvotes

A couple of days ago I wrote about the release of my framework for working with various message brokers - Propan!

Your feedback really inspired me and show that people are interested in such a tool: therefore, I took up work with renewed energy!

So, I am glad to present you the result of these labors -Propan 0.1.2 release with Kafka supporting. Now you can work with Kafka without any difficulty:

from propan import PropanApp, KafkaBroker

broker = KafkaBroker("localhost:9092")
app = PropanApp(broker)

@broker.handle("test-topic", auto_offset_reset="earliest")
async def hello(msg: str):
    print(msg)

Currently KafkaBroker supports:

  • message consuming and publishing

  • test client working without Kafka deployment

  • KafkaRouter for use with FastAPI

from fastapi import FastAPI
from propan.fastapi import KafkaRouter

app = FastAPI()
router = KafkaRouter("localhost:9092")

@router.event("test")
async def hello(m: str):
    print(m)

app.include_router(router)

As well as all other features Propan - messages validation and serialization by pydantic, lifespans, dependency injection, CLI tool, robust application template, etc. However, RPC requests are not currently implemented.

Unfortunately, code and tests are written many times faster than documentation, so this part of the Propan functionality is not covered by it yet. I will gladly accept help from everyone in writing documentation and expanding the framework functionality.


r/FastAPI • • Oct 19 '22

feedback request Komputee - Low Code Data Apps with FastAPI - https://komputee.com/product

Post image
15 Upvotes

r/FastAPI • • May 06 '22

Tutorial The ultimate FARM stack Todo app with JWT PART I - FastAPI + MongoDB | abdadeel

Thumbnail
youtube.com
15 Upvotes

r/FastAPI • • Mar 20 '22

Tutorial GitHub - roniemartinez/real-time-charts-with-fastapi: Sample application for the blog "Creating Real-Time Charts with FastAPI"

Thumbnail
github.com
15 Upvotes

r/FastAPI • • Dec 04 '21

Tutorial Building a CRUD App with FastAPI and MongoDB

Thumbnail
testdriven.io
15 Upvotes

r/FastAPI • • Nov 15 '21

Tutorial Part 2 of my FastAPI tutorial is out ! Today's menu: Setting up a Postgres database, writing the models, and generating the migrations. Hope you enjoy it, all feedback is welcome !

Thumbnail
dev.indooroutdoor.io
15 Upvotes

r/FastAPI • • Oct 24 '21

Tutorial FastAPI JWT User Authentication

Thumbnail
youtu.be
15 Upvotes

r/FastAPI • • Jul 29 '21

Question What does Flask have that FastAPI doesn't currently have?

15 Upvotes

I would like to help FastAPI/Starlette overtake flask to become the de facto framework for microservices and REST APIs. (I think Django projects generally target a different use case so I'm less concerned with trying to gain feature parity with Django)

I'm currently creating a comparison of similar 3rd party libs from each framework by cross-referencing:

I'm wondering if any of you with flask experience ever have feelings of:

I wish FastAPI had an equivalent of flask feature/plugin 'X'

Depending on the scope of the feature I'd like to try to implement or improve the biggest gaps between FastAPI and flask. Either by contributing to an active repo or creating my own.

For context, I've been working with FastAPI/Starlette/pydantic and asyncio on/off for around 2 years.