import gradio as gr from transformers import pipeline import requests import torch import time # Nutritionix API setup api_url = "https://trackapi.nutritionix.com/v2/natural/nutrients" # App ID, App Key provided by Nutritionix headers = { "x-app-id": "703922ce", "x-app-key": "44e3db1ebdb8fa6a763ea2aaced07959", } # Load the Models # Check if a GPU is available device = 0 if torch.cuda.is_available() else -1 # Load the BLIP VQA Model (Recognize the food) visual_quest_ans = pipeline("visual-question-answering", model="Salesforce/blip-vqa-base", device=device) # Function to recognize food from the image using the VQA model def food_recognizer(image): # Pass the image and the question to the model to identify the food in the image result = visual_quest_ans(image=image, question="What is the food or the drink in the image?") return result[0]['answer'] # Function to fetch nutritional information from Nutritionix API def nutrition_info(food): # Prepare the data for the API request data = { "query": food } # Send a POST request to the Nutritionix API with the food item response = requests.post(api_url, headers=headers, json=data) # Check if the response is valid and contains the necessary information if response.status_code == 200: nutritions = response.json() if 'foods' in nutritions and len(nutritions['foods']) > 0: return nutritions['foods'][0] # Return the first food item return None # Return None if no valid nutrition data is found # Function to process food recognition and get nutrition info def process_food_result(image): # Recognize the food item in the uploaded image food_item = food_recognizer(image) # Fetch nutritional information for the recognized food item food_info = nutrition_info(food_item) # If no nutritional information is found, return a message if food_info is None: return f"Sorry, no nutritional information found for '{food_item}'." # Extract nutritional information calories = food_info['nf_calories'] protein = food_info['nf_protein'] carbs = food_info['nf_total_carbohydrate'] fat = food_info['nf_total_fat'] # Use 'Unknown' if value is not available sugars = food_info.get('nf_sugars', 'Unknown') fiber = food_info.get('nf_dietary_fiber', 'Unknown') sodium = food_info.get('nf_sodium', 'Unknown') serving_size = food_info.get('serving_weight_grams', 'Unknown') # Identify if the food item is a liquid (simple check for common drink categories) liquid_keywords = ['juice', 'water', 'milk', 'soda', 'tea', 'coffee'] is_liquid = any(keyword in food_item.lower() for keyword in liquid_keywords) # Convert serving size to milliliters if it's a liquid if is_liquid and serving_size != 'Unknown': serving_size_ml = serving_size # Assume 1 gram ≈ 1 milliliter for liquids serving_size_text_en = f"{serving_size_ml} mL" serving_size_text_ar = f"{serving_size_ml} مل" else: serving_size_text_en = f"{serving_size} grams" serving_size_text_ar = f"{serving_size} جرام" # Generate output in the selected language output_en = f""" Food: {food_item} Serving Size: {serving_size_text_en} Calories: {calories} kcal Protein: {protein}g Carbohydrates: {carbs}g Sugars: {sugars}g Fiber: {fiber}g Sodium: {sodium}mg Fat: {fat}g """ return output_en # Gradio interface function def gradio_function(image): # Call the process_food_result function to get the output result = process_food_result(image) return result # Setup the Gradio interface iface = gr.Interface( fn=gradio_function, # Function to call inputs=[gr.Image(type="pil", label="Upload an image")], outputs=gr.Textbox(label="Food and Nutrition Information"), title="Food Recognition and Nutrition Info Tool", # Title of the Gradio interface description="Upload an image of food, and the tool will recognize it and provide nutritional information." # Description of the tool ) # Launch the Gradio interface with debug mode enabled iface.launch(share=True)