Created
July 20, 2020 15:02
-
-
Save tbass134/7985c0adf44c938d6e683c18dabac8f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from google.cloud import storage | |
import joblib | |
import sklearn | |
def predict(request): | |
"""Responds to any HTTP request. | |
Args: | |
request (flask.Request): HTTP request object. | |
Returns: | |
The response text or any set of values that can be turned into a | |
Response object using | |
`make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`. | |
""" | |
request_json = request.get_json() | |
message = request_json['message'] | |
# message = "im on my way home" | |
model = load_model() | |
vectorizer = load_vectorizer() | |
print("model",model) | |
print("vectorizer", vectorizer) | |
message = vectorizer.transform([message]) | |
message = message.toarray() | |
prediction = model.predict(message)[0] | |
print("prediction",prediction) | |
return prediction | |
def load_model(): | |
storage_client = storage.Client() | |
bucket_name="spam_classifier" | |
model_bucket='model.joblib' | |
model_local='/tmp/local.joblib' | |
bucket = storage_client.get_bucket(bucket_name) | |
#select bucket file | |
blob = bucket.blob(model_bucket) | |
#download that file and name it 'local.joblib' | |
blob.download_to_filename(model_local) | |
#load that file from local file | |
job=joblib.load(model_local) | |
return job | |
def load_vectorizer(): | |
storage_client = storage.Client() | |
bucket_name="spam_classifier" | |
model_bucket='vectorizer.joblib' | |
model_local='/tmp/vectorizer.joblib' | |
bucket = storage_client.get_bucket(bucket_name) | |
#select bucket file | |
blob = bucket.blob(model_bucket) | |
#download that file and name it 'local.joblib' | |
blob.download_to_filename(model_local) | |
#load that file from local file | |
job=joblib.load(model_local) | |
return job |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment