This is an example of how you can use the web API from Python.
To test this
$ python -m dffml.util.testing.consoletest docs/python.rst
First start the server
Warning
By no means should you use the following command in a production environment! You should instead see the security
docs!
$ dffml service http server -insecure -cors '*' -addr 0.0.0.0 -port 8080
Configure a model
configure.py
import sys
import json
import urllib.request
# The URL that the HTTP service is running on
# http://127.0.0.1:8080/
LABEL = sys.argv[-1]
MODEL = sys.argv[-2]
ENDPOINT = sys.argv[-3]
print(urllib.request.urlopen(
f"{ENDPOINT}/configure/model/{MODEL}/{LABEL}",
data=json.dumps(
{
"model": {
"plugin": None,
"config": {
"directory": {
"plugin": [
"mymodel"
],
"config": {}
},
"predict": {
"plugin": "Y:int:1",
"config": {}
},
"features": {
"plugin": [
"X:int:1",
],
"config": {}
}
}
}
}
).encode()
).read())
$ python configure.py http://localhost:8080 slr mymodel
Create a context for a model
context.py
import sys
import json
import urllib.request
# The URL that the HTTP service is running on
# http://127.0.0.1:8080/
CONTEXT = sys.argv[-1]
MODEL = sys.argv[-2]
ENDPOINT = sys.argv[-3]
print(urllib.request.urlopen(
f"{ENDPOINT}/context/model/{MODEL}/{CONTEXT}",
).read())
$ python context.py http://localhost:8080 mymodel mymodelcontext
print("Hello World")
*Note*: On successful creation and configuration the server will return {"error": null}
## Context Creation
URL = "https://127.0.0.1:5000/context/mdoel/{label}/{ctx_label}".format(label = "mymodel", ctx_label = "ctx_mymodel")
result = requests.get(url = URL, params = {})
*Note*: On successful creation of a context the server will return {"error": null}
## Train the Model
URL = "https://127.0.0.1:5000/model/{ctx_label}/train".format(ctx_label = "ctx_mymodel")
params = {
[
"my_training_dataset"
]
}
result = requests.post(url = URL, params = PARAMS)
*Note*: On successful execution the server will return {"error": null}
## Assess Accuracy
URL = "https://127.0.0.1:5000/model/{ctx_label}/accuracy".format(ctx_label = "ctx_mymodel")
params = {
[
"my_test_dataset"
]
}
result = requests.post(url = URL, params = PARAMS)
*Note*: On successful execution the response will be a JSON object containing the accuracy as a float value : {"accuracy": 0.42}
## Make Prediction
URL = "https://127.0.0.1:5000/model/{ctx_label}/predict/0".format(ctx_label = "ctx_mymodel")
PARAMS = {
{
"42": {
"features": {
"by_ten": 420
}
}
}
}
*Note*: The JSON passed as param maps key of the record to the JSON representation of dffml.record.Record as received by the source record endpoint
result = requests.post(url = URL, params = PARAMS)
*Note*: On successful execution the response will be a JSON object similar to this:
response = {
"iterkey": null,
"records": {
"42": {
"key": "42",
"features": {
"by_ten": 420
},
"prediction": {
"confidence": 42,
"value": 4200
},
"last_updated": "2019-10-15T08:19:41Z",
"extra": {}
}
}
}