<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def create_image_uri(region, framework, instance_type, framework_version, py_version=None, account='520713654638', accelerator_type=None, optimized_families=None):
 """Return the ECR URI of an image. Args: region (str):
 AWS region where the image is uploaded. framework (str):
 framework used by the image. instance_type (str):
 SageMaker instance type. Used to determine device type (cpu/gpu/family-specific optimized). framework_version (str):
 The version of the framework. py_version (str):
 Optional. Python version. If specified, should be one of 'py2' or 'py3'. If not specified, image uri will not include a python component. account (str):
 AWS account that contains the image. (default: '520713654638') accelerator_type (str):
 SageMaker Elastic Inference accelerator type. optimized_families (str):
 Instance families for which there exist specific optimized images. Returns: str: The appropriate image URI based on the given parameters. """

    optimized_families = optimized_families or []

    if py_version and py_version not in VALID_PY_VERSIONS:
        raise ValueError('invalid py_version argument: {}'.format(py_version))

    # Handle Account Number for Gov Cloud
    account = VALID_ACCOUNTS_BY_REGION.get(region, account)

    # Handle Local Mode
    if instance_type.startswith('local'):
        device_type = 'cpu' if instance_type == 'local' else 'gpu'
    elif not instance_type.startswith('ml.'):
        raise ValueError('{} is not a valid SageMaker instance type. See: '
                         'https://aws.amazon.com/sagemaker/pricing/instance-types/'.format(instance_type))
    else:
        family = instance_type.split('.')[1]

        # For some frameworks, we have optimized images for specific families, e.g c5 or p3. In those cases,
        # we use the family name in the image tag. In other cases, we use 'cpu' or 'gpu'.
        if family in optimized_families:
            device_type = family
        elif family[0] in ['g', 'p']:
            device_type = 'gpu'
        else:
            device_type = 'cpu'

    if py_version:
        tag = "{}-{}-{}".format(framework_version, device_type, py_version)
    else:
        tag = "{}-{}".format(framework_version, device_type)

    if _accelerator_type_valid_for_framework(framework=framework, accelerator_type=accelerator_type,
                                             optimized_families=optimized_families):
        framework += '-eia'

    return "{}/sagemaker-{}:{}" \
        .format(get_ecr_image_uri_prefix(account, region), framework, tag)