module testing.dummy_applications
¶
Short summary¶
module lightmlrestapi.testing.dummy_applications
Machine Learning Post request
Functions¶
function |
truncated documentation |
---|---|
Computes the distance between two images. The function uses Pillow. |
|
Calls |
|
Defines a dummy application using this API. It returns a score produced by a model trained on Iris datasets … |
|
Defines a dummy application using this API including authentification. It returns a score produced by a model trained … |
|
Defines an application as defined in the tutorial Create your first REST API. |
|
Defines a dummy application using this API and processing one image. The API ingests an image, resizes it to 224x224 … |
|
Defines a dummy application using this API. It returns a list of neighbors with a score on Iris datasets. … |
|
Defines a dummy application using this API. It returns a list of one neighbor for an image and metadata (random). … |
|
Defines a dummy application using this API. It stores a model and it returns a score produced by a model trained … |
Documentation¶
Machine Learning Post request
- lightmlrestapi.testing.dummy_applications._distance_img(img1, img2, arr1=None)¶
Computes the distance between two images. The function uses Pillow.
- Parameters:
img1 – reference PIL.Image.Image
img2 – new image PIL.Image.Image
arr1 – img1 as an array if available (to avoid converting the same image multiple times)
- Returns:
distance (in [0, 1]) or list of distances
- lightmlrestapi.testing.dummy_applications._distance_img_b64(img1, img2, arr1=None)¶
Calls
_distance_img
on an image encoded with base64.- Parameters:
img1 – reference PIL.Image.Image
img2 – new image or list of images encoded with base64
arr1 – img1 as an array if available (to avoid converting the same image multiple times)
- Returns:
distance (in [0, 1]) or list of distances
- lightmlrestapi.testing.dummy_applications.dummy_application(app=None, **params)¶
Defines a dummy application using this API. It returns a score produced by a model trained on Iris datasets and two features.
- Parameters:
app – application, if None, creates one
params – parameters sent to
MachineLearningPost
- Returns:
app
Query a REST API with features
This example shows how to query a REST API by sending a vector of features. You can start it by running:
start_mlrestapi --name=dummy
And then query it with:
import requests import ujson features = ujson.dumps({'X': [0.1, 0.2]}) r = requests.post('http://127.0.0.1:8081', data=features) print(r) print(r.json())
It should return:
{'Y': [[0.4994216179, 0.4514893599, 0.0490890222]]}
- lightmlrestapi.testing.dummy_applications.dummy_application_auth(app=None, algo='sha224', **params)¶
Defines a dummy application using this API including authentification. It returns a score produced by a model trained on Iris datasets and two features.
- Parameters:
app – application, if None, creates one
algo – algorithm used to encrypt the passwords
params – parameters sent to
MachineLearningPost
- Returns:
app
It adds one users with the login ‘me’ and the password ‘dummy’.
- lightmlrestapi.testing.dummy_applications.dummy_application_fct(restapi_load, restapi_predict, users=None, algo='sha224', app=None, **params)¶
Defines an application as defined in the tutorial Create your first REST API.
- Parameters:
restapi_load – function to load a model
restapi_predict – predict function
params – parameters sent to
MachineLearningPost
users – restrict to authenticated users,
load_passwords
algo – algorithm used to encrypt password
app – application, if None, creates one
- lightmlrestapi.testing.dummy_applications.dummy_application_image(app=None, options=None, **params)¶
Defines a dummy application using this API and processing one image. The API ingests an image, resizes it to 224x224 and returns a distance to an original image from subfolder data.
- Parameters:
app – application, if None, creates one
options – if not empty, path to an image
params – parameters sent to
MachineLearningPost
- Returns:
app
Query a REST API with an image
This example shows how to query a REST API by sending an image. You can start it by running:
start_mlrestapi --name=dummyimg
And then query it with:
import requests import ujson from lightmlrestapi.args import image2base64 img = "path_to_image" b64 = image2base64(img)[1] features = ujson.dumps({'X': b64}, reject_bytes=False) r = requests.post('http://127.0.0.1:8081', data=features) print(r) print(r.json())
It should return something like:
{'distance': [0.21]}
- lightmlrestapi.testing.dummy_applications.dummy_application_neighbors(app=None, **params)¶
Defines a dummy application using this API. It returns a list of neighbors with a score on Iris datasets.
- Parameters:
app – application, if None, creates one
params – parameters sent to
MachineLearningPost
- Returns:
app
Query a REST API with an image and get neighbors
A previous example shows how to send an image, this one illustrates a result which is a classifier result neither a regressor one but neighbors. You can start it by running:
start_mlrestapi --name=dummyknn
And then query it with:
import requests import ujson features = ujson.dumps({'X': [0.1, 0.2]}) r = requests.post('http://127.0.0.1:8081', data=features) print(r) print(r.json())
It should return:
{'Y': [[[41, 4.8754486973], [13, 5.0477717856], [8, 5.0774009099], [38, 5.1312766443], [60, 5.2201532545]]]}
- lightmlrestapi.testing.dummy_applications.dummy_application_neighbors_image(app=None, options=None, **params)¶
Defines a dummy application using this API. It returns a list of one neighbor for an image and metadata (random).
- Parameters:
app – application, if None, creates one
options – if not empty, path to an image
params – parameters sent to
MachineLearningPost
- Returns:
app
You can start it by running:
start_mlrestapi --name=dummyknnimg
And then query it with:
import requests import ujson from lightmlrestapi.args import image2base64 img = "path_to_image" b64 = image2base64(img)[1] features = ujson.dumps({'X': b64}, reject_bytes=False) r = requests.post('http://127.0.0.1:8081', data=features) print(r) print(r.json())
It should return:
{'Y': [[[41, 4.8754486973, {'name': 'wiki.png', description='something'}]]]}
- lightmlrestapi.testing.dummy_applications.dummy_mlstorage(app=None, **params)¶
Defines a dummy application using this API. It stores a model and it returns a score produced by a model trained on Iris datasets and two features.
- Parameters:
app – application, if None, creates one
params – parameters sent to
MLStoragePost
- Returns:
app