Skip to content

Commit

Permalink
feat: add ingredient origin extraction
Browse files Browse the repository at this point in the history
Adapted from PR by Pykrom:
#890
  • Loading branch information
raphael0202 committed Oct 12, 2022
1 parent 65491c1 commit 04b79b8
Show file tree
Hide file tree
Showing 6 changed files with 721 additions and 0 deletions.
1 change: 1 addition & 0 deletions data/taxonomies/countries.json

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions robotoff/app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,41 @@ def on_get(self, req: falcon.Request, resp: falcon.Response):
resp.media = {"nutrients": [p.to_dict() for p in predictions]}


class OriginPredictorResource:
def on_get(self, req: falcon.Request, resp: falcon.Response):
ocr_url = req.get_param("ocr_url", required=True)

if not ocr_url.endswith(".json"):
raise falcon.HTTPBadRequest("a JSON file is expected")

barcode = get_barcode_from_url(ocr_url)

if barcode is None:
raise falcon.HTTPBadRequest(f"invalid OCR URL: {ocr_url}")

try:
predictions = extract_ocr_predictions(
barcode, ocr_url, [PredictionType.origin]
)

except requests.exceptions.RequestException:
resp.media = {
"error": "download_error",
"error_description": "an error occurred during OCR JSON download",
}
return

except OCRParsingException as e:
logger.error(e)
resp.media = {
"error": "invalid_ocr",
"error_description": "an error occurred during OCR parsing",
}
return

resp.media = {"origin": [p.to_dict() for p in predictions]}


class OCRInsightsPredictorResource:
def on_get(self, req: falcon.Request, resp: falcon.Response):
ocr_url = req.get_param("ocr_url", required=True)
Expand Down
2 changes: 2 additions & 0 deletions robotoff/prediction/ocr/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .label import find_labels
from .location import find_locations
from .nutrient import find_nutrient_mentions, find_nutrient_values
from .origin import origin_parser
from .packager_code import find_packager_codes
from .packaging import find_packaging
from .product_weight import find_product_weight
Expand All @@ -43,6 +44,7 @@
PredictionType.trace: find_traces,
PredictionType.nutrient: find_nutrient_values,
PredictionType.nutrient_mention: find_nutrient_mentions,
PredictionType.origin: origin_parser.find_origin,
PredictionType.brand: find_brands,
PredictionType.store: find_stores,
PredictionType.packaging: find_packaging,
Expand Down
Loading

0 comments on commit 04b79b8

Please sign in to comment.