def image_to_attributes()

in experiments/google/cloud/ml/applied/images/image_to_text.py [0:0]


def image_to_attributes(req: m.ImageRequest) -> m.ProductAttributes:
    prompt = """Provide the list of all the product attributes for the main
    product in the image in JSON format"""

    if req.image.startswith("gs://"):
        im = from_gsc_uri(req.image)
    else:
        im = from_url(req.image)

    responses = content_generation(prompt, im)
    res = responses.text
    res = res.replace("```", "")
    res = res.replace("json", "")

    # This produces multiple models, NOT USEFUL for API calls.
    # had to create a parser to return consistent values

    attributes_json = json.loads(res.strip())
    print(attributes_json)

    if "product_attributes" not in attributes_json:
        print("parsing")
        response = m.parse_project_attributes_from_dict(attributes_json)
    else:
        # response = m.parse_list_to_dict(attributes_json.get('product_attributes'))
        response = m.parse_project_attributes_from_dict(
            attributes_json.get("product_attributes")
        )

    return response