import json

import numpy as np

import os

import pickle

from keras.models import model_from_json

from azureml.core.model import Model



def init():

    global model    

    model_root = Model.get_model_path('DNNdemo')

    # load json and create model

    json_file = open(os.path.join(model_root, 'model.json'), 'r')

    model_json = json_file.read()

    json_file.close()

    model = model_from_json(model_json)

    # load weights into new model

    model.load_weights(os.path.join(model_root, "model.h5"))   

    model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

     

        

def run(raw_data):

    data = np.array(json.loads(raw_data)['data'])

    # make prediction

    y_hat = np.argmax(model.predict(data), axis=1)

    return y_hat.tolist()
