Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add off sdk #1151

Merged
merged 2 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
835 changes: 429 additions & 406 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ lark = "~1.1.4"
h5py = "~3.8.0"
opencv-contrib-python = "~4.7.0.72"
toml = "~0.10.2"
openfoodfacts = "~0.1.5"

[tool.poetry.dependencies.sentry-sdk]
version = "~1.14.0"
Expand Down
2 changes: 1 addition & 1 deletion robotoff/insights/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ def add_optional_fields(cls, insight: ProductInsight, product: Optional[Product]
campaigns = []
if (
insight.value_tag in taxonomy
and "agribalyse_food_code" in taxonomy[insight.value_tag].additional_data
and "agribalyse_food_code" in taxonomy[insight.value_tag].properties
):
# This category is linked to an agribalyse category, add it as a
# campaign tag
Expand Down
36 changes: 1 addition & 35 deletions robotoff/off.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from urllib.parse import urlparse

import requests
from openfoodfacts.images import split_barcode
from requests.exceptions import JSONDecodeError

from robotoff import settings
Expand Down Expand Up @@ -95,41 +96,6 @@ def get_barcode_from_path(path: str) -> Optional[str]:
return barcode or None


BARCODE_PATH_REGEX = re.compile(r"^(...)(...)(...)(.*)$")


def split_barcode(barcode: str) -> list[str]:
"""Split barcode in the same way as done by Product Opener to generate a
product image folder.

:param barcode: The barcode of the product. For the pro platform only,
it must be prefixed with the org ID using the format
`{ORG_ID}/{BARCODE}`
:raises ValueError: raise a ValueError if `barcode` is invalid
:return: a list containing the splitted barcode
"""
org_id = None
if "/" in barcode:
# For the pro platform, `barcode` is expected to be in the format
# `{ORG_ID}/{BARCODE}` (ex: `org-lea-nature/3307130803004`)
org_id, barcode = barcode.split("/", maxsplit=1)

if not barcode.isdigit():
raise ValueError(f"unknown barcode format: {barcode}")

match = BARCODE_PATH_REGEX.fullmatch(barcode)

splits = [x for x in match.groups() if x] if match else [barcode]

if org_id is not None:
# For the pro platform only, images and OCRs belonging to an org
# are stored in a folder named after the org for all its products, ex:
# https://images.pro.openfoodfacts.org/images/products/org-lea-nature/330/713/080/3004/1.jpg
splits.append(org_id)

return splits


def _generate_file_path(product_id: ProductIdentifier, image_id: str, suffix: str):
splitted_barcode = split_barcode(product_id.barcode)
return f"/{'/'.join(splitted_barcode)}/{image_id}{suffix}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import numpy as np

from robotoff import settings
from robotoff.taxonomy import Taxonomy, fetch_taxonomy
from robotoff.taxonomy import Taxonomy
from robotoff.types import JSONType
from robotoff.utils.text import KeywordProcessor

Expand All @@ -35,11 +35,7 @@

@functools.cache
def get_ingredient_taxonomy():
return fetch_taxonomy(
"",
V3_MODEL_DATA_DIR / "ingredients.full.json.gz",
offline=True,
)
return Taxonomy.from_path(V3_MODEL_DATA_DIR / "ingredients.full.json.gz")


@functools.cache
Expand Down
Loading
Loading