File size: 1,440 Bytes
c1ad412
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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 {}