I will be deploying a stock prediction model as a RESTful API using FastAPI to AWS EC2, and make it available (i.e., public) to end users.
We will use Prophet to predict stock market prices. We defined three functions here:
-
train
downloads historical stock data withyfinance
, creates a new Prophet model, fits the model to the stock data, and then serializes and saves the model as aJoblib file
. -
predict
loads and deserializes the saved model, generates a new forecast, creates images of the forecast plot and forecast components, and returns the days included in the forecast as a list of dicts. -
convert
takes the list of dicts from predict and outputs a dict of dates and forecasted values; e.g., {"07/02/2020": 200}). -
The last block of code allows you to execute the model from the command line, with two arguments, a valid stock ticker and the number of days to predict.
Build a container image for your FastAPI application.
curl \ --header "Content-Type: application/json" \ --request POST \ --data '{"ticker":"MSFT", "days":7}' \ http://54.243.8.157:8000/predict
- Adapted from Deploying and Hosting a Machine Learning Model with FastAPI and Heroku
- To learn about API in general, Postman Learning Center is a good starting point.
- Machine Learning Operations (MLOps): Overview, Definition, and Architecture (July 2022)