Skip to content

Added GetModel functionality and tests #781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5252,11 +5252,11 @@ declare namespace admin.machineLearning {
readonly validationError?: string;
readonly published: boolean;
readonly etag: string;
readonly modelHash: string;
readonly modelHash?: string;
readonly locked: boolean;
waitForUnlocked(maxTimeSeconds?: number): Promise<void>;

readonly tfLiteModel?: TFLiteModel;
readonly tfliteModel?: TFLiteModel;
}

/**
Expand Down
194 changes: 194 additions & 0 deletions src/machine-learning/machine-learning-api-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/*!
* Copyright 2020 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { HttpRequestConfig, HttpClient, HttpError, AuthorizedHttpClient } from '../utils/api-request';
import { PrefixedFirebaseError } from '../utils/error';
import { FirebaseMachineLearningError, MachineLearningErrorCode } from './machine-learning-utils';
import * as utils from '../utils/index';
import * as validator from '../utils/validator';
import { FirebaseApp } from '../firebase-app';

const ML_V1BETA1_API = 'https://mlkit.googleapis.com/v1beta1';
const FIREBASE_VERSION_HEADER = {
'X-Firebase-Client': 'fire-admin-node/<XXX_SDK_VERSION_XXX>',
};

export interface StatusErrorResponse {
readonly code: number;
readonly message: string;
}

export interface ModelContent {
readonly displayName?: string;
readonly tags?: string[];
readonly state?: {
readonly validationError?: StatusErrorResponse;
readonly published?: boolean;
};
readonly tfliteModel?: {
readonly gcsTfliteUri: string;
readonly sizeBytes: number;
};
}

export interface ModelResponse extends ModelContent {
readonly name: string;
readonly createTime: string;
readonly updateTime: string;
readonly etag: string;
readonly modelHash?: string;
}


/**
* Class that facilitates sending requests to the Firebase ML backend API.
*
* @private
*/
export class MachineLearningApiClient {
private readonly httpClient: HttpClient;
private projectIdPrefix?: string;

constructor(private readonly app: FirebaseApp) {
if (!validator.isNonNullObject(app) || !('options' in app)) {
throw new FirebaseMachineLearningError(
'invalid-argument',
'First argument passed to admin.machineLearning() must be a valid '
+ 'Firebase app instance.');
}

this.httpClient = new AuthorizedHttpClient(app);
}

public getModel(modelId: string): Promise<ModelResponse> {
return Promise.resolve()
.then(() => {
return this.getModelName(modelId);
})
.then((modelName) => {
return this.getResource<ModelResponse>(modelName);
});
}

/**
* Gets the specified resource from the ML API. Resource names must be the short names without project
* ID prefix (e.g. `models/123456789`).
*
* @param {string} name Full qualified name of the resource to get.
* @returns {Promise<T>} A promise that fulfills with the resource.
*/
private getResource<T>(name: string): Promise<T> {
return this.getUrl()
.then((url) => {
const request: HttpRequestConfig = {
method: 'GET',
url: `${url}/${name}`,
};
return this.sendRequest<T>(request);
});
}

private sendRequest<T>(request: HttpRequestConfig): Promise<T> {
request.headers = FIREBASE_VERSION_HEADER;
return this.httpClient.send(request)
.then((resp) => {
return resp.data as T;
})
.catch((err) => {
throw this.toFirebaseError(err);
});
}

private toFirebaseError(err: HttpError): PrefixedFirebaseError {
if (err instanceof PrefixedFirebaseError) {
return err;
}

const response = err.response;
if (!response.isJson()) {
return new FirebaseMachineLearningError(
'unknown-error',
`Unexpected response with status: ${response.status} and body: ${response.text}`);
}

const error: Error = (response.data as ErrorResponse).error || {};
let code: MachineLearningErrorCode = 'unknown-error';
if (error.status && error.status in ERROR_CODE_MAPPING) {
code = ERROR_CODE_MAPPING[error.status];
}
const message = error.message || `Unknown server error: ${response.text}`;
return new FirebaseMachineLearningError(code, message);
}

private getUrl(): Promise<string> {
return this.getProjectIdPrefix()
.then((projectIdPrefix) => {
return `${ML_V1BETA1_API}/${this.projectIdPrefix}`;
});
}

private getProjectIdPrefix(): Promise<string> {
if (this.projectIdPrefix) {
return Promise.resolve(this.projectIdPrefix);
}

return utils.findProjectId(this.app)
.then((projectId) => {
if (!validator.isNonEmptyString(projectId)) {
throw new FirebaseMachineLearningError(
'invalid-argument',
'Failed to determine project ID. Initialize the SDK with service account credentials, or '
+ 'set project ID as an app option. Alternatively, set the GOOGLE_CLOUD_PROJECT '
+ 'environment variable.');
}

this.projectIdPrefix = `projects/${projectId}`;
return this.projectIdPrefix;
});
}

private getModelName(modelId: string): string {
if (!validator.isNonEmptyString(modelId)) {
throw new FirebaseMachineLearningError(
'invalid-argument', 'Model ID must be a non-empty string.');
}

if (modelId.indexOf('/') !== -1) {
throw new FirebaseMachineLearningError(
'invalid-argument', 'Model ID must not contain any "/" characters.');
}

return `models/${modelId}`;
}
}

interface ErrorResponse {
error?: Error;
}

interface Error {
code?: number;
message?: string;
status?: string;
}

