Update app.py
Browse files
app.py
CHANGED
|
@@ -36,6 +36,46 @@ def save_uploaded_image(image):
|
|
| 36 |
|
| 37 |
return temp_file.name
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
def get_recipe_suggestions(api_key, images, num_recipes=3, dietary_restrictions="None", cuisine_preference="Any"):
|
| 40 |
"""
|
| 41 |
Get recipe suggestions based on the uploaded images of ingredients
|
|
@@ -66,9 +106,20 @@ def get_recipe_suggestions(api_key, images, num_recipes=3, dietary_restrictions=
|
|
| 66 |
# Initialize Together client with the provided API key
|
| 67 |
client = Together(api_key=api_key)
|
| 68 |
|
| 69 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
system_prompt = """You are a culinary expert AI assistant that specializes in creating recipes based on available ingredients.
|
| 71 |
-
|
|
|
|
| 72 |
|
| 73 |
For each recipe suggestion, include:
|
| 74 |
1. Recipe name
|
|
@@ -81,23 +132,16 @@ def get_recipe_suggestions(api_key, images, num_recipes=3, dietary_restrictions=
|
|
| 81 |
|
| 82 |
Consider any dietary restrictions and cuisine preferences mentioned by the user."""
|
| 83 |
|
| 84 |
-
user_prompt = f"""Based on the ingredients
|
|
|
|
|
|
|
|
|
|
| 85 |
Dietary restrictions to consider: {dietary_restrictions}
|
| 86 |
Cuisine preference: {cuisine_preference}
|
| 87 |
-
Please be specific about what ingredients you can identify in the images and creative with your recipe suggestions. Try to use ingredients from all images if possible."""
|
| 88 |
|
| 89 |
-
|
| 90 |
-
content = [{"type": "text", "text": user_prompt}]
|
| 91 |
-
|
| 92 |
-
# Add all images to the content
|
| 93 |
-
for img_path in image_paths:
|
| 94 |
-
content.append({
|
| 95 |
-
"type": "image_url",
|
| 96 |
-
"image_url": {
|
| 97 |
-
"url": f"file://{img_path}"
|
| 98 |
-
}
|
| 99 |
-
})
|
| 100 |
|
|
|
|
| 101 |
response = client.chat.completions.create(
|
| 102 |
model="meta-llama/Llama-Vision-Free",
|
| 103 |
messages=[
|
|
@@ -107,7 +151,7 @@ def get_recipe_suggestions(api_key, images, num_recipes=3, dietary_restrictions=
|
|
| 107 |
},
|
| 108 |
{
|
| 109 |
"role": "user",
|
| 110 |
-
"content":
|
| 111 |
}
|
| 112 |
],
|
| 113 |
max_tokens=2048,
|
|
@@ -121,7 +165,14 @@ def get_recipe_suggestions(api_key, images, num_recipes=3, dietary_restrictions=
|
|
| 121 |
except:
|
| 122 |
pass
|
| 123 |
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
except Exception as e:
|
| 127 |
# Clean up the temporary files in case of error
|
|
@@ -300,6 +351,22 @@ button.primary-button:hover {
|
|
| 300 |
padding: 20px;
|
| 301 |
}
|
| 302 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
/* Responsive styles */
|
| 304 |
@media (max-width: 768px) {
|
| 305 |
.app-title {
|
|
|
|
| 36 |
|
| 37 |
return temp_file.name
|
| 38 |
|
| 39 |
+
def analyze_single_image(client, img_path):
|
| 40 |
+
"""Analyze a single image to identify ingredients"""
|
| 41 |
+
system_prompt = """You are a culinary expert AI assistant that specializes in identifying ingredients in images.
|
| 42 |
+
Your task is to analyze the provided image and list all the food ingredients you can identify.
|
| 43 |
+
Be specific and detailed about what you see. Only list ingredients, don't suggest recipes yet."""
|
| 44 |
+
|
| 45 |
+
user_prompt = "Please identify all the food ingredients visible in this image. List each ingredient on a new line."
|
| 46 |
+
|
| 47 |
+
# Create message with the image
|
| 48 |
+
content = [
|
| 49 |
+
{"type": "text", "text": user_prompt},
|
| 50 |
+
{
|
| 51 |
+
"type": "image_url",
|
| 52 |
+
"image_url": {
|
| 53 |
+
"url": f"file://{img_path}"
|
| 54 |
+
}
|
| 55 |
+
}
|
| 56 |
+
]
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
response = client.chat.completions.create(
|
| 60 |
+
model="meta-llama/Llama-Vision-Free",
|
| 61 |
+
messages=[
|
| 62 |
+
{
|
| 63 |
+
"role": "system",
|
| 64 |
+
"content": system_prompt
|
| 65 |
+
},
|
| 66 |
+
{
|
| 67 |
+
"role": "user",
|
| 68 |
+
"content": content
|
| 69 |
+
}
|
| 70 |
+
],
|
| 71 |
+
max_tokens=500,
|
| 72 |
+
temperature=0.2
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
return response.choices[0].message.content
|
| 76 |
+
except Exception as e:
|
| 77 |
+
return f"Error analyzing image: {str(e)}"
|
| 78 |
+
|
| 79 |
def get_recipe_suggestions(api_key, images, num_recipes=3, dietary_restrictions="None", cuisine_preference="Any"):
|
| 80 |
"""
|
| 81 |
Get recipe suggestions based on the uploaded images of ingredients
|
|
|
|
| 106 |
# Initialize Together client with the provided API key
|
| 107 |
client = Together(api_key=api_key)
|
| 108 |
|
| 109 |
+
# First, analyze each image separately to identify ingredients
|
| 110 |
+
all_ingredients = []
|
| 111 |
+
for img_path in image_paths:
|
| 112 |
+
ingredients_text = analyze_single_image(client, img_path)
|
| 113 |
+
all_ingredients.append(ingredients_text)
|
| 114 |
+
|
| 115 |
+
# Combine all ingredients into one list
|
| 116 |
+
combined_ingredients = "\n\n".join([f"Image {i+1} ingredients:\n{ingredients}"
|
| 117 |
+
for i, ingredients in enumerate(all_ingredients)])
|
| 118 |
+
|
| 119 |
+
# Now generate recipes based on all identified ingredients
|
| 120 |
system_prompt = """You are a culinary expert AI assistant that specializes in creating recipes based on available ingredients.
|
| 121 |
+
You will be provided with lists of ingredients identified from multiple images. Your task is to suggest creative,
|
| 122 |
+
detailed recipes that use as many of the identified ingredients as possible.
|
| 123 |
|
| 124 |
For each recipe suggestion, include:
|
| 125 |
1. Recipe name
|
|
|
|
| 132 |
|
| 133 |
Consider any dietary restrictions and cuisine preferences mentioned by the user."""
|
| 134 |
|
| 135 |
+
user_prompt = f"""Based on the following ingredients identified from multiple images, suggest {num_recipes} creative and delicious recipes.
|
| 136 |
+
|
| 137 |
+
{combined_ingredients}
|
| 138 |
+
|
| 139 |
Dietary restrictions to consider: {dietary_restrictions}
|
| 140 |
Cuisine preference: {cuisine_preference}
|
|
|
|
| 141 |
|
| 142 |
+
Please be creative with your recipe suggestions and try to use ingredients from multiple images if possible."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
+
# Generate recipe suggestions based on all identified ingredients
|
| 145 |
response = client.chat.completions.create(
|
| 146 |
model="meta-llama/Llama-Vision-Free",
|
| 147 |
messages=[
|
|
|
|
| 151 |
},
|
| 152 |
{
|
| 153 |
"role": "user",
|
| 154 |
+
"content": user_prompt
|
| 155 |
}
|
| 156 |
],
|
| 157 |
max_tokens=2048,
|
|
|
|
| 165 |
except:
|
| 166 |
pass
|
| 167 |
|
| 168 |
+
# Add information about the ingredients identified
|
| 169 |
+
result = "## 📋 Ingredients Identified\n\n"
|
| 170 |
+
result += combined_ingredients
|
| 171 |
+
result += "\n\n---\n\n"
|
| 172 |
+
result += "## 🍽️ Recipe Suggestions\n\n"
|
| 173 |
+
result += response.choices[0].message.content
|
| 174 |
+
|
| 175 |
+
return result
|
| 176 |
|
| 177 |
except Exception as e:
|
| 178 |
# Clean up the temporary files in case of error
|
|
|
|
| 351 |
padding: 20px;
|
| 352 |
}
|
| 353 |
|
| 354 |
+
.recipe-output {
|
| 355 |
+
max-height: 800px;
|
| 356 |
+
overflow-y: auto;
|
| 357 |
+
padding-right: 10px;
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
.recipe-output h2 {
|
| 361 |
+
color: var(--primary-color);
|
| 362 |
+
border-bottom: 1px solid var(--secondary-color);
|
| 363 |
+
padding-bottom: 5px;
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
+
.recipe-output h3 {
|
| 367 |
+
color: var(--secondary-color);
|
| 368 |
+
}
|
| 369 |
+
|
| 370 |
/* Responsive styles */
|
| 371 |
@media (max-width: 768px) {
|
| 372 |
.app-title {
|