Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,9 +20,6 @@ device = 0 if torch.cuda.is_available() else -1
|
|
| 20 |
# Load the BLIP VQA Model (Recognize the food)
|
| 21 |
visual_quest_ans = pipeline("visual-question-answering", model="Salesforce/blip-vqa-base", device=device)
|
| 22 |
|
| 23 |
-
# Load the Translation Model (English to Arabic)
|
| 24 |
-
translation_eng_to_ar = pipeline("translation_en_to_ar", model="marefa-nlp/marefa-mt-en-ar", device=device)
|
| 25 |
-
|
| 26 |
# Function to recognize food from the image using the VQA model
|
| 27 |
def food_recognizer(image):
|
| 28 |
# Pass the image and the question to the model to identify the food in the image
|
|
@@ -46,12 +43,7 @@ def nutrition_info(food):
|
|
| 46 |
return nutritions['foods'][0] # Return the first food item
|
| 47 |
return None # Return None if no valid nutrition data is found
|
| 48 |
|
| 49 |
-
|
| 50 |
-
def translator(text):
|
| 51 |
-
text = text.strip() # Remove leading/trailing spaces
|
| 52 |
-
result = translation_eng_to_ar(text) # Use the translation model to translate the text
|
| 53 |
-
result = result[0]['translation_text']
|
| 54 |
-
return result
|
| 55 |
|
| 56 |
# Function to process food recognition and get nutrition info
|
| 57 |
def process_food_result(image, language,progress= gr.Progress()):
|
|
@@ -93,26 +85,10 @@ def process_food_result(image, language,progress= gr.Progress()):
|
|
| 93 |
serving_size_text_ar = f"{serving_size} جرام"
|
| 94 |
|
| 95 |
# Generate output in the selected language
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
food_item_ar = translator(food_item)
|
| 99 |
-
output_ar = f"""
|
| 100 |
-
<div style='direction: rtl; text-align: right;'>
|
| 101 |
-
<b>الطعام</b>: {food_item_ar}<br>
|
| 102 |
-
<b>حجم الحصة</b>: {serving_size_text_ar}<br>
|
| 103 |
-
<b>السعرات الحرارية</b>: {calories} كيلو كالوري<br>
|
| 104 |
-
<b>البروتين</b>: {protein} جرام<br>
|
| 105 |
-
<b>الكربوهيدرات</b>: {carbs} جرام<br>
|
| 106 |
-
<b>السكر</b>: {sugars} جرام<br>
|
| 107 |
-
<b>الألياف</b>: {fiber} جرام<br>
|
| 108 |
-
<b>الصوديوم</b>: {sodium} مجم<br>
|
| 109 |
-
<b>الدهون</b>: {fat} جرام
|
| 110 |
-
</div>
|
| 111 |
-
"""
|
| 112 |
-
return output_ar
|
| 113 |
-
else:
|
| 114 |
# For English output
|
| 115 |
-
|
| 116 |
<div style='text-align: left;'>
|
| 117 |
<b>Food</b>: {food_item}<br>
|
| 118 |
<b>Serving Size</b>: {serving_size_text_en}<br>
|
|
@@ -125,7 +101,7 @@ def process_food_result(image, language,progress= gr.Progress()):
|
|
| 125 |
<b>Fat</b>: {fat}g
|
| 126 |
</div>
|
| 127 |
"""
|
| 128 |
-
|
| 129 |
|
| 130 |
# Gradio interface function
|
| 131 |
def gradio_function(image, language):
|
|
|
|
| 20 |
# Load the BLIP VQA Model (Recognize the food)
|
| 21 |
visual_quest_ans = pipeline("visual-question-answering", model="Salesforce/blip-vqa-base", device=device)
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
# Function to recognize food from the image using the VQA model
|
| 24 |
def food_recognizer(image):
|
| 25 |
# Pass the image and the question to the model to identify the food in the image
|
|
|
|
| 43 |
return nutritions['foods'][0] # Return the first food item
|
| 44 |
return None # Return None if no valid nutrition data is found
|
| 45 |
|
| 46 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# Function to process food recognition and get nutrition info
|
| 49 |
def process_food_result(image, language,progress= gr.Progress()):
|
|
|
|
| 85 |
serving_size_text_ar = f"{serving_size} جرام"
|
| 86 |
|
| 87 |
# Generate output in the selected language
|
| 88 |
+
|
| 89 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
# For English output
|
| 91 |
+
output_en = f"""
|
| 92 |
<div style='text-align: left;'>
|
| 93 |
<b>Food</b>: {food_item}<br>
|
| 94 |
<b>Serving Size</b>: {serving_size_text_en}<br>
|
|
|
|
| 101 |
<b>Fat</b>: {fat}g
|
| 102 |
</div>
|
| 103 |
"""
|
| 104 |
+
return output_en
|
| 105 |
|
| 106 |
# Gradio interface function
|
| 107 |
def gradio_function(image, language):
|