Last active
September 13, 2018 22:45
-
-
Save jaycosaur/a66160951284ef9134833e7e9f4fa3f2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { google } from 'googleapis'; | |
import { auth } from 'google-auth-library' | |
export default class getModel { | |
modelName: string; | |
projectName: string; | |
constructor(attrs) { | |
this.modelName = attrs.modelName | |
this.projectName = attrs.projectName | |
} | |
// our data format was an array of strings, this needs to match your model | |
predict(dataToPredict:Array<string>):Promise<Array<any>> { | |
return new Promise((resolve,reject)=>{ | |
auth.getApplicationDefault((err, authClient)=>{ | |
if (err) { | |
reject(err); | |
} | |
// Create authenticated ml engine client | |
const ml = google.ml({ | |
version: 'v1', | |
auth: authClient | |
}); | |
// Prediction | |
const params = { | |
auth: authClient, | |
name: `projects/${this.projectName}/models/${this.modelName}`, | |
resource: { | |
instances: typeof dataToPredict === "object" ? dataToPredict : [ dataToPredict ] | |
} | |
} | |
ml.projects.predict(params, function (err2, result){ | |
if (err2){ | |
reject(err2) | |
} | |
resolve(result.data.predictions) | |
}); | |
}) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment