jeremyarancio's picture
Add picture
98db947
raw
history blame
1.59 kB
from typing import Dict, Tuple
import requests
from openfoodfacts.api import ProductResource, APIConfig
BASE_URL = "https://robotoff.openfoodfacts.org/api/v1/"
def next_annotation() -> Tuple[str, str, str, str]:
insight = import_random_insight()
return (
insight["id"],
insight["data"]["original"],
insight["data"]["correction"],
get_image_url(insight["barcode"])
)
def submit_correction(insight_id: str, original: str, correction: str) -> Tuple[str, str, str]:
# Not implemented yet
return next_annotation()
def import_random_insight(
insight_type: str = "ingredient_spellcheck",
count: int = 1,
predictor: str = "fine-tuned-mistral-7b",
) -> Dict:
url = f"{BASE_URL}/insights/random?count={count}&type={insight_type}&predictor={predictor}"
response = requests.get(url)
data = response.json()
insight = data["insights"][0]
return insight
def submit_to_product_opener(
insight_id: str,
skipped: bool,
update: int = 0,
) -> None:
url = f"{BASE_URL}/insights/annotate"
annotation = -1 if skipped else 1
data = {
"insight_id": insight_id,
"annotation": annotation,
"update": update,
}
requests.post(url, data=data)
def get_image_url(
code: str,
user_agent: str = "Spellcheck-Annotate",
) -> str:
fields = ["image_ingredients_url"]
data = ProductResource(
api_config=APIConfig(user_agent=user_agent)
).get(
code=code,
fields=fields,
)
image_url = data.get(fields[0])
return image_url