|
|
|
import gradio as gr |
|
import wget |
|
from transformers import pipeline |
|
import requests |
|
import torch |
|
|
|
|
|
api_url = "https://trackapi.nutritionix.com/v2/natural/nutrients" |
|
|
|
|
|
headers = { |
|
"x-app-id": "dd773727", |
|
"x-app-key": "86f278fc4c7f276c386f280848acf3e6", |
|
} |
|
|
|
|
|
|
|
|
|
device = 0 if torch.cuda.is_available() else -1 |
|
|
|
|
|
visual_quest_ans = pipeline("visual-question-answering", model="Salesforce/blip-vqa-base", device=device) |
|
|
|
|
|
translation_eng_to_ar = pipeline("translation_en_to_ar", model="marefa-nlp/marefa-mt-en-ar", device=device) |
|
|
|
|
|
def food_recognizer(image): |
|
|
|
result = visual_quest_ans(image=image, question="What is the food or the drink in the image?") |
|
return result[0]['answer'] |
|
|
|
|
|
def nutrition_info(food): |
|
|
|
data = { |
|
"query": food |
|
} |
|
|
|
|
|
response = requests.post(api_url, headers=headers, json=data) |
|
|
|
|
|
nutritions = response.json() |
|
return nutritions |
|
|
|
|
|
def translator(text): |
|
text = text.strip() |
|
result = translation_eng_to_ar(text) |
|
result = result[0]['translation_text'] |
|
return result |
|
|
|
|
|
def process_food_result(image, language): |
|
|
|
food_item = food_recognizer(image) |
|
|
|
|
|
nutritions_info = nutrition_info(food_item) |
|
|
|
|
|
food_info = nutritions_info['foods'][0] |
|
calories = food_info['nf_calories'] |
|
protein = food_info['nf_protein'] |
|
carbs = food_info['nf_total_carbohydrate'] |
|
fat = food_info['nf_total_fat'] |
|
|
|
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') |
|
|
|
|
|
liquid_keywords = ['juice', 'water', 'milk', 'soda', 'tea', 'coffee'] |
|
is_liquid = any(keyword in food_item.lower() for keyword in liquid_keywords) |
|
|
|
|
|
if is_liquid and serving_size != 'Unknown': |
|
serving_size_ml = serving_size |
|
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} جرام" |
|
|
|
|
|
if language == "Arabic": |
|
|
|
food_item_ar = translator(food_item) |
|
output_ar = f""" |
|
<div style='direction: rtl; text-align: right;'> |
|
<b>الطعام</b>: {food_item_ar}<br> |
|
<b>حجم الحصة</b>: {serving_size_text_ar}<br> |
|
<b>السعرات الحرارية</b>: {calories} كيلو كالوري<br> |
|
<b>البروتين</b>: {protein} جرام<br> |
|
<b>الكربوهيدرات</b>: {carbs} جرام<br> |
|
<b>السكر</b>: {sugars} جرام<br> |
|
<b>الألياف</b>: {fiber} جرام<br> |
|
<b>الصوديوم</b>: {sodium} مجم<br> |
|
<b>الدهون</b>: {fat} جرام |
|
</div> |
|
""" |
|
return output_ar |
|
else: |
|
|
|
output_en = f""" |
|
<div style='text-align: left;'> |
|
<b>Food</b>: {food_item}<br> |
|
<b>Serving Size</b>: {serving_size_text_en}<br> |
|
<b>Calories</b>: {calories} kcal<br> |
|
<b>Protein</b>: {protein}g<br> |
|
<b>Carbohydrates</b>: {carbs}g<br> |
|
<b>Sugars</b>: {sugars}g<br> |
|
<b>Fiber</b>: {fiber}g<br> |
|
<b>Sodium</b>: {sodium}mg<br> |
|
<b>Fat</b>: {fat}g |
|
</div> |
|
""" |
|
return output_en |
|
|
|
|
|
|
|
def gradio_function(image, language): |
|
|
|
result = process_food_result(image, language) |
|
return result |
|
|
|
|
|
image_urls = [ |
|
"https://raw.githubusercontent.com/Abdulrahman078/ML_Datasets-Imgs-Vids/main/close-up-delicious-pizza.jpg", |
|
"https://raw.githubusercontent.com/Abdulrahman078/ML_Datasets-Imgs-Vids/main/assorted-desserts-with-chocolate-frosted-pink-glazed-sprinkles.jpg", |
|
"https://raw.githubusercontent.com/Abdulrahman078/ML_Datasets-Imgs-Vids/main/fried-fish-with-cranberries-wooden-board.jpg", |
|
"https://raw.githubusercontent.com/Abdulrahman078/ML_Datasets-Imgs-Vids/main/glass-water.jpg" |
|
] |
|
|
|
|
|
example_images = [wget.download(url) for url in image_urls] |
|
examples = [[img] for img in example_images] |
|
|
|
|
|
|
|
iface = gr.Interface( |
|
fn=gradio_function, |
|
inputs=[gr.Image(type="pil", label="Upload an image"), |
|
gr.Dropdown(choices=["Arabic", "English"], label="Select Language", value="Arabic")], |
|
outputs=gr.HTML(label="Food and Nutrition Information"), |
|
title="Bilingual Food Recognition and Nutrition Info Tool", |
|
description="Upload an image of food, and the tool will recognize it and provide nutritional information in both English or Arabic languages.", |
|
examples=examples |
|
) |
|
|
|
|
|
iface.launch(debug=True) |