-
Notifications
You must be signed in to change notification settings - Fork 389
Added CreateModel functionality and tests #788
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,16 +14,14 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
|
||
import {FirebaseApp} from '../firebase-app'; | ||
import {FirebaseServiceInterface, FirebaseServiceInternalsInterface} from '../firebase-service'; | ||
import {MachineLearningApiClient, ModelResponse} from './machine-learning-api-client'; | ||
import {MachineLearningApiClient, ModelResponse, OperationResponse, ModelContent} 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'; | ||
import { deepCopy } from '../utils/deep-copy'; | ||
|
||
/** | ||
* Internals of an ML instance. | ||
|
@@ -97,7 +95,9 @@ export class MachineLearning implements FirebaseServiceInterface { | |
* @return {Promise<Model>} A Promise fulfilled with the created model. | ||
*/ | ||
public createModel(model: ModelOptions): Promise<Model> { | ||
throw new Error('NotImplemented'); | ||
return this.convertOptionstoContent(model, true) | ||
.then((modelContent) => this.client.createModel(modelContent)) | ||
.then((operation) => handleOperation(operation)); | ||
} | ||
|
||
/** | ||
|
@@ -170,10 +170,53 @@ export class MachineLearning implements FirebaseServiceInterface { | |
public deleteModel(modelId: string): Promise<void> { | ||
return this.client.deleteModel(modelId); | ||
} | ||
|
||
private convertOptionstoContent(options: ModelOptions, forUpload?: boolean): Promise<ModelContent> { | ||
const modelContent = deepCopy(options); | ||
|
||
if (forUpload && modelContent.tfliteModel?.gcsTfliteUri) { | ||
return this.signUrl(modelContent.tfliteModel.gcsTfliteUri) | ||
.then ((uri: string) => { | ||
modelContent.tfliteModel!.gcsTfliteUri = uri; | ||
return modelContent; | ||
}) | ||
.catch((err: Error) => { | ||
throw new FirebaseMachineLearningError( | ||
'internal-error', | ||
`Error during signing upload url: ${err.message}`); | ||
}) as Promise<ModelContent>; | ||
} | ||
|
||
return Promise.resolve(modelContent) as Promise<ModelContent>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above. I need to cast at the end after all the assignments are complete. |
||
} | ||
|
||
private signUrl(unsignedUrl: string): Promise<string> { | ||
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 FirebaseMachineLearningError( | ||
'invalid-argument', | ||
`Invalid unsigned url: ${unsignedUrl}`); | ||
} | ||
const bucketName = matches[1]; | ||
const blobName = matches[2]; | ||
const bucket = this.appInternal.storage().bucket(bucketName); | ||
const blob = bucket.file(blobName); | ||
return blob.getSignedUrl({ | ||
action: 'read', | ||
expires: Date.now() + URL_VALID_DURATION, | ||
}).then((x) => { | ||
return x[0]; | ||
}); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* A Firebase ML Model output object | ||
* A Firebase ML Model output object. | ||
*/ | ||
export class Model { | ||
public readonly modelId: string; | ||
|
@@ -196,7 +239,7 @@ export class Model { | |
!validator.isNonEmptyString(model.displayName) || | ||
!validator.isNonEmptyString(model.etag)) { | ||
throw new FirebaseMachineLearningError( | ||
'invalid-argument', | ||
'invalid-server-response', | ||
`Invalid Model response: ${JSON.stringify(model)}`); | ||
} | ||
|
||
|
@@ -252,13 +295,27 @@ export class ModelOptions { | |
public displayName?: string; | ||
public tags?: string[]; | ||
|
||
public tfliteModel?: { gcsTFLiteUri: string; }; | ||
|
||
protected toJSON(forUpload?: boolean): object { | ||
throw new Error('NotImplemented'); | ||
} | ||
public tfliteModel?: { gcsTfliteUri: string; }; | ||
} | ||
|
||
|
||
function extractModelId(resourceName: string): string { | ||
return resourceName.split('/').pop()!; | ||
} | ||
|
||
|
||
function handleOperation(op: OperationResponse): Model { | ||
// Backend currently does not return operations that are not done. | ||
if (op.done) { | ||
// Done operations must have either a response or an error. | ||
if (op.response) { | ||
return new Model(op.response); | ||
} else if (op.error) { | ||
throw FirebaseMachineLearningError.fromOperationError( | ||
op.error.code, op.error.message); | ||
} | ||
} | ||
throw new FirebaseMachineLearningError( | ||
hiranya911 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'invalid-server-response', | ||
`Invalid Operation response: ${JSON.stringify(op)}`); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is kind of weird. Just define your constant to be
ModelContent
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I do that I wouldn't be able to assign to it here: modelContent.tfliteModel!.gcsTfliteUri = uri;
because all the ModelContent properties are read only.
(That's why I initially left it as Promise<object>)