File size: 25,270 Bytes
9252f62 bfaa1e6 9252f62 bfaa1e6 9252f62 eb7aed5 68cd547 eb7aed5 9252f62 eb7aed5 9252f62 eb7aed5 9252f62 eb7aed5 9252f62 eb7aed5 9252f62 eb7aed5 9252f62 eb7aed5 9252f62 eb7aed5 9252f62 eb7aed5 9252f62 eb7aed5 9252f62 eb7aed5 9252f62 bfaa1e6 9252f62 eb7aed5 9252f62 eb7aed5 9252f62 eb7aed5 9252f62 bfaa1e6 eb7aed5 bfaa1e6 eb7aed5 bfaa1e6 eb7aed5 bfaa1e6 eb7aed5 bfaa1e6 eb7aed5 bfaa1e6 eb7aed5 bfaa1e6 eb7aed5 bfaa1e6 eb7aed5 bfaa1e6 eb7aed5 bfaa1e6 eb7aed5 bfaa1e6 eb7aed5 bfaa1e6 eb7aed5 bfaa1e6 eb7aed5 9252f62 eb7aed5 bfaa1e6 eb7aed5 9252f62 eb7aed5 9252f62 bfaa1e6 eb7aed5 bfaa1e6 eb7aed5 68cd547 bfaa1e6 eb7aed5 9252f62 eb7aed5 bfaa1e6 9252f62 eb7aed5 9252f62 eb7aed5 9252f62 bfaa1e6 9252f62 eb7aed5 9252f62 eb7aed5 9252f62 bfaa1e6 eb7aed5 9252f62 eb7aed5 9252f62 bfaa1e6 eb7aed5 9252f62 68cd547 |
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 |
import os
import subprocess
import sys
import re
import numpy as np
from PIL import Image
import gradio as gr
import requests
import json
from dotenv import load_dotenv
# Attempt to install pytesseract if not found
try:
import pytesseract
except ImportError:
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'pytesseract'])
import pytesseract
# AFTER importing pytesseract, then set the path
try:
# First try the default path
if os.path.exists('/usr/bin/tesseract'):
pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'
# Try to find it on the PATH
else:
tesseract_path = subprocess.check_output(['which', 'tesseract']).decode().strip()
if tesseract_path:
pytesseract.pytesseract.tesseract_cmd = tesseract_path
except:
# If all else fails, try the default installation path
pytesseract.pytesseract.tesseract_cmd = 'tesseract'
# Load environment variables
load_dotenv()
# API Keys
MISTRAL_API_KEY = "GlrVCBWyvTYjWGKl5jqtK4K41uWWJ79F"
META_LLAMA_API_KEY = "22068836-e455-47e7-8293-373f9e4c84fb" # Updated API key
# Meta LLaMA API for ingredient extraction from product names/images
def extract_ingredients_with_llama(image=None, product_name=None):
"""
Use Meta's LLaMA API to extract ingredients from a product image or name
"""
if not image and not product_name:
return "No product information provided. Please provide an image or product name."
# Prepare API call to Meta LLaMA
headers = {
"Authorization": f"Bearer {META_LLAMA_API_KEY}",
"Content-Type": "application/json"
}
# Create prompt based on what's provided
if image:
# Convert image to base64 for API
import base64
from io import BytesIO
buffered = BytesIO()
image.save(buffered, format="JPEG")
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
prompt = [
{"role": "system", "content": "You are an expert at identifying food products and their ingredients from images. Extract the product name and list all ingredients you can identify."},
{"role": "user", "content": [
{"type": "text", "text": "Look at this food product image and list all the ingredients it contains. If you can identify the product name, mention that first, then list all ingredients in a comma-separated format."},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{img_str}"}}
]}
]
else:
# Product name provided
prompt = [
{"role": "system", "content": "You are an expert at identifying food product ingredients. Your task is to list all common ingredients for the specified product."},
{"role": "user", "content": f"Please list all the common ingredients typically found in {product_name}. Provide the ingredients in a comma-separated format."}
]
# Call Meta LLaMA API
try:
data = {
"model": "meta-llama/Llama-3-8b-hf", # Use an appropriate model
"messages": prompt,
"temperature": 0.2, # Lower temperature for more factual responses
"max_tokens": 800
}
# Add logging for debugging
print(f"Sending request to LLaMA API with data structure: {json.dumps(data)[:300]}...")
response = requests.post(
"https://api.llama-api.com/chat/completions", # Replace with correct endpoint if different
headers=headers,
json=data,
timeout=30 # Add timeout to prevent hanging
)
if response.status_code == 200:
text_response = response.json()['choices'][0]['message']['content']
print(f"LLaMA API response received: {text_response[:100]}...")
# Extract ingredients from the response
# Look for a list of ingredients following common patterns
ingredients_section = re.search(r'ingredients:?\s*([^\.]+)', text_response, re.IGNORECASE)
if ingredients_section:
ingredients_text = ingredients_section.group(1)
else:
# If no explicit "ingredients:" section, try to identify comma-separated lists
# and take the longest one as it's likely to be the ingredients list
comma_lists = re.findall(r'([^\.;:]+(?:,\s*[^\.;:]+){2,})', text_response)
if comma_lists:
ingredients_text = max(comma_lists, key=len)
else:
ingredients_text = text_response # Use full response if no list found
# Parse the ingredients
ingredients = parse_ingredients(ingredients_text)
# Extract product name if possible
product_match = re.search(r'product(?:\s+name)?(?:\s+is)?:?\s*([^\.;,\n]+)', text_response, re.IGNORECASE)
if product_match:
product_name = product_match.group(1).strip()
return ingredients, product_name
return ingredients, None
else:
print(f"Error response from LLaMA API: {response.status_code} - {response.text}")
# Fall back to dummy analysis on error
return f"Error calling Meta LLaMA API: {response.status_code} - {response.text}", None
except Exception as e:
print(f"Exception in LLaMA API call: {str(e)}")
return f"Error extracting ingredients with LLaMA: {str(e)}", None
# Mistral API for ingredient analysis
def analyze_ingredients_with_mistral(ingredients_list, health_conditions=None, product_name=None):
"""
Use Mistral AI to analyze ingredients and provide health insights.
"""
if not ingredients_list or (isinstance(ingredients_list, list) and len(ingredients_list) == 0):
return "No ingredients detected or provided."
# Handle error messages that might have been passed
if isinstance(ingredients_list, str) and "Error" in ingredients_list:
# Fall back to dummy analysis if there was an error
return dummy_analyze(product_name if product_name else "Unknown product", health_conditions)
# Convert to string if it's a list
if isinstance(ingredients_list, list):
ingredients_text = ", ".join(ingredients_list)
else:
ingredients_text = ingredients_list
# Create a prompt for Mistral
product_info = f"Product Name: {product_name}\n" if product_name else ""
if health_conditions and health_conditions.strip():
prompt = f"""
{product_info}Analyze the following food ingredients for a person with these health conditions: {health_conditions}
Ingredients: {ingredients_text}
For each ingredient:
1. Provide its potential health benefits
2. Identify any potential risks
3. Note if it may affect the specified health conditions
Then provide an overall assessment of the product's suitability for someone with the specified health conditions.
Format your response in markdown with clear headings and sections.
"""
else:
prompt = f"""
{product_info}Analyze the following food ingredients:
Ingredients: {ingredients_text}
For each ingredient:
1. Provide its potential health benefits
2. Identify any potential risks or common allergens associated with it
Then provide an overall assessment of the product's general health profile.
Format your response in markdown with clear headings and sections.
"""
try:
headers = {
"Authorization": f"Bearer {MISTRAL_API_KEY}",
"Content-Type": "application/json"
}
data = {
"model": "mistral-small", # Or another suitable model
"messages": [{"role": "user", "content": prompt}],
"temperature": 0.7,
}
response = requests.post(
"https://api.mistral.ai/v1/chat/completions",
headers=headers,
json=data,
timeout=30
)
if response.status_code == 200:
analysis = response.json()['choices'][0]['message']['content']
else:
print(f"Error response from Mistral API: {response.status_code} - {response.text}")
return dummy_analyze(ingredients_list if isinstance(ingredients_list, list) else [ingredients_text], health_conditions) + f"\n\n(Using fallback analysis: Mistral API Error - {response.status_code} - {response.text})"
# Add disclaimer
disclaimer = """
## Disclaimer
This analysis is provided for informational purposes only and should not replace professional medical advice.
Always consult with a healthcare provider regarding dietary restrictions, allergies, or health conditions.
"""
return analysis + disclaimer
except Exception as e:
print(f"Exception in Mistral API call: {str(e)}")
# Fallback to basic analysis if API call fails
return dummy_analyze(ingredients_list if isinstance(ingredients_list, list) else [ingredients_text], health_conditions) + f"\n\n(Using fallback analysis: {str(e)})"
# Dummy analysis function for when API is not available
def dummy_analyze(ingredients_list, health_conditions=None):
if isinstance(ingredients_list, str):
ingredients_text = ingredients_list
else:
ingredients_text = ", ".join(ingredients_list)
report = f"""
# Ingredient Analysis Report
## Detected Ingredients
{", ".join([i.title() for i in ingredients_list]) if isinstance(ingredients_list, list) else ingredients_text}
## Overview
This is a simulated analysis since the API call failed. In the actual application,
the ingredients would be analyzed by an AI model for their health implications.
## Health Considerations
"""
if health_conditions:
report += f"""
The analysis would specifically consider these health concerns: {health_conditions}
"""
else:
report += """
No specific health concerns were provided, so a general analysis would be performed.
"""
report += """
## Disclaimer
This analysis is provided for informational purposes only and should not replace professional medical advice.
Always consult with a healthcare provider regarding dietary restrictions, allergies, or health conditions.
"""
return report
# Function to extract text from images using OCR
def extract_text_from_image(image):
try:
if image is None:
return "No image captured. Please try again."
# Verify Tesseract executable is accessible
try:
subprocess.run([pytesseract.pytesseract.tesseract_cmd, "--version"],
check=True, capture_output=True, text=True)
except (subprocess.SubprocessError, FileNotFoundError):
return "Tesseract OCR is not installed or not properly configured. Please check installation."
# Import necessary libraries
import cv2
import numpy as np
from PIL import Image, ImageOps, ImageEnhance
# First approach: Invert the image for light text on dark background
inverted_image = ImageOps.invert(image)
# Try OCR on inverted image
custom_config = r'--oem 3 --psm 6 -l eng --dpi 300'
inverted_text = pytesseract.image_to_string(inverted_image, config=custom_config)
# Second approach: OpenCV processing for colored backgrounds
img_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
# Convert to grayscale
gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
# Apply bilateral filter to preserve edges while reducing noise
filtered = cv2.bilateralFilter(gray, 11, 17, 17)
# Adaptive thresholding to handle varied lighting
thresh = cv2.adaptiveThreshold(filtered, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 11, 2)
# Invert the image (if text is light on dark background)
inverted_thresh = cv2.bitwise_not(thresh)
# Try OCR on processed image
cv_text = pytesseract.image_to_string(
Image.fromarray(inverted_thresh),
config=custom_config
)
# Third approach: Color filtering to isolate text from colored background
# Convert to HSV color space to better isolate colors
hsv = cv2.cvtColor(img_cv, cv2.COLOR_BGR2HSV)
# Create a mask to extract light colored text (assuming white/light text)
lower_white = np.array([0, 0, 150])
upper_white = np.array([180, 30, 255])
mask = cv2.inRange(hsv, lower_white, upper_white)
# Apply morphological operations to clean up the mask
kernel = np.ones((2, 2), np.uint8)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
# Improve character connectivity
mask = cv2.dilate(mask, kernel, iterations=1)
# Try OCR on color filtered image
color_text = pytesseract.image_to_string(
Image.fromarray(mask),
config=r'--oem 3 --psm 6 -l eng --dpi 300'
)
# Fourth approach: Try directly with the image but with different configs
direct_text = pytesseract.image_to_string(
image,
config=r'--oem 3 --psm 11 -l eng --dpi 300'
)
# Compare results and select the best one
results = [inverted_text, cv_text, color_text, direct_text]
# Select the result with the most alphanumeric characters
def count_alphanumeric(text):
return sum(c.isalnum() for c in text)
best_text = max(results, key=count_alphanumeric)
# If still poor results, try with explicit text color inversion in tesseract
if count_alphanumeric(best_text) < 20:
# Try with tesseract's built-in inversion
neg_text = pytesseract.image_to_string(
image,
config=r'--oem 3 --psm 6 -c textord_heavy_nr=1 -c textord_debug_printable=0 -l eng --dpi 300'
)
if count_alphanumeric(neg_text) > count_alphanumeric(best_text):
best_text = neg_text
# Clean up the text
best_text = re.sub(r'[^\w\s,;:%.()\n\'-]', '', best_text)
best_text = best_text.replace('\n\n', '\n')
# Special case for ingredients list format
if "ingredient" in best_text.lower() or any(x in best_text.lower() for x in ["sugar", "cocoa", "milk", "contain"]):
# Specific cleaning for ingredient lists
best_text = re.sub(r'([a-z])([A-Z])', r'\1 \2', best_text) # Add space between lowercase and uppercase
best_text = re.sub(r'(\d+)([a-zA-Z])', r'\1 \2', best_text) # Add space between number and letter
if not best_text.strip():
return "No text could be extracted. Ensure image is clear and readable."
return best_text.strip()
except Exception as e:
return f"Error extracting text: {str(e)}"
# Function to parse ingredients from text
def parse_ingredients(text):
if not text:
return []
# Clean up the text
text = re.sub(r'^ingredients:?\s*', '', text.lower(), flags=re.IGNORECASE)
# Remove common OCR errors and extraneous characters
text = re.sub(r'[|\\/@#$%^&*()_+=]', '', text)
# Replace common OCR errors
text = re.sub(r'\bngredients\b', 'ingredients', text)
# Handle common OCR misreads
replacements = {
'0': 'o', 'l': 'i', '1': 'i',
'5': 's', '8': 'b', 'Q': 'g',
}
for error, correction in replacements.items():
text = text.replace(error, correction)
# Split by common ingredient separators
ingredients = re.split(r',|;|\n', text)
# Clean up each ingredient
cleaned_ingredients = []
for i in ingredients:
i = i.strip().lower()
if i and len(i) > 1: # Ignore single characters which are likely OCR errors
cleaned_ingredients.append(i)
return cleaned_ingredients
# Function to process input based on method (camera, product photo, or product name)
def process_input(input_method, product_name, camera_input, product_photo, health_conditions):
if input_method == "Product Photo":
if product_photo is not None:
# Use Meta LLaMA to extract ingredients from product photo
ingredients, detected_product = extract_ingredients_with_llama(image=product_photo)
# If error occurred, use fallback analysis
if isinstance(ingredients, str) and "Error" in ingredients:
print(f"LLaMA API error, using fallback: {ingredients}")
return f"Error extracting ingredients. Using fallback analysis.\n\n{dummy_analyze('Unknown food product', health_conditions)}"
# Add product name info if detected
product_info = ""
if detected_product:
product_info = f"## Product: {detected_product}\n\n"
# Analyze ingredients
analysis = analyze_ingredients_with_mistral(ingredients, health_conditions, detected_product)
return product_info + analysis
else:
return "No product image captured. Please try again."
elif input_method == "Product Name":
if product_name and product_name.strip():
# Use Meta LLaMA to extract ingredients based on product name
ingredients, _ = extract_ingredients_with_llama(product_name=product_name)
# If error occurred, use fallback analysis
if isinstance(ingredients, str) and "Error" in ingredients:
print(f"LLaMA API error, using fallback: {ingredients}")
return f"Error extracting ingredients. Using fallback analysis.\n\n{dummy_analyze(product_name, health_conditions)}"
# Analyze ingredients
return analyze_ingredients_with_mistral(ingredients, health_conditions, product_name)
else:
return "No product name entered. Please try again."
elif input_method == "Camera (Ingredients Label)":
if camera_input is not None:
extracted_text = extract_text_from_image(camera_input)
# If OCR fails, try using Meta LLaMA API as backup
if "Error" in extracted_text or "No text could be extracted" in extracted_text:
print(f"OCR failed, trying LLaMA API backup: {extracted_text}")
ingredients, detected_product = extract_ingredients_with_llama(image=camera_input)
if isinstance(ingredients, str) and "Error" in ingredients:
return f"Could not extract ingredients from image. Using fallback analysis.\n\n{dummy_analyze('Unknown food product', health_conditions)}"
product_info = ""
if detected_product:
product_info = f"## Product: {detected_product}\n\n"
analysis = analyze_ingredients_with_mistral(ingredients, health_conditions, detected_product)
return product_info + "Ingredients extracted using AI image analysis.\n\n" + analysis
# If OCR succeeded, parse ingredients normally
ingredients = parse_ingredients(extracted_text)
return analyze_ingredients_with_mistral(ingredients, health_conditions)
else:
return "No camera image captured. Please try again."
return "Please provide input using one of the available methods."
# Create the Gradio interface
with gr.Blocks(title="AI Ingredient Scanner") as app:
gr.Markdown("# AI Ingredient Scanner")
gr.Markdown("Analyze product ingredients for health benefits, risks, and potential allergens. Just take a photo of the product or enter its name!")
with gr.Row():
with gr.Column():
input_method = gr.Radio(
["Product Photo", "Product Name", "Camera (Ingredients Label)"],
label="Input Method",
value="Product Photo",
info="Choose how you want to identify the product"
)
# Product name input
product_name = gr.Textbox(
label="Enter product name",
placeholder="e.g., Coca-Cola, Oreo Cookies, Lay's Potato Chips",
visible=False
)
# Product photo capture
product_photo = gr.Image(label="Take a photo of the product", type="pil", visible=True)
# Camera input for ingredients label (original functionality)
camera_input = gr.Image(label="Capture ingredients label with camera", type="pil", visible=False)
# Health conditions input - now optional and more flexible
health_conditions = gr.Textbox(
label="Enter your health concerns (optional)",
placeholder="diabetes, high blood pressure, peanut allergy, etc.",
lines=2,
info="The AI will automatically analyze ingredients for these conditions"
)
analyze_button = gr.Button("Analyze Product")
with gr.Column():
output = gr.Markdown(label="Analysis Results")
extracted_info = gr.Textbox(label="Extracted Information (for verification)", lines=3)
# Show/hide inputs based on selection
def update_visible_inputs(choice):
return {
product_photo: gr.update(visible=(choice == "Product Photo")),
product_name: gr.update(visible=(choice == "Product Name")),
camera_input: gr.update(visible=(choice == "Camera (Ingredients Label)")),
}
input_method.change(update_visible_inputs, input_method, [product_photo, product_name, camera_input])
# Display extracted information (for verification purposes)
def show_extracted_info(input_method, product_name, camera_input, product_photo):
if input_method == "Product Photo" and product_photo is not None:
ingredients, product = extract_ingredients_with_llama(image=product_photo)
if isinstance(ingredients, list):
return f"Product: {product if product else 'Unknown'}\nIngredients: {', '.join(ingredients)}"
else:
return ingredients
elif input_method == "Product Name" and product_name:
ingredients, _ = extract_ingredients_with_llama(product_name=product_name)
if isinstance(ingredients, list):
return f"Product: {product_name}\nIngredients: {', '.join(ingredients)}"
else:
return ingredients
elif input_method == "Camera (Ingredients Label)" and camera_input is not None:
extracted_text = extract_text_from_image(camera_input)
return extracted_text
return "No input detected"
# Set up event handlers
analyze_button.click(
fn=process_input,
inputs=[input_method, product_name, camera_input, product_photo, health_conditions],
outputs=output
)
analyze_button.click(
fn=show_extracted_info,
inputs=[input_method, product_name, camera_input, product_photo],
outputs=extracted_info
)
gr.Markdown("### How to use")
gr.Markdown("""
1. Choose your input method:
- **Product Photo**: Take a photo of the entire product (front, back, or packaging)
- **Product Name**: Simply enter the name of the product
- **Camera (Ingredients Label)**: Traditional method - take a photo of the ingredients list
2. Optionally enter your health concerns
3. Click "Analyze Product" to get your personalized analysis
The AI will automatically detect the product, extract its ingredients, and analyze them.
""")
gr.Markdown("### Examples of what you can ask")
gr.Markdown("""
The system can handle a wide range of health concerns, such as:
- General health goals: "trying to reduce sugar intake" or "watching sodium levels"
- Medical conditions: "diabetes" or "hypertension"
- Allergies: "peanut allergy" or "shellfish allergy"
- Dietary restrictions: "vegetarian" or "gluten-free diet"
- Multiple conditions: "diabetes, high cholesterol, and lactose intolerance"
The AI will tailor its analysis to your specific needs.
""")
gr.Markdown("### Tips for best results")
gr.Markdown("""
- Hold the camera steady and ensure good lighting
- For Product Photo: Capture the entire product package clearly
- For Product Name: Be specific (e.g., "Honey Nut Cheerios" instead of just "Cheerios")
- For Ingredients Label: Focus directly on the ingredients list text
- Be specific about your health concerns for more targeted analysis
""")
gr.Markdown("### Disclaimer")
gr.Markdown("""
This tool is for informational purposes only and should not replace professional medical advice.
Always consult with a healthcare provider regarding dietary restrictions, allergies, or health conditions.
""")
# Launch the app
if __name__ == "__main__":
app.launch() |