<jupyter_start><jupyter_text>IntroductionModels are trained in google colab with batch_size of 200-700. For online prediction, we only need batch_size of 1.Here we recombined models in   Get source folder and append to sys directory<jupyter_code>from __future__ import print_function
import os
import sys
PROJ_ROOT = os.path.join(os.pardir)
print(os.path.abspath(PROJ_ROOT))
src_dir = os.path.join(PROJ_ROOT, "src")
sys.path.append(src_dir)
# Data path example
#pump_data_path = os.path.join(PROJ_ROOT,
#                              "data",
#                              "raw",
#                              "pumps_train_values.csv")<jupyter_output>/mnt/4ba37af6-51fd-47bc-8321-8c500c229114/study/School/KHOA LUAN TOT NGHIEP/runnable_program<jupyter_text>ImportsImport libraries and write settings here.<jupyter_code># Data manipulation
import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import sklearn
import random

# Options for pandas
pd.options.display.max_columns = 50
pd.options.display.max_rows = 30
# Display all cell outputs
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'
from IPython import get_ipython
ipython = get_ipython()
# Visualizations
import matplotlib.pyplot as plt
import pandas as pd
import os
# import tensorflow as tf
import googleapiclient.discovery
from google.api_core.client_options import ClientOptions
import feedparser
import numpy as np
from io import StringIO
from google.cloud import storage
from features import extract_features
from data import create_load_transform_processed_data

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = <KEY>/School/KHOA LUAN TOT NGHIEP/runnable_program/flask_app/flask-app-test-317210-bdec872c665d.json"
PROJECT = "flask-app-test-317210" # change for your GCP project
REGION = "us-central1" # change for your GCP region (where your model is hosted)
MODEL = ['hour_1', 'hour_2', 'hour_3', 'hour_4', 'hour_5']

def predict_json(project, region, model, instances, version=None):
    """Send json data to a deployed model for prediction.

    Args:
        project (str): project where the Cloud ML Engine Model is deployed.
        model (str): model name.
        instances ([Mapping[str: Any]]): Keys should be the names of Tensors
            your deployed model expects as inputs. Values should be datatypes
            convertible to Tensors, or (potentially nested) lists of datatypes
            convertible to Tensors.
        version (str): version of the model to target.
    Returns:
        Mapping[str: any]: dictionary of prediction results defined by the 
            model.
    """
    # Create the ML Engine service object
    prefix = "{}-ml".format(region) if region else "ml"
    api_endpoint = "https://{}.googleapis.com".format(prefix)
    client_options = ClientOptions(api_endpoint=api_endpoint)

    # Setup model path
    model_path = "projects/{}/models/{}".format(project, model)
    if version is not None:
        model_path += "/versions/{}".format(version)

    # Create ML engine resource endpoint and input data
    ml_resource = googleapiclient.discovery.build(
        "ml", "v1", cache_discovery=False, client_options=client_options).projects()
    # turn input into list (ML Engine wants JSON)
    instances_list = instances.tolist()

    input_data_json = {"signature_name": "serving_default",
                       "instances": instances_list}

    request = ml_resource.predict(name=model_path, body=input_data_json)
    response = request.execute()

    # # ALT: Create model api
    # model_api = api_endpoint + model_path + ":predict"
    # headers = {"Authorization": "Bearer " + token}
    # response = requests.post(model_api, json=input_data_json, headers=headers)

    if "error" in response:
        raise RuntimeError(response["error"])

    scaler = extract_features.load_scaler()
    reverse_scaled_prediction = scaler.inverse_transform(response['predictions'])

    return reverse_scaled_prediction
import requests

# change for your GCP key
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "flask-app-test-317210-bdec872c665d.json"
PROJECT = "flask-app-test-317210"  # change for your GCP project
# change for your GCP region (where your model is hosted)
REGION = "us-central1"


_predict_data_url = "https://flask-app-test-317210.de.r.appspot.com/get_predict_result"

