File size: 4,247 Bytes
d823103
 
 
 
ff7b9c3
d823103
 
 
 
 
2abbae1
 
d823103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e81a208
d823103
 
4f80a08
 
d823103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e81a208
c389c88
e81a208
601dbe9
c389c88
 
 
 
 
 
 
 
d823103
e81a208
d823103
 
4f80a08
d823103
5a62196
4f80a08
d823103
 
 
 
 
601dbe9
 
 
 
d823103
 
 
216b01b
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
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)