Now you know all about algorithms and how to benchmark them. Once you found the optimal algorithm and settings, however, what do you do with them?
One common way to make your findings, that is, a number of articles most highly recommended for a given customer, accessible to those who need to know is to provide your recommendations as a software service. More specifically, your recommendations are served up by a webserver, typically in JSON format and accessed by your clients via a RESTful API.
To see how that works, we first need to take care the serving-up part. This is what happens in the file 07_RESTfulAPI.py, which is not a jupyter notebook but a regular python script. So just open it in a text editor (or an IDE) to read the ample documentation and then execute it on the command line by typing
python 07_RESTfulAPI.py
on your shell prompt.
You then have a web server running, which you can access in your browser via the URL
http://localhost:<port>
where <port> is the number displayed on the command line once the server is up. Feel free to play around with what you see there. As usual, it pays off to keep an eye on the logfile to stay ahead of things.
Now that the server is running, we still need a client that accesses it and retrieves the recommendations. Having played around with the server in our webbrowser, we know exactly what we have to do to get what we want.
In [1]:
from urllib.parse import ParseResult
from urllib.request import urlopen
import json
In [2]:
base_path = '/user/'
base_query = 'n_recos='
url_params = {'scheme': 'http',
'netloc': 'localhost:5002',
'path': base_path,
'params': '',
'query': 'n_recos=6',
'fragment': ''}
rest = ParseResult(**url_params)
In [3]:
with urlopen(rest.geturl()) as url:
users = json.loads(url.read())
print('User IDs retrieved from server')
print('==============================')
print(users)
Now that we have some customer IDs, we specify below how many recommendations we want for each one, retrieve and print them.
Note: If you want to do this again (because it is really cool, now, isnt't it), you can not simply re-run the cell below. Instead, you will have to start all over again and execute all three cells(starting below the imports, of course).
In [4]:
n_recos = 6 # Specify here how many recommendations you want for each customer!
query = base_query + str(n_recos)
rest = rest._replace(query=query)
for user_id in users.pop('some user IDs'):
path = base_path + user_id
rest = rest._replace(path=path)
with urlopen(rest.geturl()) as url:
list_of = json.loads(url.read())
print('Recommendations for user', user_id)
print('===========================')
for article in list_of['recommended articles']:
print(article)
print()
Clearly the way to go, eh? Make sure to terminate the webserver (by pressing Ctrl-C on the command line) once you are done exploring and playing.
In [ ]: