| | 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 |
| |
|
| | |
| | try: |
| | import pytesseract |
| | except ImportError: |
| | subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'pytesseract']) |
| | import pytesseract |
| |
|
| | |
| | try: |
| | |
| | if os.path.exists('/usr/bin/tesseract'): |
| | pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract' |
| | |
| | else: |
| | tesseract_path = subprocess.check_output(['which', 'tesseract']).decode().strip() |
| | if tesseract_path: |
| | pytesseract.pytesseract.tesseract_cmd = tesseract_path |
| | except: |
| | |
| | pytesseract.pytesseract.tesseract_cmd = 'tesseract' |
| |
|
| | |
| | load_dotenv() |
| |
|
| | |
| | MISTRAL_API_KEY = "GlrVCBWyvTYjWGKl5jqtK4K41uWWJ79F" |
| | META_LLAMA_API_KEY = "22068836-e455-47e7-8293-373f9e4c84fb" |
| |
|
| | |
| | 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." |
| |
|
| | |
| | headers = { |
| | "Authorization": f"Bearer {META_LLAMA_API_KEY}", |
| | "Content-Type": "application/json" |
| | } |
| |
|
| | |
| | if image: |
| | |
| | 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: |
| | |
| | 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."} |
| | ] |
| |
|
| | |
| | try: |
| | data = { |
| | "model": "meta-llama/Llama-3-8b-hf", |
| | "messages": prompt, |
| | "temperature": 0.2, |
| | "max_tokens": 800 |
| | } |
| |
|
| | |
| | print(f"Sending request to LLaMA API with data structure: {json.dumps(data)[:300]}...") |
| | |
| | response = requests.post( |
| | "https://api.llama-api.com/chat/completions", |
| | headers=headers, |
| | json=data, |
| | timeout=30 |
| | ) |
| |
|
| | if response.status_code == 200: |
| | text_response = response.json()['choices'][0]['message']['content'] |
| | print(f"LLaMA API response received: {text_response[:100]}...") |
| |
|
| | |
| | |
| | ingredients_section = re.search(r'ingredients:?\s*([^\.]+)', text_response, re.IGNORECASE) |
| | if ingredients_section: |
| | ingredients_text = ingredients_section.group(1) |
| | else: |
| | |
| | |
| | comma_lists = re.findall(r'([^\.;:]+(?:,\s*[^\.;:]+){2,})', text_response) |
| | if comma_lists: |
| | ingredients_text = max(comma_lists, key=len) |
| | else: |
| | ingredients_text = text_response |
| |
|
| | |
| | ingredients = parse_ingredients(ingredients_text) |
| |
|
| | |
| | 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}") |
| | |
| | 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 |
| |
|
| | |
| | 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." |
| |
|
| | |
| | if isinstance(ingredients_list, str) and "Error" in ingredients_list: |
| | |
| | return dummy_analyze(product_name if product_name else "Unknown product", health_conditions) |
| |
|
| | |
| | if isinstance(ingredients_list, list): |
| | ingredients_text = ", ".join(ingredients_list) |
| | else: |
| | ingredients_text = ingredients_list |
| |
|
| | |
| | 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", |
| | "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})" |
| |
|
| | |
| | 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)}") |
| | |
| | return dummy_analyze(ingredients_list if isinstance(ingredients_list, list) else [ingredients_text], health_conditions) + f"\n\n(Using fallback analysis: {str(e)})" |
| |
|
| |
|
| | |
| | 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 |
| |
|
| | |
| | def extract_text_from_image(image): |
| | try: |
| | if image is None: |
| | return "No image captured. Please try again." |
| |
|
| | |
| | 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 cv2 |
| | import numpy as np |
| | from PIL import Image, ImageOps, ImageEnhance |
| |
|
| | |
| | inverted_image = ImageOps.invert(image) |
| |
|
| | |
| | custom_config = r'--oem 3 --psm 6 -l eng --dpi 300' |
| | inverted_text = pytesseract.image_to_string(inverted_image, config=custom_config) |
| |
|
| | |
| | img_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR) |
| |
|
| | |
| | gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY) |
| |
|
| | |
| | filtered = cv2.bilateralFilter(gray, 11, 17, 17) |
| |
|
| | |
| | thresh = cv2.adaptiveThreshold(filtered, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, |
| | cv2.THRESH_BINARY, 11, 2) |
| |
|
| | |
| | inverted_thresh = cv2.bitwise_not(thresh) |
| |
|
| | |
| | cv_text = pytesseract.image_to_string( |
| | Image.fromarray(inverted_thresh), |
| | config=custom_config |
| | ) |
| |
|
| | |
| | |
| | hsv = cv2.cvtColor(img_cv, cv2.COLOR_BGR2HSV) |
| |
|
| | |
| | lower_white = np.array([0, 0, 150]) |
| | upper_white = np.array([180, 30, 255]) |
| | mask = cv2.inRange(hsv, lower_white, upper_white) |
| |
|
| | |
| | kernel = np.ones((2, 2), np.uint8) |
| | mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) |
| | mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel) |
| |
|
| | |
| | mask = cv2.dilate(mask, kernel, iterations=1) |
| |
|
| | |
| | color_text = pytesseract.image_to_string( |
| | Image.fromarray(mask), |
| | config=r'--oem 3 --psm 6 -l eng --dpi 300' |
| | ) |
| |
|
| | |
| | direct_text = pytesseract.image_to_string( |
| | image, |
| | config=r'--oem 3 --psm 11 -l eng --dpi 300' |
| | ) |
| |
|
| | |
| | results = [inverted_text, cv_text, color_text, direct_text] |
| |
|
| | |
| | def count_alphanumeric(text): |
| | return sum(c.isalnum() for c in text) |
| |
|
| | best_text = max(results, key=count_alphanumeric) |
| |
|
| | |
| | if count_alphanumeric(best_text) < 20: |
| | |
| | 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 |
| |
|
| | |
| | best_text = re.sub(r'[^\w\s,;:%.()\n\'-]', '', best_text) |
| | best_text = best_text.replace('\n\n', '\n') |
| |
|
| | |
| | if "ingredient" in best_text.lower() or any(x in best_text.lower() for x in ["sugar", "cocoa", "milk", "contain"]): |
| | |
| | best_text = re.sub(r'([a-z])([A-Z])', r'\1 \2', best_text) |
| | best_text = re.sub(r'(\d+)([a-zA-Z])', r'\1 \2', best_text) |
| |
|
| | 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)}" |
| |
|
| | |
| | def parse_ingredients(text): |
| | if not text: |
| | return [] |
| |
|
| | |
| | text = re.sub(r'^ingredients:?\s*', '', text.lower(), flags=re.IGNORECASE) |
| |
|
| | |
| | text = re.sub(r'[|\\/@#$%^&*()_+=]', '', text) |
| |
|
| | |
| | text = re.sub(r'\bngredients\b', 'ingredients', text) |
| |
|
| | |
| | replacements = { |
| | '0': 'o', 'l': 'i', '1': 'i', |
| | '5': 's', '8': 'b', 'Q': 'g', |
| | } |
| |
|
| | for error, correction in replacements.items(): |
| | text = text.replace(error, correction) |
| |
|
| | |
| | ingredients = re.split(r',|;|\n', text) |
| |
|
| | |
| | cleaned_ingredients = [] |
| | for i in ingredients: |
| | i = i.strip().lower() |
| | if i and len(i) > 1: |
| | cleaned_ingredients.append(i) |
| |
|
| | return cleaned_ingredients |
| |
|
| |
|
| | |
| | def process_input(input_method, product_name, camera_input, product_photo, health_conditions): |
| | if input_method == "Product Photo": |
| | if product_photo is not None: |
| | |
| | ingredients, detected_product = extract_ingredients_with_llama(image=product_photo) |
| |
|
| | |
| | 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)}" |
| |
|
| | |
| | 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 + analysis |
| | else: |
| | return "No product image captured. Please try again." |
| |
|
| | elif input_method == "Product Name": |
| | if product_name and product_name.strip(): |
| | |
| | ingredients, _ = extract_ingredients_with_llama(product_name=product_name) |
| |
|
| | |
| | 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)}" |
| |
|
| | |
| | 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 "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 |
| |
|
| | |
| | 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." |
| |
|
| | |
| | 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 = gr.Textbox( |
| | label="Enter product name", |
| | placeholder="e.g., Coca-Cola, Oreo Cookies, Lay's Potato Chips", |
| | visible=False |
| | ) |
| |
|
| | |
| | product_photo = gr.Image(label="Take a photo of the product", type="pil", visible=True) |
| |
|
| | |
| | camera_input = gr.Image(label="Capture ingredients label with camera", type="pil", visible=False) |
| |
|
| | |
| | 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) |
| |
|
| | |
| | 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]) |
| |
|
| | |
| | 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" |
| |
|
| | |
| | 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. |
| | """) |
| |
|
| | |
| | if __name__ == "__main__": |
| | app.launch() |