Skip to content

Firebase ML Kit Get Model API implementation #326

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 7 commits into from
Aug 19, 2019
Merged

Firebase ML Kit Get Model API implementation #326

merged 7 commits into from
Aug 19, 2019

Conversation

ifielker
Copy link
Contributor

@ifielker ifielker commented Aug 19, 2019

Firebase ML Kit Get Model API implementation

Copy link
Contributor

@hiranya911 hiranya911 left a comment

Choose a reason for hiding this comment

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

Thanks for keeping changes small. This looks mostly good. Just a few things pointed out for improvement. Additionally:

  1. Fix the lint errors
  2. Remove line 19 of the .travis.yml file for now (RTDB team is working on the test failures)
  3. Edit the PR title and type in something a bit more descriptive (makes it easier to compile release notes and stuff later)

class _MLKitService(object):
"""Firebase MLKit service."""

BASE_URL = 'https://mlkit.googleapis.com'
PROJECT_URL = 'https://mlkit.googleapis.com/projects/{0}/'
BASE_URL = 'https://mlkit.googleapis.com/v1beta1/'
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't seem to be used in this module. Perhaps move to the tests module, where it's being referenced?

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

self._project_url = _MLKitService.PROJECT_URL.format(project_id)
self._client = _http_client.JsonHttpClient(credential=app.credential.get_credential())

def _request(self, method, urlpath, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems to be just appending the path to the base/project URL. I'd suggest passing the base_url parameter to the JsonHttpClient constructor and getting rid of this. You can just call the client directly as:

self._client.body(method, urlpath)

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

try:
return self._request('get', 'models/{0}'.format(model_id))
except requests.exceptions.RequestException as error:
raise _utils.handle_requests_error(error)
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this is a OnePlatform API, it's better to call _utils.handle_platform_error_from_requests(error), which knows how to parse OP errors.

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

testutils.cleanup_apps()

@staticmethod
def check_error(err, errType, msg):
Copy link
Contributor

Choose a reason for hiding this comment

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

s/errType/err_type

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 not model_id:
raise ValueError('Model Id is required for GetModel.')
if not isinstance(model_id, six.string_types):
raise TypeError('Model Id must be a string.')
Copy link
Contributor

Choose a reason for hiding this comment

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

s/Id/ID

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 self._client.body(method, url=self._project_url + urlpath, **kwargs)

def get_model(self, model_id):
if not model_id:
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be removed. If model_id is None, your type check will catch it. If it's empty your regex check will catch it.

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

_, recorder = self._instrument_mlkit_service(status=404, payload=_ERROR_RESPONSE)
with pytest.raises(exceptions.NotFoundError) as err:
mlkit.get_model(MODEL_ID_1)
self.check_firebase_error(err.value, ERROR_STATUS, ERROR_CODE)
Copy link
Contributor

Choose a reason for hiding this comment

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

Also assert the error message. If the OP error got parsed correctly, error message gets set to the error.message field of the response.

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

with pytest.raises(ValueError):
mlkit.get_model(MODEL_ID_1, app)
testutils.run_without_project_id(evaluate)

Copy link
Contributor

Choose a reason for hiding this comment

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

Remove redundant empty lines.

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

@hiranya911 hiranya911 assigned ifielker and unassigned hiranya911 Aug 19, 2019
@ifielker ifielker changed the title Mlkit 2 Firebase ML Kit Get Model API implementation Aug 19, 2019
Copy link
Contributor

@hiranya911 hiranya911 left a comment

Choose a reason for hiding this comment

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

LGTM with a couple of suggestions 👍

'Project ID is required to access MLKit service. Either set the '
'projectId option, or use service account credentials.')
self._project_url = _MLKitService.PROJECT_URL.format(project_id)
self._client = _http_client.JsonHttpClient(credential=app.credential.get_credential())
Copy link
Contributor

Choose a reason for hiding this comment

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

Also pass base_url=self._project_url to the constructor

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

try:
return self._client.body(
'get',
url=self._project_url + 'models/{0}'.format(model_id))
Copy link
Contributor

Choose a reason for hiding this comment

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

Once you pass base_url the constructor, you don't have to do this concat here. Just pass the path modles/... portion as the url.

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

