import tarfile
import shutil


def tf_model_to_tar(tf_model, run_id: int, ):
    """
    Saves tensorflow model as compressed file

    :param run_id: current Metaflow run id
    :param tf_model: tensorflow model
    :return:
    """

    model_name = "intent-model-{}/1".format(run_id)
    local_tar_name = 'model-{}.tar.gz'.format(run_id)

    # save model locally
    tf_model.save(filepath=model_name)
    # save model as .tar.gz
    with tarfile.open(local_tar_name, mode="w:gz") as _tar:
        _tar.add(model_name, recursive=True)
    # remove local model
    shutil.rmtree(model_name.split('/')[0])

    return local_tar_name