Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5256,7 +5256,7 @@ declare namespace admin.machineLearning {
readonly locked: boolean;
waitForUnlocked(maxTimeSeconds?: number): Promise<void>;

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

/**
Expand Down
217 changes: 217 additions & 0 deletions src/machine-learning/machine-learning-api-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
/*!
* 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;
}

export interface ListModelsResponse {
readonly models: ModelResponse[];
readonly nextPageToken?: string;
}

interface OperationResponse {
readonly name?: string;
readonly done: boolean;
readonly error?: StatusErrorResponse;
readonly response?: ModelResponse;
}

/**
* 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 createModel(model: ModelContent): Promise<OperationResponse> {
throw new Error('NotImplemented');
}

public updateModel(modelId: string, model: ModelContent): Promise<OperationResponse> {
throw new Error('NotImplemented');
}

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

public listModels(options: {listFilter?: string, pageSize?: number, pageToken?: string}) {
throw new Error('NotImplemented');
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove all the not-implemented stuff from this PR. Makes the future additions easier to review.

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.

}

private getUrl(): Promise<string> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I'd recommend pushing helpers like this to the bottom of the class, so that more important functionality like getResouce appears first.

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

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}`;
}

/**
* 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);
}
}

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);
}
}
55 changes: 50 additions & 5 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 tfLiteModel?: TFLiteModel;
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 get locked(): boolean {
// Backend does not currently return locked models.
Expand Down Expand Up @@ -217,3 +258,7 @@ export class ModelOptions {
throw new Error('NotImplemented');
}
}

function extractModelId(resourceName: string): string {
return resourceName.split('/').pop()!;
}
Loading