-
-
Notifications
You must be signed in to change notification settings - Fork 53
Falsk Part
Mahmoud Mabrok Fouad edited this page Apr 20, 2020
·
1 revision
Flask is most used framework for making API for machine learning
and deep learning
applications.
To create API we need two parts
- Wrap the model: code that deal with model and return response.
-
Building the app: This is where we
communicate with the client
and build an actual API withFlask
.
To deploy api to be accessed using end users we use Heroku that need :-
- Procfile :- configuration file.
- main:- code of api, define routes and its functions.
- requirements.txt:- contains list of packages needed in code.
- model.pkl :- serialized pre-trained model.
from flask import Flask , request , jsonify
from sklearn.externals import joblib
import numpy as np
app = Flask(__name__)
# main path (root )
@app.route("/")
def hello():
return "Hello Every One To Qurany App"
@app.route('/uploadfile',methods=['GET','POST'])
def uploadfile():
// do logic here, load model, predict values.
return "Result"
if __name__ == '__main__':
app.run(debug = True , port= 5874)
it consists from
-
import
statments. - creating
instance
from flask app. - define end points using
@app.route(path)
.
-
First define end point (route) using
@app.route(path)
-
add to route definition ttype of requests such as
GET
,POST
, ..etc. -
create function and return response.
-
receive data from client:
- Form-url encoded :-
request.form.get("file")
- raw json:-
request.get_josn["file"]
- files :-
file = request.files['file']
-
return response as json using jsonfy by using
return jsonify( result = str(file.filename))