Spaces:
Sleeping
Sleeping
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>{product}</h3>" | |
# Add Chicory results with explicit visibility classes | |
if chicory_result: | |
html += "<div class='chicory-result' style='visibility: visible; display: block;'>" | |
html += "<h4>Chicory Parser Results:</h4>" | |
html += "<ul>" | |
for ingredient in chicory_result: | |
html += f"<li>{ingredient}</li>" | |
html += "</ul></div>" | |
# Add similarities | |
if similarities: | |
html += "<h4>Similar Ingredients:</h4>" | |
html += "<table style='width: 100%; border-collapse: collapse;'>" | |
html += "<tr><th style='text-align: left; padding: 8px;'>Ingredient</th><th style='text-align: right; padding: 8px;'>Confidence</th></tr>" | |
for ingredient, score in similarities: | |
# Format score as percentage with color gradient based on confidence | |
confidence = int(score * 100) | |
color = f"hsl({min(confidence, 100) * 1.2}, 70%, 45%)" | |
html += f"<tr><td style='padding: 8px;'>{ingredient}</td>" | |
html += f"<td style='text-align: right; padding: 8px;'><span style='color: {color}; font-weight: bold;'>{confidence}%</span></td></tr>" | |
html += "</table>" | |
else: | |
html += "<p>No similar ingredients found.</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 |