123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.Model = exports.MachineLearning = void 0;
- const index_1 = require("../storage/index");
- const error_1 = require("../utils/error");
- const validator = require("../utils/validator");
- const deep_copy_1 = require("../utils/deep-copy");
- const utils = require("../utils");
- const machine_learning_api_client_1 = require("./machine-learning-api-client");
- const machine_learning_utils_1 = require("./machine-learning-utils");
- class MachineLearning {
-
- constructor(app) {
- if (!validator.isNonNullObject(app) || !('options' in app)) {
- throw new error_1.FirebaseError({
- code: 'machine-learning/invalid-argument',
- message: 'First argument passed to admin.machineLearning() must be a ' +
- 'valid Firebase app instance.',
- });
- }
- this.appInternal = app;
- this.client = new machine_learning_api_client_1.MachineLearningApiClient(app);
- }
-
- get app() {
- return this.appInternal;
- }
-
- createModel(model) {
- return this.signUrlIfPresent(model)
- .then((modelContent) => this.client.createModel(modelContent))
- .then((operation) => this.client.handleOperation(operation))
- .then((modelResponse) => new Model(modelResponse, this.client));
- }
-
- updateModel(modelId, model) {
- const updateMask = utils.generateUpdateMask(model);
- return this.signUrlIfPresent(model)
- .then((modelContent) => this.client.updateModel(modelId, modelContent, updateMask))
- .then((operation) => this.client.handleOperation(operation))
- .then((modelResponse) => new Model(modelResponse, this.client));
- }
-
- publishModel(modelId) {
- return this.setPublishStatus(modelId, true);
- }
-
- unpublishModel(modelId) {
- return this.setPublishStatus(modelId, false);
- }
-
- getModel(modelId) {
- return this.client.getModel(modelId)
- .then((modelResponse) => new Model(modelResponse, this.client));
- }
-
- listModels(options = {}) {
- return this.client.listModels(options)
- .then((resp) => {
- if (!validator.isNonNullObject(resp)) {
- throw new machine_learning_utils_1.FirebaseMachineLearningError('invalid-argument', `Invalid ListModels response: ${JSON.stringify(resp)}`);
- }
- let models = [];
- if (resp.models) {
- models = resp.models.map((rs) => new Model(rs, this.client));
- }
- const result = { models };
- if (resp.nextPageToken) {
- result.pageToken = resp.nextPageToken;
- }
- return result;
- });
- }
-
- deleteModel(modelId) {
- return this.client.deleteModel(modelId);
- }
- setPublishStatus(modelId, publish) {
- const updateMask = ['state.published'];
- const options = { state: { published: publish } };
- return this.client.updateModel(modelId, options, updateMask)
- .then((operation) => this.client.handleOperation(operation))
- .then((modelResponse) => new Model(modelResponse, this.client));
- }
- signUrlIfPresent(options) {
- const modelOptions = (0, deep_copy_1.deepCopy)(options);
- if ((0, machine_learning_api_client_1.isGcsTfliteModelOptions)(modelOptions)) {
- return this.signUrl(modelOptions.tfliteModel.gcsTfliteUri)
- .then((uri) => {
- modelOptions.tfliteModel.gcsTfliteUri = uri;
- return modelOptions;
- })
- .catch((err) => {
- throw new machine_learning_utils_1.FirebaseMachineLearningError('internal-error', `Error during signing upload url: ${err.message}`);
- });
- }
- return Promise.resolve(modelOptions);
- }
- signUrl(unsignedUrl) {
- const MINUTES_IN_MILLIS = 60 * 1000;
- const URL_VALID_DURATION = 10 * MINUTES_IN_MILLIS;
- const gcsRegex = /^gs:\/\/([a-z0-9_.-]{3,63})\/(.+)$/;
- const matches = gcsRegex.exec(unsignedUrl);
- if (!matches) {
- throw new machine_learning_utils_1.FirebaseMachineLearningError('invalid-argument', `Invalid unsigned url: ${unsignedUrl}`);
- }
- const bucketName = matches[1];
- const blobName = matches[2];
- const bucket = (0, index_1.getStorage)(this.app).bucket(bucketName);
- const blob = bucket.file(blobName);
- return blob.getSignedUrl({
- action: 'read',
- expires: Date.now() + URL_VALID_DURATION,
- }).then((signUrl) => signUrl[0]);
- }
- }
- exports.MachineLearning = MachineLearning;
- class Model {
-
- constructor(model, client) {
- this.model = Model.validateAndClone(model);
- this.client = client;
- }
-
- get modelId() {
- return extractModelId(this.model.name);
- }
-
- get displayName() {
- return this.model.displayName;
- }
-
- get tags() {
- return this.model.tags || [];
- }
-
- get createTime() {
- return new Date(this.model.createTime).toUTCString();
- }
-
- get updateTime() {
- return new Date(this.model.updateTime).toUTCString();
- }
-
- get validationError() {
- return this.model.state?.validationError?.message;
- }
-
- get published() {
- return this.model.state?.published || false;
- }
-
- get etag() {
- return this.model.etag;
- }
-
- get modelHash() {
- return this.model.modelHash;
- }
-
- get tfliteModel() {
-
- return (0, deep_copy_1.deepCopy)(this.model.tfliteModel);
- }
-
- get locked() {
- return (this.model.activeOperations?.length ?? 0) > 0;
- }
-
- toJSON() {
-
-
- const jsonModel = {
- modelId: this.modelId,
- displayName: this.displayName,
- tags: this.tags,
- createTime: this.createTime,
- updateTime: this.updateTime,
- published: this.published,
- etag: this.etag,
- locked: this.locked,
- };
-
- if (this.validationError) {
- jsonModel['validationError'] = this.validationError;
- }
- if (this.modelHash) {
- jsonModel['modelHash'] = this.modelHash;
- }
- if (this.tfliteModel) {
- jsonModel['tfliteModel'] = this.tfliteModel;
- }
- return jsonModel;
- }
-
- waitForUnlocked(maxTimeMillis) {
- if ((this.model.activeOperations?.length ?? 0) > 0) {
-
-
-
- return this.client.handleOperation(this.model.activeOperations[0], { wait: true, maxTimeMillis })
- .then((modelResponse) => {
- this.model = Model.validateAndClone(modelResponse);
- });
- }
- return Promise.resolve();
- }
- static validateAndClone(model) {
- 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 machine_learning_utils_1.FirebaseMachineLearningError('invalid-server-response', `Invalid Model response: ${JSON.stringify(model)}`);
- }
- const tmpModel = (0, deep_copy_1.deepCopy)(model);
-
- if (model.tfliteModel &&
- !validator.isNonEmptyString(model.tfliteModel.gcsTfliteUri)) {
-
- delete tmpModel.tfliteModel;
- }
-
- if (tmpModel['@type']) {
- delete tmpModel['@type'];
- }
- return tmpModel;
- }
- }
- exports.Model = Model;
- function extractModelId(resourceName) {
- return resourceName.split('/').pop();
- }
|