Created
November 16, 2023 07:14
-
-
Save zoltanctoth/fc22951c4e4966d2e25872777460a475 to your computer and use it in GitHub Desktop.
Simplest MLflow serving with FastAPI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import mlflow | |
import uvicorn | |
from fastapi import FastAPI, HTTPException | |
class MLService: | |
def __init__(self, model): | |
self.model = model | |
self.app = FastAPI() | |
@self.app.post("/invocations/") | |
async def invocations(payload: dict): | |
try: | |
return int(self.model.predict(payload)) | |
except Exception as e: | |
raise HTTPException( | |
status_code=400, | |
detail=str(e), | |
) | |
if __name__ == "__main__": | |
pyfunc_model = mlflow.pyfunc.load_model(os.environ["MODEL_PATH"]) | |
ml_service = MLService(pyfunc_model) | |
uvicorn.run(ml_service.app, host="0.0.0.0", port=5000, log_level="info") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment