Spaces:
Sleeping
Sleeping
Update demo.py
Browse files
demo.py
CHANGED
|
@@ -1,170 +1,166 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import base64
|
| 3 |
-
from openai import OpenAI
|
| 4 |
-
import gradio as gr
|
| 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 |
-
response
|
| 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 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
["sample_images/
|
| 144 |
-
["sample_images/
|
| 145 |
-
["sample_images/
|
| 146 |
-
["sample_images/
|
| 147 |
-
["sample_images/
|
| 148 |
-
["sample_images/
|
| 149 |
-
["sample_images/
|
| 150 |
-
["sample_images/
|
| 151 |
-
["sample_images/
|
| 152 |
-
["sample_images/
|
| 153 |
-
["sample_images/
|
| 154 |
-
["sample_images/
|
| 155 |
-
["sample_images/
|
| 156 |
-
["sample_images/
|
| 157 |
-
["sample_images/
|
| 158 |
-
["sample_images/
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|