r = requests.get(url=_predict_data_url)
site_data = r.json()
site_data
def get_data_from_bucket_as_dataframe(filename="past_data.csv"):
    """Read a blob"""
    bucket_name = "deep_learning_model_bucket"

    storage_client = storage.Client()

    bucket = storage_client.bucket(bucket_name)
    blob = bucket.blob(filename)
    if blob.exists() == False:
        return None

    return_data = blob.download_as_text()
    return_data = StringIO(return_data)
    df = pd.read_csv(return_data, sep=",", header=0, index_col=False)
    return df
data_df = get_data_from_bucket_as_dataframe()
data_df = data_df.tail(25)
data_df = data_df.astype({'time': 'datetime64[ns]', 'AQI_h': 'float'})
data_df.set_index(['site_id', 'time'], inplace=True)
data_df
def create_input_for_model(df, timesteps=[1], target_hour=[1], test_output=False, dev_output=False, output_path=None, PROJ_ROOT=os.pardir):
    """From interim dataframe:
        + add features
        + split into chunks according to timesteps
        + compressed and saved to output_path
        + estimate number of created dataset = timesteps * target_hour
    Parameters
    ----------
    df : pandas.DataFrame
        Contains interim data.
    timesteps : list of integer
        Each timestep represent 1 dataset
    target_hour : list of integer
        the label for each timesteps
    output_path : string
        Destination directory the dataset will be created
    """
    if output_path == None:
        output_path == os.path.join(PROJ_ROOT,
                                    "data",
                                    "model_input")
    for timesteps in timesteps:
        for target_hour in target_hour:
            # Create train, dev, test data
            train_df = extract_features.create_and_save_scale_data(df, output_path=output_path).copy()
            train_df = extract_features.add_features(train_df).copy()
            if test_output is not False:
                train_df, test_df = extract_features.generate_train_test_set_by_time(
                    train_df)
                test, y_test, multiclass_y_test = extract_features.data_preprocessing(
                    test_df, target_hour, timesteps=timesteps)
                create_load_transform_processed_data.reshape_array_and_save_to_path(
                    test, y_test, path=output_path, timesteps=timesteps, target_hour=target_hour, data_type="test")
            if dev_output is not False:
                train_df, dev_df = extract_features.generate_train_test_set_by_time(
                    train_df)
                dev, y_dev, multiclass_y_dev = extract_features.data_preprocessing(
                    dev_df, target_hour, timesteps=timesteps)
                create_load_transform_processed_data.reshape_array_and_save_to_path(
                    dev, y_dev, path=output_path, timesteps=timesteps, target_hour=target_hour, data_type="dev")

            train, y_train, multiclass_y = extract_features.data_preprocessing(
                train_df, target_hour, timesteps=timesteps)

            # Save data to file
            if output_path is not None:
                create_load_transform_processed_data.reshape_array_and_save_to_path(
                    train, y_train, path=output_path, timesteps=timesteps, target_hour=target_hour, data_type="train")
    train = train.astype('float32')
    y_train = y_train.astype('float32')
    print("Input have been created")
    return train, y_train
data_df = data_df.tail(48)
predict_data, label = create_input_for_model(data_df, timesteps=[5], target_hour=[1])
predict_data
tmp_predict_data = predict_data[-1].copy()
tmp_predict_data = np.reshape(tmp_predict_data, (1, predict_data.shape[1], predict_data.shape[2]))
tmp_predict_data.shape
past_prediction = get_data_from_bucket_as_dataframe(
        filename="past_data.csv")
past_prediction['AQI_h']
all_result = np.array([])
for model in range(0,5):
    preds = predict_json(project=PROJECT,
                                region=REGION,
                                model=MODEL[model],
                                instances=tmp_predict_data)
    all_result = np.append(all_result, preds)
all_result
# Now let's load and scale predicted data<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08783471837488457, 'fraction_numerical': 0.012119113573407203, 'mean_word_length': 3.358651911468813, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '173742', 'license': 'MIT', 'n_tokens_mistral': 2734, 'n_tokens_neox': 2600, 'n_words': 644, 'path': 'notebooks/10_nam_get_prediction_from_gcp.ipynb', 'repo_name': 'namph-sgn/Deep-learning-BLSTM', 'size': '8707'}