Skip to content

Commit

Permalink
feat: add Robotoff resource
Browse files Browse the repository at this point in the history
As first method, add the predict_lang method
  • Loading branch information
raphael0202 committed Apr 18, 2024
1 parent 29c40ad commit 953ae41
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions openfoodfacts/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ def send_form_urlencoded_post_request(
return r


class RobotoffResource:
def __init__(self, api_config: APIConfig):
self.api_config = api_config
self.base_url = URLBuilder.robotoff(environment=api_config.environment)

def predict_lang(self, text: str, k: int = 10, threshold: float = 0.01) -> JSONType:
"""Predict the language of a text.
:param text: the text to predict the language of
:param k: the number of predictions to return, defaults to 10
:param threshold: the minimum probability for a prediction to be
returned, defaults to 0.01
:return: the API response
"""
return http_session.post(
url=f"{self.base_url}/api/v1/predict/lang",
data={"text": text, "k": k, "threshold": threshold},
)


class FacetResource:
def __init__(self, api_config: APIConfig):
self.api_config = api_config
Expand Down Expand Up @@ -339,6 +359,7 @@ def __init__(
self.country = country
self.product = ProductResource(self.api_config)
self.facet = FacetResource(self.api_config)
self.robotoff = RobotoffResource(self.api_config)


def parse_ingredients(text: str, lang: str, api_config: APIConfig) -> list[JSONType]:
Expand Down

0 comments on commit 953ae41

Please sign in to comment.