示例#1
0
def update_model(model_id,
                 model_file=None,
                 name=None,
                 new_tags=None,
                 remove_tags=None):
    """Update one of the project's models."""
    model = ml.get_model(model_id)

    if model_file is not None:
        # Load a tflite file and upload it to Cloud Storage
        print('Uploading to Cloud Storage...')
        model_source = ml.TFLiteGCSModelSource.from_tflite_model_file(
            model_file)
        tflite_format = ml.TFLiteFormat(model_source=model_source)
        model.model_format = tflite_format

    if name is not None:
        model.display_name = name

    if new_tags is not None:
        model.tags = new_tags if model.tags is None else model.tags + new_tags

    if remove_tags is not None and model.tags is not None:
        model.tags = list(set(model.tags).difference(set(remove_tags)))

    updated_model = ml.update_model(model)
    ml.publish_model(updated_model.model_id)
示例#2
0
def test_publish_invalid_fails(firebase_model):
    assert firebase_model.validation_error is not None

    with pytest.raises(exceptions.FailedPreconditionError) as excinfo:
        ml.publish_model(firebase_model.model_id)
    check_operation_error(excinfo,
                          'Cannot publish a model that is not verified.')
示例#3
0
def publish_model_to_firebase(tflite_model_name, model_name):
    source = ml.TFLiteGCSModelSource.from_tflite_model_file(tflite_model_name)
    model_format = ml.TFLiteFormat(model_source=source)
    firebase_models = ml.list_models(
        list_filter="display_name = {0}".format(model_name)).iterate_all()
    for model in firebase_models:
        custom_model = model

    custom_model.model_format = model_format
    model_to_publish = ml.update_model(custom_model)
    ml.publish_model(model_to_publish.model_id)
示例#4
0
def test_publish_unpublish_non_existing_model(firebase_model):
    ml.delete_model(firebase_model.model_id)

    with pytest.raises(exceptions.NotFoundError) as excinfo:
        ml.publish_model(firebase_model.model_id)
    check_operation_error(
        excinfo,
        'Model \'{0}\' was not found'.format(firebase_model.as_dict().get('name')))

    with pytest.raises(exceptions.NotFoundError) as excinfo:
        ml.unpublish_model(firebase_model.model_id)
    check_operation_error(
        excinfo,
        'Model \'{0}\' was not found'.format(firebase_model.as_dict().get('name')))
示例#5
0
def add_automl_model(model_ref, name, tags=None):
    """Add an AutoML tflite model file to the project and publish it."""
    # Create the model object
    model_source = ml.TFLiteAutoMlSource(model_ref)
    model = ml.Model(display_name=name,
                     model_format=ml.TFLiteFormat(model_source=model_source))
    if tags is not None:
        model.tags = tags

    # Add the model to your Firebase project and publish it
    new_model = ml.create_model(model)
    new_model.wait_for_unlocked()
    ml.publish_model(new_model.model_id)

    print('Model uploaded and published:')
    print_models([new_model], headers=False)
示例#6
0
def test_publish_unpublish_model(firebase_model):
    assert firebase_model.published is False

    published_model = ml.publish_model(firebase_model.model_id)
    assert published_model.published is True

    unpublished_model = ml.unpublish_model(published_model.model_id)
    assert unpublished_model.published is False
示例#7
0
def upload_model(model_file, name, tags=None):
    """Upload a tflite model file to the project and publish it."""
    # Load a tflite file and upload it to Cloud Storage
    print('Uploading to Cloud Storage...')
    model_source = ml.TFLiteGCSModelSource.from_tflite_model_file(model_file)

    # Create the model object
    tflite_format = ml.TFLiteFormat(model_source=model_source)
    model = ml.Model(display_name=name, model_format=tflite_format)
    if tags is not None:
        model.tags = tags

    # Add the model to your Firebase project and publish it
    new_model = ml.create_model(model)
    ml.publish_model(new_model.model_id)

    print('Model uploaded and published:')
    print_models([new_model], headers=False)