BMCVRN commited on
Commit
36d7b2f
ยท
verified ยท
1 Parent(s): 8e617b1

Update demo.py

Browse files
Files changed (1) hide show
  1. demo.py +166 -170
demo.py CHANGED
@@ -1,170 +1,166 @@
1
- import os
2
- import base64
3
- from openai import OpenAI
4
- import gradio as gr
5
- from dotenv import load_dotenv
6
- import requests
7
-
8
- # Load environment variables from .env file
9
- load_dotenv()
10
-
11
- # Set OpenAI API key
12
- openai_api_key = os.getenv("OPENAI_API_KEY")
13
- edamam_app_id = os.getenv("EDAMAM_APP_ID")
14
- edamam_app_key = os.getenv("EDAMAM_API_KEY")
15
-
16
- def encode_image(image_path):
17
- try:
18
- with open(image_path, "rb") as image_file:
19
- return base64.b64encode(image_file.read()).decode('utf-8')
20
- except Exception as e:
21
- raise ValueError(f"Failed to encode image: {e}")
22
-
23
-
24
- def image_to_ingredient_list(image_path):
25
- """
26
- Transforms an image of food items/dishes into an ingredient list using GPT-4.
27
-
28
- Args:
29
- image_path (str): The file path to the uploaded image.
30
-
31
- Returns:
32
- str: The generated ingredient list or an error message.
33
- """
34
- try:
35
- # Encode the image to base64
36
- base64_image = encode_image(image_path)
37
- client = OpenAI(api_key=openai_api_key)
38
- # Prepare the message with the base64-encoded image
39
- messages = [
40
- {
41
- "role": "user",
42
- "content": [
43
- {
44
- "type": "text",
45
- "text": """Please provide an ingredient list with estimated quantities used to make the following food image.
46
- If the image is a drink, list the ingredients in the drink with estimated quantities based on the image.
47
- Only estimate the quantities based on the current image not the entire food. For example if the image is a slice of cake, adjust the quantities to fit a single slice and not a whole cake.
48
- Be confident in your answer and don't list down ingredients or quantities you are unsure of.
49
- Only output the ingredients list with estimated quantities and separate each item with a newline.
50
- Example:
51
- 1 cup mango slices
52
- 1/2 cup plain yogurt
53
- 1 tablespoon peanut butter"""
54
- },
55
- {
56
- "type": "image_url",
57
- "image_url": {
58
- "url": f"data:image/jpeg;base64,{base64_image}"
59
- },
60
- },
61
- ],
62
- }
63
- ]
64
-
65
- # Call OpenAI's Chat Completion API with image
66
- response = client.chat.completions.create(
67
- model="gpt-4o-mini",
68
- messages=messages
69
- )
70
- print(response.choices[0].message.content)
71
- # Extract the response text
72
- ingredient_list = response.choices[0].message.content
73
-
74
- return ingredient_list, display_nutrition(ingredient_list)
75
-
76
- except Exception as e:
77
- return f"Error processing image: {str(e)}", ""
78
-
79
- def get_nutritional_info(ingredient_list, app_id=edamam_app_id, app_key=edamam_app_key):
80
- """
81
- Fetches nutritional information for a list of ingredients using Edamam's Nutrition Analysis API.
82
-
83
- Parameters:
84
- - ingredient_list (str): A string where each ingredient is on a new line.
85
- - app_id (str): Edamam application ID.
86
- - app_key (str): Edamam application key.
87
-
88
- Returns:
89
- - dict: Nutritional information returned by the API.
90
- """
91
- print(edamam_app_id, edamam_app_key)
92
- url = 'https://api.edamam.com/api/nutrition-details'
93
- headers = {'Content-Type': 'application/json'}
94
- data = {
95
- 'title': 'Recipe',
96
- 'ingr': ingredient_list.split('\n')
97
- }
98
- params = {
99
- 'app_id': app_id,
100
- 'app_key': app_key
101
- }
102
- response = requests.post(url, headers=headers, json=data, params=params)
103
- # response.raise_for_status() # Raise an error for bad status codes
104
- return response.json()
105
-
106
- import requests
107
-
108
- def format_nutrition(ingredients):
109
- nutrition_data = get_nutritional_info(ingredients)
110
-
111
- # Extract relevant nutritional information
112
- calories = nutrition_data.get('calories', 0)
113
- total_nutrients = nutrition_data.get('totalNutrients', {})
114
- carbs = total_nutrients.get('CHOCDF', {}).get('quantity', 0)
115
- protein = total_nutrients.get('PROCNT', {}).get('quantity', 0)
116
- fats = total_nutrients.get('FAT', {}).get('quantity', 0)
117
-
118
- return {
119
- 'Calories': calories,
120
- 'Carbs': carbs,
121
- 'Protein': protein,
122
- 'Fats': fats
123
- }
124
-
125
- def display_nutrition(ingredients):
126
- nutrition_info = format_nutrition(ingredients)
127
- return (
128
- f"๐Ÿ”ฅ Calories: {nutrition_info['Calories']} kcal\n"
129
- f"๐Ÿž Carbs: {nutrition_info['Carbs']:.2f} g\n"
130
- f"๐Ÿ— Protein: {nutrition_info['Protein']:.2f} g\n"
131
- f"๐Ÿฅ‘ Fats: {nutrition_info['Fats']:.2f} g"
132
- )
133
-
134
-
135
- # Define Gradio interface
136
- iface = gr.Interface(
137
- fn=image_to_ingredient_list,
138
- inputs=gr.Image(type="filepath", label="Upload Food Image"),
139
- outputs=[gr.Textbox(label="Ingredient List"), gr.Textbox(label="Nutritional Information")],
140
- title="Food Image to Ingredient List",
141
- description="Upload an image containing food items, and the app will generate an ingredient list like '1 cup rice, 10 oz chickpeas'.",
142
- examples=[
143
- ["sample_images/Picture1.jpg"],
144
- ["sample_images/Picture2.jpg"],
145
- ["sample_images/Picture3.jpg"],
146
- ["sample_images/Picture4.jpg"],
147
- ["sample_images/Picture5.jpg"],
148
- ["sample_images/Picture6.jpg"],
149
- ["sample_images/Picture7.jpg"],
150
- ["sample_images/Picture8.jpg"],
151
- ["sample_images/Picture9.jpg"],
152
- ["sample_images/Picture10.jpg"],
153
- ["sample_images/Picture11.jpg"],
154
- ["sample_images/Picture12.jpg"],
155
- ["sample_images/Picture13.jpg"],
156
- ["sample_images/Picture14.jpg"],
157
- ["sample_images/Picture15.jpg"],
158
- ["sample_images/Picture16.jpg"],
159
- ["sample_images/Picture17.jpg"],
160
- ["sample_images/Picture18.jpg"],
161
- ["sample_images/Picture19.jpg"],
162
- ["sample_images/Picture20.jpg"]
163
-
164
- ],
165
- allow_flagging="never"
166
- )
167
-
168
- # Launch the app
169
- if __name__ == "__main__":
170
- iface.launch()
 
