I want to create a machine learning service without programming! WebAPI

Contents of this time

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.

About frameworks and software to use

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).

Sample execution procedure

 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 used from the outside

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.

Commentary

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.

next time

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.

Finally (important)

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

I want to create a machine learning service without programming! WebAPI
I want to create a machine learning service without programming! Text classification
I want to do machine learning even without a server --Time Series Edition -
I want to easily create a Noise Model
I want to create a window in Python
I want to create a plug-in type implementation
I want to climb a mountain with reinforcement learning
I want to manually create a legend with matplotlib
How to create a face image data set used in machine learning (1: Acquire candidate images using WebAPI service)
I want to create a Dockerfile for the time being.
Matching app I tried to take statistics of strong people & tried to create a machine learning model
Qiskit: I want to create a circuit that creates arbitrary states! !!
I want to create a system to prevent forgetting to tighten the key 1
I want to create a pipfile and reflect it in docker
How to create a serverless machine learning API with AWS Lambda
I want to print in a comprehension
I tried to create a linebot (preparation)
I want to build a Python environment
I changed my job to a machine learning engineer at AtCoder Jobs
I want to make matplotlib a dark theme
I want to INSERT a DataFrame into MSSQL
I want to make a game with Python
If you want to create a Word Cloud.
I don't want to take a coding test
I want to easily find a delicious restaurant
I want to write to a file with Python
I want to upload a Django app to heroku
I want to create a nice Python development environment for my new Mac
I want to create a priority queue that can be updated in Python (2.7)
[Python] Deep Learning: I tried to implement deep learning (DBN, SDA) without using a library.
[Azure] I tried to create a Linux virtual machine in Azure of Microsoft Learn
I want to create a web application that uses League of Legends data ①
A machine learning beginner tried to create a sheltie judgment AI in one day
I tried to understand the learning function of neural networks carefully without using a machine learning library (first half).
Create a dataset of images to use for learning
[Keras] I tried to solve a donut-type region classification problem by machine learning [Study]
I tried to move machine learning (ObjectDetection) with TouchDesigner
I want to embed a variable in a Python string
I tried to create a table only with Django
I want to iterate a Python generator many times
I want DQN Puniki to hit a home run
100 image processing knocks !! (021-030) I want to take a break ...
I tried to make a real-time sound source separation mock with Python machine learning
I want to give a group_id to a pandas data frame
I want to generate a UUID quickly (memorandum) ~ Python ~
I want to transition with a button in flask
I want to write in Python! (2) Let's write a test
I want to find a popular package on PyPi
I want to randomly sample a file in Python
I want to easily build a model-based development environment
I tried to create a reinforcement learning environment for Othello with Open AI gym
I want to split a character string with hiragana
Preparing to start "Python machine learning programming" (for macOS)
I want to create a lunch database [EP1] Django study for the first time
I want to create a lunch database [EP1-4] Django study for the first time
[Python] I want to make a nested list a tuple
Machine learning beginners try to make a decision tree
I want to send a business start email automatically
I want to run a quantum computer with Python
Introduction to machine learning
I want to bind a local variable with lambda