Skip to content

Instantly share code, notes, and snippets.

@ohtaman
Created December 13, 2019 22:26
Show Gist options
  • Save ohtaman/e69fd11c25b43595993c823c6c8c0dac to your computer and use it in GitHub Desktop.
Save ohtaman/e69fd11c25b43595993c823c6c8c0dac to your computer and use it in GitHub Desktop.
TensorFlow Hub + Cloud Run
import os
from flask import Flask, request, jsonify
import tensorflow_hub as hub
import tensorflow_text
app = Flask(__name__)
model = "https://tfhub.dev/google/nnlm-ja-dim50-with-normalization/2"
module = hub.load(model)
@app.route('/')
def get_embedding():
sentence = request.args.get('sentence', '')
emb = module([sentence])
return jsonify({'embedding': emb.numpy().tolist()})
if __name__ == "__main__":
app.run(host='0.0.0.0',port=int(os.environ.get('PORT', 8080)))
FROM python:3.7
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . .
RUN pip install Flask gunicorn tensorflow tensorflow-hub tensorflow_text>=2.0.0rc0
RUN python load_module.py
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 app:app
import tensorflow_hub as hub
import tensorflow_text
model = "https://tfhub.dev/google/nnlm-ja-dim50-with-normalization/2"
if __name__ == '__main__':
hub.load(model)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment