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