def save_onnx_model(model, filename=None):
    """
    Saves a model as a file or bytes.

    :param model: *ONNX* model
    :param filename: filename or None to return bytes
    :return: bytes
    """
    content = model.SerializeToString()
    if filename is not None:
        if hasattr(filename, 'write'):
            filename.write(content)
        else:
            with open(filename, "wb") as f:
                f.write(content)
    return content