Пример #1
0
def generate_nutrition_image_insights():
    logger.info("Starting nutrition image insight generation")
    logger.info("Deleting previous nutrition image insights...")
    deleted = (ProductInsight.delete().where(
        ProductInsight.annotation.is_null(),
        ProductInsight.type == InsightType.nutrition_image.name,
        ProductInsight.server_domain == settings.OFF_SERVER_DOMAIN,
    ).execute())
    logger.info("{} insights deleted".format(deleted))
    product_store: DBProductStore = get_product_store()
    added = 0
    seen_set: Set[str] = set()

    latent_insight: ProductInsight
    for latent_insight in (ProductInsight.select().where(
            ProductInsight.type == InsightType.nutrient_mention.name).order_by(
                ProductInsight.source_image.desc()).iterator()):
        barcode = latent_insight.barcode

        if barcode in seen_set:
            continue

        mentions = latent_insight.data["mentions"]
        nutrition_image_langs = find_nutrition_image_lang(mentions)

        if not nutrition_image_langs:
            continue

        image_id = get_image_id(latent_insight.source_image)
        rotation = get_image_orientation(barcode, image_id)

        if rotation is None:
            continue

        product = product_store.get_product(barcode, ["images"])

        if product is None:
            continue

        images = product.get("images", {})

        if not has_nutrition_image(images):
            for lang in nutrition_image_langs:
                if not (ProductInsight.select().where(
                        ProductInsight.type
                        == InsightType.nutrition_image.name,
                        ProductInsight.barcode == barcode,
                        ProductInsight.value_tag == lang,
                        ProductInsight.server_domain
                        == settings.OFF_SERVER_DOMAIN,
                ).count()):
                    ProductInsight.create_from_latent(
                        latent_insight,
                        type=InsightType.nutrition_image.name,
                        value_tag=lang,
                        data={
                            "from_latent": str(latent_insight.id),
                            "languages": nutrition_image_langs,
                            "rotation": rotation or None,
                        },
                        id=str(uuid.uuid4()),
                    )
                    added += 1

    logger.info("Added: {}".format(added))
Пример #2
0
    timestamp = peewee.DateTimeField(null=True)
    value_tag = peewee.TextField(null=True, index=True)
    value = peewee.TextField(null=True, index=True)
    source_image = peewee.TextField(null=True, index=True)
    server_domain = peewee.TextField(
        null=True, help_text="server domain linked to the insight", index=True)
    server_type = peewee.CharField(
        null=True,
        max_length=10,
        help_text="project associated with the server_domain, "
        "one of 'off', 'obf', 'opff', 'opf'",
        index=True,
    )


insight_types = [
    InsightType.image_lang.name,
    InsightType.image_orientation.name,
    InsightType.nutrient_mention.name,
    InsightType.nutrient.name,
]

moved = 0

for insight in tqdm.tqdm(LatentProductInsight.select().where(
        LatentProductInsight.type.in_(insight_types)).iterator()):
    ProductInsight.create_from_latent(insight)
    moved += 1

print("{} insights moved".format(moved))