Skip to content
This repository was archived by the owner on Nov 23, 2023. It is now read-only.

Commit 4335715

Browse files
committed
Initial commit
0 parents  commit 4335715

File tree

14 files changed

+12232
-0
lines changed

14 files changed

+12232
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
### Python ###
2+
# Byte-compiled / optimized / DLL files
3+
__pycache__/
4+
*.py[cod]
5+
*$py.class

README.md

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
# Flask-based Model API
2+
#### EXPLORE Data Science Academy Regression Predict
3+
4+
### Table of Contents
5+
6+
- [Flask-based Model API](#flask-based-model-api)
7+
- [EXPLORE Data Science Academy Regression Predict](#explore-data-science-academy-regression-predict)
8+
- [Table of Contents](#table-of-contents)
9+
- [1) Overview](#1-overview)
10+
- [1.1) Wait, what is an API again?](#11-wait-what-is-an-api-again)
11+
- [1.2) How our API will work](#12-how-our-api-will-work)
12+
- [Description of files](#description-of-files)
13+
- [2) Usage Instructions](#2-usage-instructions)
14+
- [2.1) Creating a copy of this repo](#21-creating-a-copy-of-this-repo)
15+
- [2.2) Running the API on your local machine](#22-running-the-api-on-your-local-machine)
16+
- [2.3) Updating the API to use your own model](#23-updating-the-api-to-use-your-own-model)
17+
- [Prerequisites](#prerequisites)
18+
- [Making the changes](#making-the-changes)
19+
- [2.4) Running the API on a remote AWS EC2 instance](#24-running-the-api-on-a-remote-aws-ec2-instance)
20+
- [3) FAQ](#3-faq)
21+
22+
## 1) Overview
23+
24+
This repository forms the basis of *Task 2* for the **Regression Predict** within EDSA's Data Science course. It hosts template code which will enable students to deploy their own developed models through a web server-based API.
25+
26+
#### 1.1) Wait, what is an API again?
27+
28+
[![What is an API](assets/imgs/What_is_an_API.png)](https://youtu.be/s7wmiS2mSXY)
29+
30+
31+
An API - or Application Programming Interface - refers to a set of procedures and protocols that allows us to send and request information between ourselves and remote applications. You can think of this as a channel of communication to a remote server using specific commands that enable you to use their applications without needing to host that functionality yourself. Many types of APIs exist, but for this predict task we are interested specifically in Web APIs. These allow us to send and receive information using web development languages, such as HTML and JSON. The video above provides a simple and intuitive explanation of how APIs operate.
32+
33+
#### 1.2) How our API will work
34+
35+
<p align='center'>
36+
<img src="assets/imgs/API.png"
37+
alt='Flask Web-server'
38+
width=1000px/>
39+
<br>
40+
<em>A representation of the API framework that will be created within this predict task.</em>
41+
</p>
42+
43+
##### Description of files
44+
45+
Several files within this repository enable the correct functioning of our API. We provide a high-level description of these salient files within the table below:
46+
47+
| File Name | Description |
48+
| :--------------------- | :-------------------- |
49+
| `api.py` | Flask web server application definition and instantiation. |
50+
| `model.py` | Contains helper functions to separate model specific code from our API definition. |
51+
| `utils/request.py` | Simple script to simulate a POST request sent to our API. |
52+
| `utils/train_model.py` | Code used to train the simple model used for demonstration of the API's functioning. |
53+
54+
## 2) Usage Instructions
55+
56+
#### 2.1) Creating a copy of this repo
57+
58+
| ⚡️ WARNING ⚡️ |
59+
|:--------------------|
60+
|Do **NOT** *clone* this repository. Instead follow the instructions in this section to *fork* the repo.|
61+
62+
As described within the Predict instructions for the Regression Sprint, this code represents a *template* from which you can base your own model's API. As such, in order to modify the template to serve your own model (and the associated code changes which are required for this), you will need to **[fork](https://help.github.com/en/github/getting-started-with-github/fork-a-repo)** this repository. Failing to do this will lead to complications when trying to work on the API remotely.
63+
64+
65+
![Fork Repo](assets/imgs/fork_repo.png)
66+
67+
To fork the repo, simply ensure that you are logged into your GitHub account, and then click on the 'fork' button at the top of this page as indicated within the figure above.
68+
69+
70+
#### 2.2) Running the API on your local machine
71+
72+
As a first step to becoming familiar with our API's functioning, we recommend setting up a running instance on your own local machine.
73+
74+
To do this, follow the steps below by running the given commands within a Git bash (Windows), or terminal (Mac/Linux):
75+
76+
1. Ensure that you have the prerequisite Python libraries installed on your local machine:
77+
78+
```bash
79+
pip install -U flask numpy pandas scikit-learn
80+
```
81+
82+
2. Clone the *forked* repo to your local machine.
83+
84+
```bash
85+
git clone https://github.com/{your-account-name}/load-shortfall-regression-predict-api.git
86+
```
87+
88+
3. Navigate to the base of the cloned repo, and run the API web-server initialisation script.
89+
90+
91+
```bash
92+
cd load-shortfall-regression-predict-api/
93+
python api.py
94+
```
95+
96+
If the web server was able to initialise successfully, the following message should be displayed within your bash/terminal session:
97+
98+
```
99+
----------------------------------------
100+
Model successfully loaded
101+
----------------------------------------
102+
* Serving Flask app "api" (lazy loading)
103+
* Environment: production
104+
WARNING: This is a development server. Do not use it in a production deployment.
105+
Use a production WSGI server instead.
106+
* Debug mode: off
107+
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
108+
```
109+
110+
4. Leave the web server script running within the current bash/terminal session. Open a new session, and navigate to the `utils` subfolder of the cloned repo.
111+
112+
```
113+
cd {your/custom/path}/load-shortfall-regression-predict-api/utils/
114+
```
115+
116+
5. Run the `request.py` script located within the utils subfolder to simulate a POST request for our running API.
117+
118+
```
119+
python request.py
120+
```
121+
122+
123+
If you receive an error at this point, please ensure that the web server is still running in your original bash/terminal session. If the script ran successfully, you should receive similar output to the message shown below:
124+
125+
126+
127+
```
128+
Sending POST request to web server API at: http://127.0.0.1:5000/api_v0.1
129+
130+
Querying API with the following data:
131+
[8764, '2018-01-01 03:00:00', 4.6666666667, 'level_8', 0.0, 5.3333333333, 89.0, 78.0, 0.0, 3.6666666667000003, 0.0, 143.3333333333, 4.6666666667, 266.6666666667, 0.0, 0.6666666667, 0.0, 'sp25', 0.0, 0, 1020.3333333333001, 0.0, 0.0, 0.0, 0, 800.0, 800.3333333333001, 1026.6666666667, 800.0, nan, 282.48333333330004, 1030.3333333333, 284.15, 284.15, 721.0, 281.67333333330004, 53.6666666667, 284.15, 284.8166666667, 280.48333333330004, 284.19, 277.8166666667, 281.01, 283.48333333330004, 284.15, 281.15, 279.1933333333, 278.15]
132+
133+
Received POST response:
134+
**************************************************
135+
API prediction result: 9518.37493239743
136+
The response took: 0.010888 seconds
137+
**************************************************
138+
```
139+
140+
Congratulations! You've now officially deployed your first web server API, and have successfully received a response from it.
141+
142+
With these steps completed, we're now ready to both modify the template code to place our own model within the API, and to host this API within an AWS EC2 instance. These processes are outlined within the sections below.
143+
144+
#### 2.3) Updating the API to use your own model
145+
| ℹ️ NOTE ℹ️ |
146+
|:--------------------|
147+
|We strongly encourage you to be familiar with running the API as described in Section 2.2 before attempting to use your own model.|
148+
149+
##### Prerequisites
150+
Before you can update the API code-base to use your own custom model, you will need to have the following:
151+
152+
- Your own `sklearn` model, trained and saved as a `.pkl` file.
153+
154+
For a simple example of how to pickle your model, review the script found in `utils/train_model.py`. For further instructions, consult the *'Saving and Restoring Models in Python'* train in Athena.
155+
156+
(Note: You are not limited to use only a single model within the API. Furthermore, other `sklearn` structures which have saved parameters may be required for your model to function as well. Obviously, you are expected to handle the loading of such structures in a similar way as described within this section.)
157+
158+
- Code for the data preprocessing pipeline used to train your model.
159+
160+
This code should cover aspects such as data cleaning, feature engineering, feature selection, and feature transformations.
161+
162+
The requirement of this code is vital as your API is built to provide a standard interface for POST requests. I.e. someone asking your API to make a prediction shouldn't have to worry about what specific features your model uses internally. Instead, anyone who sends a request with the standard features within the public dataset, should expect to receive a prediction result. This design principle makes it far easier to swap out an old model for a newer one, even if ends up using radically different features.
163+
164+
##### Making the changes
165+
166+
Once you've gathered the prerequisites from the above section, making the changes to API is relatively straight forward. It involves three steps:
167+
168+
1. Place your `.pkl` file within the `assets/trained-models/` directory of the repo.
169+
170+
2. Modify the `api.py` file by changing the `path_to_model` variable to reflect the new model `.pkl` file.
171+
172+
3. Modify the `model.py` file by adding your data preprocessing code to the `_preprocess_data()` helper function.
173+
174+
If the following steps were carried out successfully, running the API should now produce a new prediction result.
175+
176+
#### 2.4) Running the API on a remote AWS EC2 instance
177+
| ℹ️ NOTE ℹ️ |
178+
|:--------------------|
179+
|You will only be able to work on this section of the API setup once you've completed the *'Introduction to Amazon AWS - Part I'* train on Athena.|
180+
181+
The following steps will enable you to run your web server API on a remote EC2 instance, allowing it to the queried by any device/application which has internet access.
182+
183+
Within these setup steps, we will be using a remote EC2 instance, which we will refer to as the ***Host***, in addition to our local machine, which we will call the ***Client***. We use these designations for convenience, and to align our terminology with that of common web server practices. In cases where commands are provided, use Git bash (Windows) or Terminal (Mac/Linux) to enter these.
184+
185+
1. Ensure that you have access to a running AWS EC2 instance with an assigned public IP address. Instructions for this process are found within the *'Introduction to Amazon AWS - Part I'* train on Athena.
186+
187+
2. Install the prerequisite python libraries on both the Host (EC2 instance), and Client (local machine):
188+
189+
```bash
190+
pip install -U flask numpy pandas scikit-learn
191+
```
192+
193+
3. Clone your copy of the API repo onto both the Host and Client machines, then navigate to the base of the cloned repo:
194+
195+
```bash
196+
git clone https://github.com/{your-account-name}/load-shortfall-regression-predict-api-template.git
197+
cd load-shortfall-regression-predict-api-template/
198+
```
199+
**[On the Host]:**
200+
201+
4. Run the API web-server initialisation script.
202+
203+
```bash
204+
python api.py
205+
```
206+
207+
If this command ran successfully, the following output should be observed on the Host:
208+
209+
```
210+
----------------------------------------
211+
Model successfully loaded
212+
----------------------------------------
213+
* Serving Flask app "api" (lazy loading)
214+
* Environment: production
215+
WARNING: This is a development server. Do not use it in a production deployment.
216+
Use a production WSGI server instead.
217+
* Debug mode: off
218+
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
219+
```
220+
221+
**[On the Client]:**
222+
223+
5. Navigate to the `utils` subdirectory within the repo.
224+
225+
```bash
226+
cd utils/
227+
```
228+
229+
6. Open the `request.py` file using your favorite text editor.
230+
231+
Change the value of the `url` variable to reflect the ***public IP address*** of the Host. (Instructions for getting the public IP address are provided within the *‘Introduction to Amazon AWS - Part I’* train on Athena.)
232+
233+
```bash
234+
url = 'http://{public-ip-address-of-remote-machine}:5000/api_v0.1'
235+
```
236+
237+
7. Once the editing is completed, close the file and run it:
238+
239+
```bash
240+
python request.py
241+
```
242+
243+
If the command ran successfully, you should see output similar to the following:
244+
245+
```
246+
Sending POST request to web server API at: http://54.229.152.221:5000/api_v0.1
247+
248+
Querying API with the following data:
249+
[8764, '2018-01-01 03:00:00', 4.6666666667, 'level_8', 0.0, 5.3333333333, 89.0, 78.0, 0.0, 3.6666666667000003, 0.0, 143.3333333333, 4.6666666667, 266.6666666667, 0.0, 0.6666666667, 0.0, 'sp25', 0.0, 0, 1020.3333333333001, 0.0, 0.0, 0.0, 0, 800.0, 800.3333333333001, 1026.6666666667, 800.0, nan, 282.48333333330004, 1030.3333333333, 284.15, 284.15, 721.0, 281.67333333330004, 53.6666666667, 284.15, 284.8166666667, 280.48333333330004, 284.19, 277.8166666667, 281.01, 283.48333333330004, 284.15, 281.15, 279.1933333333, 278.15]
250+
251+
Received POST response:
252+
**************************************************
253+
API prediction result: 9518.37493239743
254+
The response took: 0.010888 seconds
255+
**************************************************
256+
```
257+
258+
If you have completed the steps in 2.3), then the prediction result should differ from the one given above.
259+
260+
**[On the Host]**
261+
262+
You should also see an update to the web server output, indicating that it was contacted by the Client (the values of this string will differ for your output):
263+
264+
```
265+
102.165.194.240 - - [08/June/2021 07:31:31] "POST /api_v0.1 HTTP/1.1" 200 -
266+
```
267+
268+
If you are able to see these messages on both the Host and Client, then your API has successfully been deployed to the Web. Snap ⚡️!
269+
270+
## 3) FAQ
271+
272+
This section of the repo will be periodically updated to represent common questions which may arise around its use. If you detect any problems/bugs, please [create an issue](https://help.github.com/en/github/managing-your-work-on-github/creating-an-issue) and we will do our best to resolve it as quickly as possible.
273+
274+
We wish you all the best in your learning experience :rocket:
275+
276+
<p align='center'>
277+
<img src="assets/imgs/EDSA_logo.png"
278+
alt='EXPLORE Data Science Academy Logo'
279+
width=450px/>
280+
<br>
281+
</p>

api.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
3+
Simple Flask-based API for Serving an sklearn Model.
4+
5+
Author: Explore Data Science Academy.
6+
7+
Note:
8+
---------------------------------------------------------------------
9+
Please follow the instructions provided within the README.md file
10+
located within this directory for guidance on how to use this script
11+
correctly.
12+
---------------------------------------------------------------------
13+
14+
Description: This file instantiates a Flask webserver
15+
as a means to create a simple API used to deploy models trained within
16+
the sklearn framework.
17+
18+
"""
19+
20+
# API Dependencies
21+
import pickle
22+
import json
23+
import numpy as np
24+
from model import load_model, make_prediction
25+
from flask import Flask, request, jsonify
26+
27+
# Application definition
28+
app = Flask(__name__)
29+
30+
# Load our model into memory.
31+
# Please update this path to reflect your own trained model.
32+
static_model = load_model(
33+
path_to_model='assets/trained-models/load_shortfall_simple_lm_regression.pkl')
34+
35+
print ('-'*40)
36+
print ('Model successfully loaded')
37+
print ('-'*40)
38+
39+
""" You may use this section (above the app routing function) of the python script to implement
40+
any auxiliary functions required to process your model's artifacts.
41+
"""
42+
43+
44+
# Define the API's interface.
45+
# Here the 'model_prediction()' function will be called when a POST request
46+
# is sent to our interface located at:
47+
# http:{Host-machine-ip-address}:5000/api_v0.1
48+
@app.route('/api_v0.1', methods=['POST'])
49+
def model_prediction():
50+
# We retrieve the data payload of the POST request
51+
data = request.get_json(force=True)
52+
# We then preprocess our data, and use our pretrained model to make a
53+
# prediction.
54+
output = make_prediction(data, static_model)
55+
# We finally package this prediction as a JSON object to deliver a valid
56+
# response with our API.
57+
return jsonify(output)
58+
59+
# Configure Server Startup properties.
60+
# Note:
61+
# When developing your API, set `debug=True`
62+
# This will allow Flask to automatically restart itself everytime you
63+
# update your API code.
64+
if __name__ == '__main__':
65+
app.run(host='0.0.0.0', port=5000, debug=False)

assets/imgs/API.png

89.4 KB
Loading

assets/imgs/EDSA_logo.png

35.7 KB
Loading

assets/imgs/Flask_logo.png

13.1 KB
Loading

assets/imgs/What_is_an_API.png

77 KB
Loading

assets/imgs/fork_repo.png

18.8 KB
Loading
600 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)