import requests import json from typing import List, Dict, Any def call_chicory_parser(product_names: List[str]) -> Dict[str, Any]: """ Call the Chicory Parser V3 API to get ingredient predictions Args: product_names: List of product names to parse Returns: Dictionary mapping product names to their Chicory Parser results """ url = "https://prod-parserv3.chicoryapp.com/api/v3/prediction" # Prepare the payload items = [{"id": i, "text": name} for i, name in enumerate(product_names)] payload = json.dumps({"items": items}) # Set headers headers = { 'Content-Type': 'application/json' } try: response = requests.post(url, headers=headers, data=payload) response.raise_for_status() # Raise exception for HTTP errors # Parse the response results = response.json() # Create a dictionary mapping product names to results product_results = {} for result in results: product_name = result["input_text"] product_results[product_name] = result return product_results except requests.exceptions.RequestException as e: print(f"Error calling Chicory Parser API: {e}") return {} except json.JSONDecodeError: print(f"Error parsing Chicory API response: {response.text}") return {}