1
+ import os
2
+ import base64
3
+ from openai import OpenAI
4
+ import gradio as gr
5
+ import requests
6
+
7
+ # Set OpenAI API key
8
+ openai_api_key = os.getenv("OPENAI_API_KEY")
9
+ edamam_app_id = os.getenv("EDAMAM_APP_ID")
10
+ edamam_app_key = os.getenv("EDAMAM_API_KEY")
11
+
12
+ def encode_image(image_path):
13
+ try:
14
+ with open(image_path, "rb") as image_file:
15
+ return base64.b64encode(image_file.read()).decode('utf-8')
16
+ except Exception as e:
17
+ raise ValueError(f"Failed to encode image: {e}")
18
+
19
+
20
+ def image_to_ingredient_list(image_path):
21
+ """
22
+ Transforms an image of food items/dishes into an ingredient list using GPT-4.
23
+
24
+ Args:
25
+ image_path (str): The file path to the uploaded image.
26
+
27
+ Returns:
28
+ str: The generated ingredient list or an error message.
29
+ """
30
+ try:
31
+ # Encode the image to base64
32
+ base64_image = encode_image(image_path)
33
+ client = OpenAI(api_key=openai_api_key)
34
+ # Prepare the message with the base64-encoded image
35
+ messages = [
36
+ {
37
+ "role": "user",
38
+ "content": [
39
+ {
40
+ "type": "text",
41
+ "text": """Please provide an ingredient list with estimated quantities used to make the following food image.
42
+ If the image is a drink, list the ingredients in the drink with estimated quantities based on the image.
43
+ Only estimate the quantities based on the current image not the entire food. For example if the image is a slice of cake, adjust the quantities to fit a single slice and not a whole cake.
44
+ Be confident in your answer and don't list down ingredients or quantities you are unsure of.
45
+ Only output the ingredients list with estimated quantities and separate each item with a newline.
46
+ Example:
47
+ 1 cup mango slices
48
+ 1/2 cup plain yogurt
49
+ 1 tablespoon peanut butter"""
50
+ },
51
+ {
52
+ "type": "image_url",
53
+ "image_url": {
54
+ "url": f"data:image/jpeg;base64,{base64_image}"
55
+ },
56
+ },
57
+ ],
58
+ }
59
+ ]
60
+
61
+ # Call OpenAI's Chat Completion API with image
62
+ response = client.chat.completions.create(
63
+ model="gpt-4o-mini",
64
+ messages=messages
65
+ )
66
+ print(response.choices[0].message.content)
67
+ # Extract the response text
68
+ ingredient_list = response.choices[0].message.content
69
+
70
+ return ingredient_list, display_nutrition(ingredient_list)
71
+
72
+ except Exception as e:
73
+ return f"Error processing image: {str(e)}", ""
74
+
75
+ def get_nutritional_info(ingredient_list, app_id=edamam_app_id, app_key=edamam_app_key):
76
+ """
77
+ Fetches nutritional information for a list of ingredients using Edamam's Nutrition Analysis API.
78
+
79
+ Parameters:
80
+ - ingredient_list (str): A string where each ingredient is on a new line.
81
+ - app_id (str): Edamam application ID.
82
+ - app_key (str): Edamam application key.
83
+
84
+ Returns:
85
+ - dict: Nutritional information returned by the API.
86
+ """
87
+ print(edamam_app_id, edamam_app_key)
88
+ url = 'https://api.edamam.com/api/nutrition-details'
89
+ headers = {'Content-Type': 'application/json'}
90
+ data = {
91
+ 'title': 'Recipe',
92
+ 'ingr': ingredient_list.split('\n')
93
+ }
94
+ params = {
95
+ 'app_id': app_id,
96
+ 'app_key': app_key
97
+ }
98
+ response = requests.post(url, headers=headers, json=data, params=params)
99
+ # response.raise_for_status() # Raise an error for bad status codes
100
+ return response.json()
101
+
102
+ import requests
103
+
104
+ def format_nutrition(ingredients):
105
+ nutrition_data = get_nutritional_info(ingredients)
106
+
107
+ # Extract relevant nutritional information
108
+ calories = nutrition_data.get('calories', 0)
109
+ total_nutrients = nutrition_data.get('totalNutrients', {})
110
+ carbs = total_nutrients.get('CHOCDF', {}).get('quantity', 0)
111
+ protein = total_nutrients.get('PROCNT', {}).get('quantity', 0)
112
+ fats = total_nutrients.get('FAT', {}).get('quantity', 0)
113
+
114
+ return {
115
+ 'Calories': calories,
116
+ 'Carbs': carbs,
117
+ 'Protein': protein,
118
+ 'Fats': fats
119
+ }
120
+
121
+ def display_nutrition(ingredients):
122
+ nutrition_info = format_nutrition(ingredients)
123
+ return (
124
+ f"๐Ÿ”ฅ Calories: {nutrition_info['Calories']} kcal\n"
125
+ f"๐Ÿž Carbs: {nutrition_info['Carbs']:.2f} g\n"
126
+ f"๐Ÿ— Protein: {nutrition_info['Protein']:.2f} g\n"
127
+ f"๐Ÿฅ‘ Fats: {nutrition_info['Fats']:.2f} g"
128
+ )
129
+
130
+
131
+ # Define Gradio interface
132
+ iface = gr.Interface(
133
+ fn=image_to_ingredient_list,
134
+ inputs=gr.Image(type="filepath", label="Upload Food Image"),
135
+ outputs=[gr.Textbox(label="Ingredient List"), gr.Textbox(label="Nutritional Information")],
136
+ title="Food Image to Ingredient List",
137
+ description="Upload an image containing food items, and the app will generate an ingredient list like '1 cup rice, 10 oz chickpeas'.",
138
+ examples=[
139
+ ["sample_images/Picture1.jpg"],
140
+ ["sample_images/Picture2.jpg"],
141
+ ["sample_images/Picture3.jpg"],
142
+ ["sample_images/Picture4.jpg"],
143
+ ["sample_images/Picture5.jpg"],
144
+ ["sample_images/Picture6.jpg"],
145
+ ["sample_images/Picture7.jpg"],
146
+ ["sample_images/Picture8.jpg"],
147
+ ["sample_images/Picture9.jpg"],
148
+ ["sample_images/Picture10.jpg"],
149
+ ["sample_images/Picture11.jpg"],
150
+ ["sample_images/Picture12.jpg"],
151
+ ["sample_images/Picture13.jpg"],
152
+ ["sample_images/Picture14.jpg"],
153
+ ["sample_images/Picture15.jpg"],
154
+ ["sample_images/Picture16.jpg"],
155
+ ["sample_images/Picture17.jpg"],
156
+ ["sample_images/Picture18.jpg"],
157
+ ["sample_images/Picture19.jpg"],
158
+ ["sample_images/Picture20.jpg"]
159
+
160
+ ],
161
+ allow_flagging="never"
162
+ )
163
+
164
+ # Launch the app
165
+ if __name__ == "__main__":
166
+ iface.launch()