File size: 5,813 Bytes
a318724
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
beeb862
 
18ec5e9
a318724
18ec5e9
 
 
 
cf30a67
 
18ec5e9
cf30a67
 
 
 
18ec5e9
 
 
 
 
ba66486
18ec5e9
beeb862
18ec5e9
 
 
 
 
 
 
 
 
 
 
beeb862
18ec5e9
a318724
 
 
ba66486
 
 
 
 
 
 
 
 
 
18ec5e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import pickle
import json
import os

class SafeProgress:
    """Wrapper for progress tracking that handles None gracefully"""
    def __init__(self, progress_obj=None):
        self.progress = progress_obj
    
    def __call__(self, value, desc=""):
        if self.progress is not None:
            try:
                self.progress(value, desc=desc)
            except:
                print(f"Progress {value}: {desc}")
        else:
            print(f"Progress {value}: {desc}")

def load_embeddings(embeddings_path):
    """Load ingredient embeddings from pickle file"""
    print(f"Loading ingredient embeddings from {embeddings_path}")
    with open(embeddings_path, "rb") as f:
        ingredients_embeddings = pickle.load(f)
    print(f"Loaded {len(ingredients_embeddings)} ingredient embeddings")
    return ingredients_embeddings

def parse_product_file(file_path):
    """Parse a file containing product data and extract product names"""
    try:
        with open(file_path, 'r') as f:
            try:
                products_data = json.load(f)
                if isinstance(products_data, list):
                    # Extract product names if it's a list of objects with 'name' field
                    if all(isinstance(item, dict) for item in products_data):
                        product_names = [item.get('name', '') for item in products_data if isinstance(item, dict)]
                    else:
                        # If it's just a list of strings
                        product_names = [str(item) for item in products_data if item]
                else:
                    # If it's just a list of product names
                    product_names = []
            except json.JSONDecodeError:
                # If not JSON, try reading as text file with one product per line
                f.seek(0)
                product_names = [line.strip() for line in f.readlines() if line.strip()]
    except Exception as e:
        raise Exception(f"Error reading file: {str(e)}")
    
    return product_names

def format_categories_html(product, similarities, chicory_result=None):
    """Format the similarities as HTML with bootstrap styling"""
    html = f"<div class='product-result'><h3 style='color: #fff;'>{product}</h3>"
    
    # Add Chicory results with enhanced styling
    if chicory_result:
        html += "<div class='result-section chicory-section' style='background-color: #1a3c6e; color: white; padding: 15px; border-radius: 5px; margin: 10px 0; box-shadow: 0 2px 4px rgba(0,0,0,0.1);'>"
        html += "<h4 style='margin-top: 0; border-bottom: 1px solid rgba(255,255,255,0.3); padding-bottom: 8px;'>Chicory Parser Results</h4>"
        
        if isinstance(chicory_result, dict):
            # Extract important fields with better formatting
            ingredient = chicory_result.get("ingredient", "Not found")
            confidence = chicory_result.get("confidence", 0)
            confidence_pct = int(confidence * 100) if confidence else 0
            
            html += f"<div style='display: flex; justify-content: space-between; align-items: center; background-color: rgba(255,255,255,0.1); padding: 10px; border-radius: 4px;'>"
            html += f"<span style='font-size: 1.1em;'>{ingredient}</span>"
            html += f"<span style='background-color: {get_confidence_bg_color(confidence)}; color: {get_confidence_text_color(confidence)}; padding: 4px 8px; border-radius: 12px; font-weight: bold;'>{confidence_pct}%</span>"
            html += "</div>"
        html += "</div>"
    
    # Add embedding similarities with matching styling
    if similarities:
        html += "<div class='result-section embedding-section' style='background-color: #263238; color: white; padding: 15px; border-radius: 5px; margin: 15px 0; box-shadow: 0 2px 4px rgba(0,0,0,0.1);'>"
        html += "<h4 style='margin-top: 0; border-bottom: 1px solid rgba(255,255,255,0.3); padding-bottom: 8px;'>Embedding Similarity</h4>"
        
        for i, (ingredient, score) in enumerate(similarities):
            confidence_pct = int(score * 100)
            html += f"<div style='display: flex; justify-content: space-between; align-items: center; padding: 8px; border-radius: 4px; margin: 4px 0; background-color: rgba(255,255,255,{0.07 + (i * 0.01)});'>"
            html += f"<span>{ingredient}</span>"
            html += f"<span style='background-color: {get_confidence_bg_color(score)}; color: {get_confidence_text_color(score)}; padding: 4px 8px; border-radius: 12px; font-weight: bold;'>{confidence_pct}%</span>"
            html += "</div>"
        
        html += "</div>"
    else:
        html += "<p style='color: #b0bec5; font-style: italic; padding: 10px; background-color: rgba(255,255,255,0.05); border-radius: 4px; margin: 10px 0;'>No similar ingredients found above the confidence threshold.</p>"
    
    html += "</div>"
    return html

def get_confidence_color(score):
    """Get color based on confidence score"""
    if score >= 0.8:
        return "#1a8a38"  # Strong green
    elif score >= 0.65:
        return "#4caf50"  # Medium green
    elif score >= 0.5:
        return "#8bc34a"  # Light green
    else:
        return "#9e9e9e"  # Gray

def get_confidence_bg_color(score):
    """Get background color for confidence badge based on score"""
    if score >= 0.8:
        return "#2e7d32"  # Dark green
    elif score >= 0.65:
        return "#558b2f"  # Medium green
    elif score >= 0.5:
        return "#9e9d24"  # Light green/yellow
    else:
        return "#757575"  # Gray

def get_confidence_text_color(score):
    """Get text color that's readable on the confidence background"""
    if score >= 0.5:
        return "#ffffff"  # White text on dark backgrounds
    else:
        return "#f5f5f5"  # Light gray on gray background