CIAZIZ commited on
Commit
ba1877d
·
verified ·
1 Parent(s): 37dad77

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -70
app.py CHANGED
@@ -1,4 +1,3 @@
1
- # Import necessary libraries
2
  import gradio as gr
3
  import wget
4
  from transformers import pipeline
@@ -16,80 +15,52 @@ headers = {
16
 
17
  # Load the Models
18
 
19
- # Check if a GPU is available
20
  device = 0 if torch.cuda.is_available() else -1
21
 
22
- # Load the BLIP VQA Model (Recognize the food)
23
  visual_quest_ans = pipeline("visual-question-answering", model="Salesforce/blip-vqa-base", device=device)
24
-
25
- # Load the Translation Model (English to Arabic)
26
  translation_eng_to_ar = pipeline("translation_en_to_ar", model="marefa-nlp/marefa-mt-en-ar", device=device)
27
 
28
- # Function to recognize food from the image using the VQA model
29
  def food_recognizer(image):
30
- # Pass the image and the question to the model to identify the food on the image
31
  result = visual_quest_ans(image=image, question="What is the food or the drink in the image?")
32
  return result[0]['answer']
33
 
34
- # Function to fetch nutritional information from Nutritionix API
35
  def nutrition_info(food):
36
- # Prepare the data for the API request
37
- data = {
38
- "query": food
39
- }
40
-
41
- # Send a POST request to the Nutritionix API with the food item
42
  response = requests.post(api_url, headers=headers, json=data)
 
43
 
44
- # Get the nutritional information in JSON format
45
- nutritions = response.json()
46
- return nutritions
47
-
48
- # Function to translate text from English to Arabic with preprocessing
49
  def translator(text):
50
- text = text.strip() # Remove leading/trailing spaces
51
- result = translation_eng_to_ar(text) # Use the translation model to translate the text
52
- result = result[0]['translation_text']
53
- return result
54
 
55
- # Function to process food recognition and get nutrition info
56
  def process_food_result(image, language):
57
- # Recognize the food item in the uploaded image
58
  food_item = food_recognizer(image)
59
-
60
- # Fetch nutritional information for the recognized food item
61
  nutritions_info = nutrition_info(food_item)
62
-
63
- # Extract nutritional information
64
  food_info = nutritions_info['foods'][0]
 
65
  calories = food_info['nf_calories']
66
  protein = food_info['nf_protein']
67
  carbs = food_info['nf_total_carbohydrate']
68
  fat = food_info['nf_total_fat']
69
- # Use 'Unknown' if value is not available
70
  sugars = food_info.get('nf_sugars', 'Unknown')
71
  fiber = food_info.get('nf_dietary_fiber', 'Unknown')
72
  sodium = food_info.get('nf_sodium', 'Unknown')
73
  serving_size = food_info.get('serving_weight_grams', 'Unknown')
74
 
75
- # Identify if the food item is a liquid (simple check for common drink categories)
76
  liquid_keywords = ['juice', 'water', 'milk', 'soda', 'tea', 'coffee']
77
  is_liquid = any(keyword in food_item.lower() for keyword in liquid_keywords)
78
 
79
- # Convert serving size to milliliters if it's a liquid
80
  if is_liquid and serving_size != 'Unknown':
81
- serving_size_ml = serving_size # Assume 1 gram ≈ 1 milliliter for liquids
82
- serving_size_text_en = f"{serving_size_ml} mL"
83
- serving_size_text_ar = f"{serving_size_ml} مل"
84
  else:
85
  serving_size_text_en = f"{serving_size} grams"
86
  serving_size_text_ar = f"{serving_size} جرام"
87
 
88
- # Generate output in the selected language
89
  if language == "Arabic":
90
- # Translate the food item name to Arabic
91
  food_item_ar = translator(food_item)
92
- output_ar = f"""
93
  <div style='direction: rtl; text-align: right;'>
94
  <b>الطعام</b>: {food_item_ar}<br>
95
  <b>حجم الحصة</b>: {serving_size_text_ar}<br>
@@ -102,10 +73,8 @@ def process_food_result(image, language):
102
  <b>الدهون</b>: {fat} جرام
103
  </div>
104
  """
105
- return output_ar
106
  else:
107
- # For English output
108
- output_en = f"""
109
  <div style='text-align: left;'>
110
  <b>Food</b>: {food_item}<br>
111
  <b>Serving Size</b>: {serving_size_text_en}<br>
@@ -118,38 +87,15 @@ def process_food_result(image, language):
118
  <b>Fat</b>: {fat}g
119
  </div>
120
  """
121
- return output_en
122
-
123
 
124
- # Gradio interface function
125
  def gradio_function(image, language):
126
- # Call the process_food_result function to get the output
127
- result = process_food_result(image, language)
128
- return result
129
-
130
- # Define URLs of example images
131
- image_urls = [
132
- "https://raw.githubusercontent.com/Abdulrahman078/ML_Datasets-Imgs-Vids/main/close-up-delicious-pizza.jpg",
133
- "https://raw.githubusercontent.com/Abdulrahman078/ML_Datasets-Imgs-Vids/main/assorted-desserts-with-chocolate-frosted-pink-glazed-sprinkles.jpg",
134
- "https://raw.githubusercontent.com/Abdulrahman078/ML_Datasets-Imgs-Vids/main/fried-fish-with-cranberries-wooden-board.jpg",
135
- "https://raw.githubusercontent.com/Abdulrahman078/ML_Datasets-Imgs-Vids/main/glass-water.jpg"
136
- ]
137
-
138
- # Download the images and use their paths
139
- example_images = [wget.download(url) for url in image_urls]
140
- examples = [[img] for img in example_images]
141
-
142
 
143
- # Setup the Gradio interface
144
  iface = gr.Interface(
145
- fn=gradio_function, # Function to call
146
- inputs=[gr.Image(type="pil", label="Upload an image"), # Input: Image (in PIL format)
147
- gr.Dropdown(choices=["Arabic", "English"], label="Select Language", value="Arabic")], # Input: Dropdown for language selection
148
- outputs=gr.HTML(label="Food and Nutrition Information"), # Output: HTML for displaying nutrition info
149
- title="Bilingual Food Recognition and Nutrition Info Tool", # Title of the Gradio interface
150
- description="Upload an image of food, and the tool will recognize it and provide nutritional information in both English or Arabic languages.", # Description of the tool
151
- examples=examples # Add examples with the image and language
152
  )
153
 
154
- # Launch the Gradio interface with debug mode enabled
155
- iface.launch(debug=True)
 
 
1
  import gradio as gr
2
  import wget
3
  from transformers import pipeline
 
15
 
16
  # Load the Models
17
 
 
18
  device = 0 if torch.cuda.is_available() else -1
19
 
 
20
  visual_quest_ans = pipeline("visual-question-answering", model="Salesforce/blip-vqa-base", device=device)
 
 
21
  translation_eng_to_ar = pipeline("translation_en_to_ar", model="marefa-nlp/marefa-mt-en-ar", device=device)
22
 
 
23
  def food_recognizer(image):
 
24
  result = visual_quest_ans(image=image, question="What is the food or the drink in the image?")
25
  return result[0]['answer']
26
 
 
27
  def nutrition_info(food):
28
+ data = {"query": food}
 
 
 
 
 
29
  response = requests.post(api_url, headers=headers, json=data)
30
+ return response.json()
31
 
 
 
 
 
 
32
  def translator(text):
33
+ text = text.strip()
34
+ result = translation_eng_to_ar(text)
35
+ return result[0]['translation_text']
 
36
 
 
37
  def process_food_result(image, language):
 
38
  food_item = food_recognizer(image)
 
 
39
  nutritions_info = nutrition_info(food_item)
 
 
40
  food_info = nutritions_info['foods'][0]
41
+
42
  calories = food_info['nf_calories']
43
  protein = food_info['nf_protein']
44
  carbs = food_info['nf_total_carbohydrate']
45
  fat = food_info['nf_total_fat']
 
46
  sugars = food_info.get('nf_sugars', 'Unknown')
47
  fiber = food_info.get('nf_dietary_fiber', 'Unknown')
48
  sodium = food_info.get('nf_sodium', 'Unknown')
49
  serving_size = food_info.get('serving_weight_grams', 'Unknown')
50
 
 
51
  liquid_keywords = ['juice', 'water', 'milk', 'soda', 'tea', 'coffee']
52
  is_liquid = any(keyword in food_item.lower() for keyword in liquid_keywords)
53
 
 
54
  if is_liquid and serving_size != 'Unknown':
55
+ serving_size_text_en = f"{serving_size} mL"
56
+ serving_size_text_ar = f"{serving_size} مل"
 
57
  else:
58
  serving_size_text_en = f"{serving_size} grams"
59
  serving_size_text_ar = f"{serving_size} جرام"
60
 
 
61
  if language == "Arabic":
 
62
  food_item_ar = translator(food_item)
63
+ return f"""
64
  <div style='direction: rtl; text-align: right;'>
65
  <b>الطعام</b>: {food_item_ar}<br>
66
  <b>حجم الحصة</b>: {serving_size_text_ar}<br>
 
73
  <b>الدهون</b>: {fat} جرام
74
  </div>
75
  """
 
76
  else:
77
+ return f"""
 
78
  <div style='text-align: left;'>
79
  <b>Food</b>: {food_item}<br>
80
  <b>Serving Size</b>: {serving_size_text_en}<br>
 
87
  <b>Fat</b>: {fat}g
88
  </div>
89
  """
 
 
90
 
 
91
  def gradio_function(image, language):
92
+ return process_food_result(image, language)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
 
94
  iface = gr.Interface(
95
+ fn=gradio_function,
96
+ inputs=[gr.Image(type="pil", label="Upload an image"),
97
+ gr.Dropdown(choices=["Arabic", "English"], label="Select Language", value="Arabic")],
98
+ outputs=gr.HTML(label="Food and Nutrition Information")
 
 
 
99
  )
100
 
101
+ iface.launch(debug=True)