def on_get(self, req, resp): ocr_url = req.get_param('ocr_url', required=True) try: insights = extract_ocr_insights(ocr_url, [InsightType.nutrient.name]) 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 if not insights: resp.media = { 'nutrients': {}, } else: resp.media = {'nutrients': insights['nutrient'][0]['nutrients']}
def on_get(self, req, resp): ocr_url = req.get_param("ocr_url", required=True) try: insights = extract_ocr_insights(ocr_url, DEFAULT_INSIGHT_TYPES) 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 = { "insights": insights, }
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") try: insights = extract_ocr_insights(ocr_url, [InsightType.nutrient]) 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 if not insights: resp.media = { "nutrients": {}, } else: nutrient_insights = insights[InsightType.nutrient] resp.media = nutrient_insights.to_dict()
def predict_insight(ocr_url: str) -> None: import json from robotoff.insights.extraction import DEFAULT_INSIGHT_TYPES, extract_ocr_insights from robotoff.utils import get_logger get_logger() results = extract_ocr_insights(ocr_url, DEFAULT_INSIGHT_TYPES) print(json.dumps(results, indent=4))