pythondockerfastapifacebook-prophet

FastAPI app running locally but not in Docker container


I have a FastAPI app that is working as expected when running locally, however, I get an 'Internal Server Error' when I try to run in a Docker container. Here's the code for my app:

from fastapi import FastAPI
from pydantic import BaseModel
import pandas as pd
from fbprophet import Prophet

class Data(BaseModel):
    length: int
    ds: list
    y: list
    model: str
    changepoint: float = 0.5
    daily: bool = False
    weekly: bool = False
    annual: bool = False
    upper: float = None
    lower: float = 0.0
    national_holidays: str = None

app = FastAPI()

@app.post("/predict/")
async def create_item(data: Data):

    # Create df from base model
    df = pd.DataFrame(list(zip(data.ds, data.y)), columns =['ds', 'y'])

    # Add the cap and floor to df for logistic model
    if data.model == "logistic":
        df['y'] = 10 - df['y']
        df['cap'] = data.upper
        df['floor'] = data.lower

    # make basic prediction
    m = Prophet(growth=data.model,
                changepoint_prior_scale=data.changepoint,
                weekly_seasonality=data.weekly,
                daily_seasonality=data.daily,
                yearly_seasonality=data.annual
                )

    # Add national holidays
    if data.national_holidays is not None:
        m.add_country_holidays(country_name=data.national_holidays)

    # Fit data frame
    m.fit(df)

    # Create data frame for future
    future = m.make_future_dataframe(periods=data.length)

    # Add the cap and floor to future for logistic model
    if data.model == "logistic":
        future['cap'] = 6
        future['floor'] = 1.5

    # forecast
    forecast = m.predict(future)

    # Print values
    print(list(forecast[['ds']].values))

    # Return results
    # {'ds': forecast[['ds']], 'yhat': forecast[['yhat']], 'yhat_lower': forecast[['yhat_lower']], 'yhat_upper': forecast[['yhat_upper']] }
    return [forecast[['ds']], forecast[['yhat']], forecast[['yhat_lower']], forecast[['yhat_upper']]]

Which is working locally with uvicorn main:app, but not when I build using this Dockerfile:

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
COPY ./app /app
RUN pip install -r requirements.txt

and start with

docker run -d --name mycontainer -p 8000:80 myimage

I'm seeing Internal Server Error in Postman. Is there something wrong with my dockerfile or docker commands? Or else how do I debug this?


Solution

  • Run the docker without the -d parameter and you'll get more clues about it. If I were to guess, I might say that you're missing some python requirement.