const ERROR_CODE_MAPPING: {[key: string]: MachineLearningErrorCode} = {
INVALID_ARGUMENT: 'invalid-argument',
NOT_FOUND: 'not-found',
RESOURCE_EXHAUSTED: 'resource-exhausted',
UNAUTHENTICATED: 'authentication-error',
UNKNOWN: 'unknown-error',
};
34 changes: 34 additions & 0 deletions src/machine-learning/machine-learning-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*!
* Copyright 2020 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { PrefixedFirebaseError } from '../utils/error';

export type MachineLearningErrorCode =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove any errors codes that you're not explicitly referencing for now. You can add them in the future PR if they become necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are all possible to come back from the server.

'already-exists'
| 'authentication-error'
| 'internal-error'
| 'invalid-argument'
| 'invalid-server-response'
| 'not-found'
| 'resource-exhausted'
| 'service-unavailable'
| 'unknown-error';

export class FirebaseMachineLearningError extends PrefixedFirebaseError {
constructor(code: MachineLearningErrorCode, message: string) {
super('machine-learning', code, message);
}
}
59 changes: 52 additions & 7 deletions src/machine-learning/machine-learning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

import {FirebaseApp} from '../firebase-app';
import {FirebaseServiceInterface, FirebaseServiceInternalsInterface} from '../firebase-service';
import {MachineLearningApiClient, ModelResponse} from './machine-learning-api-client';
import {FirebaseError} from '../utils/error';

import * as validator from '../utils/validator';
import {FirebaseMachineLearningError} from './machine-learning-utils';

// const ML_HOST = 'mlkit.googleapis.com';

Expand Down Expand Up @@ -58,6 +60,7 @@ export interface ListModelsResult {
export class MachineLearning implements FirebaseServiceInterface {
public readonly INTERNAL = new MachineLearningInternals();

private readonly client: MachineLearningApiClient;
private readonly appInternal: FirebaseApp;

/**
Expand All @@ -68,12 +71,13 @@ export class MachineLearning implements FirebaseServiceInterface {
if (!validator.isNonNullObject(app) || !('options' in app)) {
throw new FirebaseError({
code: 'machine-learning/invalid-argument',
message: 'First argument passed to admin.MachineLearning() must be a ' +
message: 'First argument passed to admin.machineLearning() must be a ' +
'valid Firebase app instance.',
});
}

this.appInternal = app;
this.client = new MachineLearningApiClient(app);
}

/**
Expand Down Expand Up @@ -138,7 +142,10 @@ export class MachineLearning implements FirebaseServiceInterface {
* @return {Promise<Model>} A Promise fulfilled with the unpublished model.
*/
public getModel(modelId: string): Promise<Model> {
throw new Error('NotImplemented');
return this.client.getModel(modelId)
.then((modelResponse) => {
return new Model(modelResponse);
});
}

/**
Expand Down Expand Up @@ -172,14 +179,48 @@ export class Model {
public readonly modelId: string;
public readonly displayName: string;
public readonly tags?: string[];
public readonly createTime: number;
public readonly updateTime: number;
public readonly createTime: string;
public readonly updateTime: string;
public readonly validationError?: string;
public readonly published: boolean;
public readonly etag: string;
public readonly modelHash: string;
public readonly modelHash?: string;

public readonly tfliteModel?: TFLiteModel;

constructor(model: ModelResponse) {
if (!validator.isNonNullObject(model) ||
!validator.isNonEmptyString(model.name) ||
!validator.isNonEmptyString(model.createTime) ||
!validator.isNonEmptyString(model.updateTime) ||
!validator.isNonEmptyString(model.displayName) ||
!validator.isNonEmptyString(model.etag)) {
throw new FirebaseMachineLearningError(
'invalid-argument',
`Invalid Model response: ${JSON.stringify(model)}`);
}

this.modelId = extractModelId(model.name);
this.displayName = model.displayName;
this.tags = model.tags || [];
this.createTime = new Date(model.createTime).toUTCString();
this.updateTime = new Date(model.updateTime).toUTCString();
if (model.state?.validationError?.message) {
this.validationError = model.state?.validationError?.message;
}
this.published = model.state?.published || false;
this.etag = model.etag;
if (model.modelHash) {
this.modelHash = model.modelHash;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is set conditionally, change the type declaration to be modelHash?. Both here in the class, and also in the d.ts file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

}
if (model.tfliteModel) {
this.tfliteModel = {
gcsTfliteUri: model.tfliteModel.gcsTfliteUri,
sizeBytes: model.tfliteModel.sizeBytes,
};
}

public readonly tfLiteModel?: TFLiteModel;
}

public get locked(): boolean {
// Backend does not currently return locked models.
Expand Down Expand Up @@ -211,9 +252,13 @@ export class ModelOptions {
public displayName?: string;
public tags?: string[];

public tfLiteModel?: { gcsTFLiteUri: string; };
public tfliteModel?: { gcsTFLiteUri: string; };

protected toJSON(forUpload?: boolean): object {
throw new Error('NotImplemented');
}
}

function extractModelId(resourceName: string): string {
return resourceName.split('/').pop()!;
}
34 changes: 34 additions & 0 deletions test/integration/machine-learning.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*!
* Copyright 2020 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as admin from '../../lib/index';

describe('admin.machineLearning', () => {
describe('getModel()', () => {
it('rejects with not-found when the Model does not exist', () => {
const nonExistingName = '00000000';
return admin.machineLearning().getModel(nonExistingName)
.should.eventually.be.rejected.and.have.property(
'code', 'machine-learning/not-found');
});

it('rejects with invalid-argument when the ModelId is invalid', () => {
return admin.machineLearning().getModel('invalid-model-id')
.should.eventually.be.rejected.and.have.property(
'code', 'machine-learning/invalid-argument');
});
});
});
Loading