CodexBloom - Programming Q&A Platform

OCI Data Science: scenarios When Using Custom Model Deployment with FastAPI

πŸ‘€ Views: 87 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-23
oracle-cloud-infrastructure fastapi data-science Python

I'm trying to configure I'm relatively new to this, so bear with me. This might be a silly question, but I'm trying to deploy a custom machine learning model using OCI Data Science and FastAPI, but I'm running into an scenario where the model fails to load properly during the invocation. I've packaged my model with the necessary dependencies in a Docker container, and I'm using the following code snippet for my FastAPI app: ```python from fastapi import FastAPI, HTTPException import joblib app = FastAPI() model = None @app.on_event("startup") def load_model(): global model try: model = joblib.load('model.pkl') except Exception as e: raise HTTPException(status_code=500, detail=f"Failed to load model: {str(e)}") @app.post("/predict") def predict(data: dict): global model if model is None: raise HTTPException(status_code=503, detail="Model not ready") prediction = model.predict([data['features']]) return {'prediction': prediction.tolist()} ``` I've created a deployment on OCI Data Science and defined the entry point for the FastAPI application. Everything seems set up correctly, but when I invoke the `/predict` endpoint, I'm getting a 500 Internal Server behavior with the message `Failed to load model: [Errno 2] No such file or directory: 'model.pkl'`. I've confirmed that `model.pkl` exists in the deployed container, and the file path should be correct. I've tried modifying the container's entry point, changed the working directory, and even added logging to verify the existence of the model file before loading it. Here’s the relevant part of my Dockerfile: ```dockerfile FROM oraclelinux:7-slim COPY model.pkl /app/model.pkl COPY app.py /app/app.py WORKDIR /app RUN pip install fastapi uvicorn joblib ENTRYPOINT ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"] ``` Could this be an scenario with how OCI handles file paths in the container? Or is there a specific way to refer to static files when using OCI Data Science? Any advice would be greatly appreciated! I'm using Python 3.9 in this project. What are your experiences with this? I'm developing on CentOS with Python. Cheers for any assistance! This is part of a larger application I'm building. How would you solve this?