from sklearn.neighbors import KNeighborsClassifier
from sklearn import datasets
from joblib import dump
from google.cloud import storage

import os
import datetime

def upload_blob(bucket_name, source_file_name, destination_blob_name):
    """Uploads a file to the bucket."""
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(destination_blob_name)

    blob.upload_from_filename(source_file_name)

    print(f'File {source_file_name} uploaded to {destination_blob_name}.')

def rename_blob(bucket_name, blob_name, new_name):
    """Renames a blob."""
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(blob_name)

    new_blob = bucket.rename_blob(blob, new_name)

    print(f'Blob {blob.name} has been renamed to {new_blob.name}')

def list_blobs(bucket_name, prefix):
    """Lists all the blobs in the bucket."""
    storage_client = storage.Client()

    # Note: Client.list_blobs requires at least package version 1.17.0.
    blobs = storage_client.list_blobs(bucket_name, prefix=prefix)

    return blobs

# Training

bucket_name = os.environ['ML_MODELS_BUCKET']
datetime_now = f"{datetime.datetime.now():%Y%m%d_%H%M%S}"
model_name = f"iris_{datetime_now}.joblib.latest"
bucket_prefix = 'iris'

print("Training model ...")
clf = KNeighborsClassifier()
iris = datasets.load_iris()
X, y = iris.data, iris.target
clf.fit(X, y)
dump(clf, model_name)

print(f"Finding previous version in bucket {bucket_name} ...")
blob_names = list_blobs(bucket_name, bucket_prefix)

previous_model_old_name = ""
previous_model_new_name = ""
for blob in blob_names:
    if blob.name.find(".latest", -8) > 0:
        previous_model_old_name = blob.name
        previous_model_new_name = blob.name.replace(".latest","")
        print(f"Renaming previous version {previous_model_old_name} to {previous_model_new_name} ...")
        rename_blob(bucket_name, previous_model_old_name, previous_model_new_name)

destination_blob_name = f"{bucket_prefix}/{model_name}"
print(f"Uploading {model_name} to bucket {bucket_name} as {destination_blob_name} ...")
upload_blob(bucket_name,model_name,destination_blob_name)

print(clf.predict(X))
print("Done.")

from sklearn.neighbors import KNeighborsClassifier
from sklearn import datasets
from joblib import dump
from google.cloud import storage

import os
import datetime

def upload_blob(bucket_name, source_file_name, destination_blob_name):
    """Uploads a file to the bucket."""
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(destination_blob_name)

    blob.upload_from_filename(source_file_name)

    print(f'File {source_file_name} uploaded to {destination_blob_name}.')

def rename_blob(bucket_name, blob_name, new_name):
    """Renames a blob."""
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(blob_name)

    new_blob = bucket.rename_blob(blob, new_name)

    print(f'Blob {blob.name} has been renamed to {new_blob.name}')

def list_blobs(bucket_name, prefix):
    """Lists all the blobs in the bucket."""
    storage_client = storage.Client()

    # Note: Client.list_blobs requires at least package version 1.17.0.
    blobs = storage_client.list_blobs(bucket_name, prefix=prefix)

    return blobs

# Training

bucket_name = os.environ['ML_MODELS_BUCKET']
datetime_now = f"{datetime.datetime.now():%Y%m%d_%H%M%S}"
model_name = f"iris_{datetime_now}.joblib.latest"
bucket_prefix = 'iris'

print("Training model ...")
clf = KNeighborsClassifier()
iris = datasets.load_iris()
X, y = iris.data, iris.target
clf.fit(X, y)
dump(clf, model_name)

print(f"Finding previous version in bucket {bucket_name} ...")
blob_names = list_blobs(bucket_name, bucket_prefix)

previous_model_old_name = ""
previous_model_new_name = ""
for blob in blob_names:
    if blob.name.find(".latest", -8) > 0:
        previous_model_old_name = blob.name
        previous_model_new_name = blob.name.replace(".latest","")
        print(f"Renaming previous version {previous_model_old_name} to {previous_model_new_name} ...")
        rename_blob(bucket_name, previous_model_old_name, previous_model_new_name)

destination_blob_name = f"{bucket_prefix}/{model_name}"
print(f"Uploading {model_name} to bucket {bucket_name} as {destination_blob_name} ...")
upload_blob(bucket_name,model_name,destination_blob_name)

print(clf.predict(X))
print("Done.")

Uploads a file to the bucket. Renames a blob. Lists all the blobs in the bucket. # Note: Client.list_blobs requires at least package version 1.17.0. # Training