@hiranya911 hiranya911 removed their assignment Aug 19, 2019
@ifielker ifielker merged commit 65f64c0 into mlkit Aug 19, 2019
@ifielker ifielker deleted the mlkit-2 branch August 19, 2019 20:28
lahirumaramba pushed a commit that referenced this pull request Apr 20, 2020
* Introduced the exceptions module (#296)

* Added the exceptions module

* Cleaned up the error handling logic; Added tests

* Updated docs; Fixed some typos

* Migrating FCM Send APIs to the New Exceptions (#297)

* Migrated FCM send APIs to the new error handling regime

* Moved error parsing logic to _utils

* Refactored OP error handling code

* Fixing a broken test

* Added utils for handling googleapiclient errors

* Added tests for new error handling logic

* Updated public API docs

* Fixing test for python3

* Cleaning up the error code lookup code

* Cleaning up the error parsing APIs

* Cleaned up error parsing logic; Updated docs

* Migrated remaining messaging APIs to new error types (#298)

* Migrated FCM send APIs to the new error handling regime

* Moved error parsing logic to _utils

* Refactored OP error handling code

* Fixing a broken test

* Added utils for handling googleapiclient errors

* Added tests for new error handling logic

* Updated public API docs

* Fixing test for python3

* Cleaning up the error code lookup code

* Cleaning up the error parsing APIs

* Cleaned up error parsing logic; Updated docs

* Migrated the FCM IID APIs to the new error types

* Introducing TokenSignError to represent custom token creation errors (#302)

* Migrated FCM send APIs to the new error handling regime

* Moved error parsing logic to _utils

* Refactored OP error handling code

* Fixing a broken test

* Added utils for handling googleapiclient errors

* Added tests for new error handling logic

* Updated public API docs

* Fixing test for python3

* Cleaning up the error code lookup code

* Cleaning up the error parsing APIs

* Cleaned up error parsing logic; Updated docs

* Migrated the FCM IID APIs to the new error types

* Migrated custom token API to new error types

* Raising FirebaseError from create_session_cookie() API (#306)

* Migrated FCM send APIs to the new error handling regime

* Moved error parsing logic to _utils

* Refactored OP error handling code

* Fixing a broken test

* Added utils for handling googleapiclient errors

* Added tests for new error handling logic

* Updated public API docs

* Fixing test for python3

* Cleaning up the error code lookup code

* Cleaning up the error parsing APIs

* Cleaned up error parsing logic; Updated docs

* Migrated the FCM IID APIs to the new error types

* Migrated custom token API to new error types

* Migrated create cookie API to new error types

* Improved error message computation

* Refactored the shared error handling code

* Fixing lint errors

* Renamed variable for clarity

* Introducing UserNotFoundError type (#309)

* Added UserNotFoundError type

* Fixed some lint errors

* Some formatting updates

* Updated docs and tests

* New error handling support in create/update/delete user APIs (#311)

* New error handling support in create/update/delete user APIs

* Fixing some lint errors

* Error handling improvements in email action link APIs (#312)

* New error handling support in create/update/delete user APIs

* Fixing some lint errors

* Error handling update in email action link APIs

* Project management API migrated to new error types (#314)

* Error handling updated for remaining user_mgt APIs (#315)

* Error handling updated for remaining user_mgt APIs

* Removed unused constants

* Migrated token verification APIs to new exception types (#317)

* Migrated token verification APIs to new error types

* Removed old AuthError type

* Added new exception types for revoked tokens

* Migrated the db module to the new exception types (#318)

* Migrating db module to new exception types

* Error handling for transactions

* Updated integration tests

* Restoring the old txn abort behavior

* Updated error type in snippet

* Added comment

* Adding a few overlooked error types (#319)

* Adding some missing error types

* Updated documentation

* Removing the ability to delete user properties by passing None (#320)

* Adding beginning of _MLKitService (#323)

* Adding beginning of _MLKitService

* Added License and Docstring

* Firebase ML Kit Get Model API implementation (#326)

* added GetModel
* Added tests for get_model

* Firebase ML Kit Delete Model API implementation (#327)

* implement delete model

* Firebase ML Kit List Models API implementation (#331)

* implemented list models plus tests

* Implementation of Model, ModelFormat, TFLiteModelSource and subclasses (#335)

* Implementation of Model, ModelFormat, ModelSource and subclasses

* Firebase ML Kit Create Model API implementation (#337)

* create model plus long running operation handling
* Model.wait_for_unlocked

* Firebase ML Kit Update Model API implementation (#343)

* Firebase ML Kit Create Model API implementation

* Firebase ML Kit Publish and Unpublish Implementation (#345)

* Firebase ML Kit Publish and Unpublish Implementation

* Firebase ML Kit TFLiteGCSModelSource.from_tflite_model implementation and conversion helpers (#346)

* Firebase ML Kit TFLiteGCSModelSource.from_tflite_model implementation
* support for tensorflow lite conversion helpers (version 1.x)

* Quick pass at filling in missing docstrings (#367)

* Quick pass at filling in missing docstrings

* More punctuation

* Modify Operation Handling to not require a name for Done Operations (#371)

* Firebase ML Kit Modify Operation Handling to not require a name for Done Operations
* Adding support for TensorFlow 2.x

* rename from mlkit to ml (#373)

* Adding File naming capability to from_saved_model and from_keras_model. (#375)

adding File naming capability for ModelSource

* Firebase ML Modify Operation Handling Code to match rpc codes not html codes (#390)

* Firebase ML Modify Operation Handling Code to match actual codes
* apply database fix too

* Mlkit fix date handling2 (#391)

* Fix create/update date handling
* Skip unrelated failing tests (until sync)

* Firebase Ml Fix upload file naming (#392)

* Fix File Naming

* Integration tests for Firebase ML (#394)

* Integration tests for Firebase ML

* Fixing lint errors for Py3 (#401)

* Fixing lint errors for Py3

* Removed dependency on six

* Fixing a couple of merge errors

* Modifying operation handling to support backend changes (#423)

* modifying operation handling to support backend changes

* Firebase ML Changing service endpoint (#421)

* Mlkit add headers (#445)

* add Headers

* fixed test (#448)

* Adding tensorflow and keras so we don't skip tests (#449)

* Adding tensorflow and keras so we don't skip tests
* Add additional instructions for integration tests for ml

Co-authored-by: Hiranya Jayathilaka <hiranya911@gmail.com>
Co-authored-by: Kevin Cheung <kevinthecheung@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants