This is the second installment of I want to create a machine learning service without programming! Text classification. Makes machine learning classification results available from the Web API.
At this stage, it wasn't really non-programming, and it became a title scam as early as possible, but I think it's not difficult to read because it's a little over 60 lines of code.
The source is available at https://github.com/shuukei-imas-cg/imas_cg_words.
Falcon Falcon is a very fast and minimalistic web framework for backend applications. According to the benchmark posted on the Official Site, it is 9 times more performant than Flask (0.11.1), which is a standard web framework for Python. This time it's a big deal, so I'll do cythonize and set it up to run faster.
Gunicorn Gunicorn is a simple and fast WSGI web server recommended by Falcon. WSGI stands for Web Server Gateway Interface and is a standardized interface definition for web servers and web frameworks. If you follow this, you can use it with your favorite combination of web server and web application (framework).
cd imas_cg_words / (directory cloned in last article)
git pull
# Install Gunicorn, cython
pip install cython gunicorn
# Install while compiling falcon with C compiler
pip install --no-binary :all: falcon
# Start webapi server on localhost
cd webapi/
python server.py &
# Query
curl http: // localhost: 8080 / imas_cg-words / v1 / predict / delusion
[
{
"score": 1.2635555267333984,
"name:": "Hinako Kita"
},
{
"score": -0.9001807570457458,
"name:": "Atsumi Munakata"
},
{
"score": -1.0048713684082031,
"name:": "Nanami Asari"
}
]
If you throw a query with curl and it looks like the above, it's working fine. Receives arbitrary text with HTTP GET method and returns the classification result in JSON format.
When publishing as a Web API that can be accessed from the outside, simple_server is not enough, so use Gunicorn as follows. Note that when booting from Gunicorn, the IP address and port number set in config.py will be ignored and the values specified in the command line options will take precedence.
gunicorn -b (IP address) :( port number) server: api &
# When specifying the number of workers and log files
gunicorn -w 4 -b (IP address) :( port number) --access-logfile log / access.log --error-logfile log / error.log server: api &
For the IP address, specify an IP address that can be referenced from the outside.
I will explain the contents of webapi / server.py.
First, create an instance of Falcon and set the routing to assign the class PredictSerif to the path of the URI. The {words} part takes an arbitrary value, and its contents are passed to the class or function as a variable.
# Add route
api = falcon.API()
api.add_route('/imas_cg-words/v1/predict/{words}', PredictSerif())
Predict Serif defines the behavior of the HTTP GET method. The content of the function predict is almost the same as the one created previous. The return value is converted to JSON format and displayed as the output of WebAPI.
_add_headers adds some HTTP headers. In the next article, in order to access this Web API from SPA using JavaScript, we will set to allow CORS for the time being.
class PredictSerif(object):
def on_get(self, req, resp, words):
result = predict(words)
json_out = json.dumps(result, indent=4, ensure_ascii=False)
_add_headers(resp)
resp.body = json_out
resp.status = falcon.HTTP_200
logging.info("predict {0} {1}".format(req.remote_addr, words.decode('utf-8')))
Indent = 4 in json.dumps is set for readability. You can delete it in production.
You can now use the machine learning classification function as a Web API. Next time, I will create a front end that uses this Web API with Vue.js.
If you like this article, it will be held around April 2018 at THE IDOLM @ STER CINDERELLA GIRLS as much as you think it is good. Please vote for Hinako Kita at the "7th Cinderella Girl General Election" that will be held.
Recommended Posts