<jupyter_start><jupyter_text>![banner](https://user-images.githubusercontent.com/5395649/46774810-22395980-ccb9-11e8-8f1a-535769d657ec.png)  Loading the Model from the Model Catalog<jupyter_code>import os 
import warnings
warnings.filterwarnings('ignore')
import logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.ERROR)
import tensorflow as tf 
tf.logging.set_verbosity(tf.logging.ERROR)
import pickle as pkl 
import pandas as pd 
import numpy as np 
import json 
import requests 
from json import dumps, dump
import librosa
import IPython.display as ipd

# oci: 
import oci 
from oci.config import from_file
from oci import pagination
import oci.functions as functions
from oci.functions import FunctionsManagementClient, FunctionsInvokeClient

# ads: 
from ads.common.model_artifact import ModelArtifact
from ads.catalog.model import ModelSummaryList, ModelCatalog
mc = ModelCatalog(compartment_id = os.environ['NB_SESSION_COMPARTMENT_OCID'])

mc.list_models()
path_to_my_loaded_model = '/home/datascience/block_storage/models/tmp'
mc.download_model(mc.list_models()[0].id, path_to_my_loaded_model, force_overwrite=True)
model_artifact = ModelArtifact(path_to_my_loaded_model)
model_artifact.reload()
model3 = model_artifact.model<jupyter_output><empty_output><jupyter_text>We're going to load some of this audio data<jupyter_code>df = pkl.load(open('../data/processed_data.pkl','rb'))
X_test = df[df['sample'] == "testing"]['mfcc'].values
Y_test = df[df['sample'] == "testing"]['y_hot'].values
Xtest = np.asarray([ sub.reshape(20,32,1) for sub in X_test ])
model3.predict(Xtest[0].reshape(1,20,32,1))<jupyter_output><empty_output><jupyter_text>Testing Model Deployment through Oracle FunctionsWe will load pre-recorded wave files and invoke the Function through the OCI Python SDK. Note that the docker image has already been build and and push to the OCI Registry. We also deployed a Function to Oracle Functions. Here we will walk you through the steps needed to invoke that Function. You do not need to modify the cell below. We gave your user group access to the Functions deployed in the compartment listed below.<jupyter_code># Lets specify the location of our OCI configuration file: 
oci_config = from_file("/home/datascience/block_storage/.oci/config")

# Lets specify the compartment OCID, and the application + function names: 
compartment_id = 'ocid1.compartment.oc1..aaaaaaaafl3avkal72rrwuy4m5rumpwh7r4axejjwq5hvwjy4h4uoyi7kzyq' 
app_name = 'machine-learning-models'
fn_name = 'speech-commands'
fn_management_client = FunctionsManagementClient(oci_config)
app_result = pagination.list_call_get_all_results(
        fn_management_client.list_applications,
        compartment_id,
        display_name=app_name
    )
app_result.data[0].id
fn_result = pagination.list_call_get_all_results(
        fn_management_client.list_functions,
        app_result.data[0].id,
        display_name=fn_name
    )
fn_result.data[0].id<jupyter_output><empty_output><jupyter_text>In the cell below we are creating an instance of the `FunctionsInvokeClient` object. This instance object will target our deployed Function<jupyter_code>invoke_client = FunctionsInvokeClient(oci_config, service_endpoint=fn_result.data[0].invoke_endpoint)<jupyter_output><empty_output><jupyter_text>Let's pass an audio waveform to our function object.Note the audio clip waveform is converted to a json string object before being passed to the Function.<jupyter_code># directory where some sample wave files are located: 
sample_data_dir = '../data/'

# filenames 
files = ['6b81fead_nohash_0.wav',
         'ff2b842e_nohash_2.wav',
         'ccb1266b_nohash_1.wav']<jupyter_output><empty_output><jupyter_text>Let's play these audio clips<jupyter_code>ipd.Audio(sample_data_dir+files[0])
ipd.Audio(sample_data_dir+files[1])
ipd.Audio(sample_data_dir+files[2])
waveform0, _ = librosa.load(sample_data_dir+files[0], mono=True, sr=None)
waveform1, _ = librosa.load(sample_data_dir+files[1], mono=True, sr=None)
waveform2, _ = librosa.load(sample_data_dir+files[2], mono=True, sr=None)<jupyter_output><empty_output><jupyter_text>Below we call the deployed Function. Note that the first call could take 60 sec. or more. This is due to the cold start problem of Function. Subsequent calls are much faster. Typically < 1 sec.<jupyter_code>%%time

resp = invoke_client.invoke_function(fn_result.data[0].id, 
                                     invoke_function_body=json.dumps({"input": waveform0.tolist()}))
print(resp.data.text)
%%time

resp = invoke_client.invoke_function(fn_result.data[0].id, 
                                     invoke_function_body=json.dumps({"input": waveform1.tolist()}))
print(resp.data.text)
%%time

resp = invoke_client.invoke_function(fn_result.data[0].id, 
                                     invoke_function_body=json.dumps({"input": waveform2.tolist()}))
print(resp.data.text)<jupyter_output>{"what-you-said-was": "eight"}
CPU times: user 32.8 ms, sys: 2.9 ms, total: 35.8 ms
Wall time: 385 ms
{'fraction_non_alphanumeric': 0.09948269001193792, 'fraction_numerical': 0.02248308794269797, 'mean_word_length': 5.554106910039113, 'pattern_counts': {'":': 4, '<': 29, '<?xml version=': 0, '>': 28, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '447446', 'license': 'MIT', 'n_tokens_mistral': 1733, 'n_tokens_neox': 1644, 'n_words': 383, 'path': 'notebooks/3-testing-model-deployment.ipynb', 'repo_name': 'jrgauthier01/speech-commands-oow2018', 'size': '5026'}