Create an API that returns data from a model using turicreate

Overview

There is an Apple machine learning library called turicreate. (It's machine learning models, so that should be fine)

A memo when I made a configuration to handle a model using this on GCP Reference: https://cloud.google.com/blog/products/ai-machine-learning/how-to-serve-deep-learning-models-using-tensorflow-2-0-with-cloud-functions

Constitution

Untitled (1).png

The user accesses GAE, and after that Cloud Run downloads the model from storage and works. Of course, it doesn't make sense to bring it from storage every time, so the loaded model is cached in memory.

Trouble + solution

In the reference configuration, Tensorflow model is loaded by Cloud Functions and returned as API, but in the case of Turicreate, probably it will not be possible to build and an error will occur when trying to resolve the dependency. (It's the same with GAE, it may be good to change the Cloud Build settings, but I didn't know if it would be touched by GAE or Cloud Functions)

Therefore, I decided to use Cloud Run, which allows you to create, deploy, and run your own execution image.

Implementation

The implementation itself is nothing, almost following the configuration of python + CloudRun

main.py


import turicreate
import os
from flask import Flask, request, jsonify
from google.cloud import storage
import zipfile

model = None

app = Flask(__name__)


def download_model(bucket_name, source_blob_name, dest_blob_name):
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(source_blob_name)

    blob.download_to_filename(dest_blob_name)
    with zipfile.ZipFile(dest_blob_name) as modelZip:
        modelZip.extractall('.')


@app.route('/')
def root():
    global model
    request_json = request.get_json()

    if request.args and 'userId' in request.args:
        userId = request.args.get('userId')
    else:
        return jsonify({'message': 'userId is not found'}), 400
    if 'limit' in request.args:
        limit = int(request.args.get('limit'))
    else:
        limit = 10

    if model is None:
        load_model()

    result = model.recommend(users=[userId], k=limit)

    random.shuffle(result)
    return jsonify({'result': result})

def load_model():
    global model

    os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = './credential.json'
    download_model("learning-data", "model.zip", "./model.zip")
    model = turicreate.load_model('./model')
    

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=int(
        os.environ.get('PORT', 8080)), debug=False)
FROM python:3.7

ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . .

RUN pip install -r requirements.txt

CMD [ "python", "main.py" ]

requirements.txt


turicreate==6.1
flask==1.1.1
gunicorn==20.0.4
google-cloud-storage==1.26.0

GAE is OK as long as you access the completed service

Task

→ I'm thinking about how to set the data in memory to key: time, value: model so that the model data is read asynchronously first at a separate timing.

This area may be helpful https://cloud.google.com/run/docs/tips?hl=ja

Recommended Posts

Create an API that returns data from a model using turicreate
I want to create an API that returns a model with a recursive relationship in the Django REST Framework
Tornado-Let's create a Web API that easily returns JSON with JSON
Create a CRUD API using FastAPI
Create an application using the Spotify API
Create a graph that displays an image with a mouse hover using the data visualization library Dash
Create a dataframe from excel using pandas
Create multiple line charts from a data frame at once using Matplotlib
Let's create a REST API using SpringBoot + MongoDB
Create a phylogenetic tree from Biopyton using ClustalW2
Creating an interactive application using a topic model
Create a binary data parser using Kaitai Struct
Create a Word Cloud from an academic program
I made a LINE BOT that returns a terrorist image using the Flickr API
Create a REST API using the model learned in Lobe and TensorFlow Serving.
Create an app that works well with people's reports using the COTOHA API
Develop a web API that returns data stored in DB with Django and SQLite
Create a data collection bot in Python using Selenium
Building a seq2seq model using keras's Functional API Overview
[Go] Create a tool that returns the Pokemon race value received from the standard input
A memo that detects and returns an image acquired from a webcam with Django's OpenCV
Create a pseudo REST API server using GitHub Pages
Building a seq2seq model using keras' Functional API Inference
I tried reading data from a file using Node.js.
A little bit from Python using the Jenkins API
Creating an API that returns negative-positive inference results using BERT in the Django REST framework
How to create an instance of a particular class from dict using __new__ () in python
An example of a mechanism that returns a prediction by HTTP from the result of machine learning
I made an API with Docker that returns the predicted value of the machine learning model
A memo that reads data from dashDB with Python & Spark
Instantly create a diagram of 2D data using python's matplotlib
Create a data frame from the acquired boat race text data
Build a seq2seq model using keras's Functional API Model building & learning
Create a real-time auto-reply bot using the Twitter Streaming API
Create a web API that can deliver images with Django
Create a GCE instance from a GCR Docker image using terraform
A Python program that aggregates time usage from icalendar data
Create an easy-to-use follow model in Django using ManyToManyField through
Create an API with Django
Create a dummy data file
Create a model to store information from the Google Books API for intuitive handling and testing
I came up with a way to create a 3D model from a photo Part 01 Creating an environment
Create an instance of a predefined class from a string in Python
[numpy] Create a moving window matrix from multidimensional time series data
[Python / Django] Create a web API that responds in JSON format
Create REST API that returns the current time with Python3 + Falcon
[Ev3dev] Create a program that captures the LCD (screen) using python
Create a TalkBot easily using Discord.py and A3RT's Talk API (pya3rt).
[LINE Messaging API] Create a BOT that connects with someone with Python
Get data from your website on a regular basis using ScraperWiki
Get Salesforce data using REST API
Data acquisition using python googlemap api
Create a python GUI using tkinter
Create a pandas Dataframe from a string.
A function that returns a random name
Create a nested dictionary using defaultdict
Get Amazon data using Keep API # 1 Get data
Data acquisition memo using Backlog API
Create API using hug with mod_wsgi
[Data science basics] Data acquisition from API
Creating a learning model using MNIST