File size: 11,046 Bytes
c005ef1 42ecad8 9f9138d c005ef1 42ecad8 7c29aab 42ecad8 ed8445d 01d7ddf ed8445d 42ecad8 c005ef1 42ecad8 c005ef1 42ecad8 c005ef1 b88bbe1 c005ef1 42ecad8 c005ef1 9f9138d 7c29aab 9f9138d b88bbe1 7c29aab 9f9138d 7c29aab 42ecad8 7c29aab 42ecad8 7c29aab 42ecad8 7c29aab 42ecad8 7c29aab c005ef1 42ecad8 7c29aab 9f9138d 7c29aab 9f9138d 42ecad8 9f9138d 42ecad8 e30a956 c005ef1 |
1 2 3 4 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 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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
from fastapi import FastAPI, File, UploadFile, HTTPException, Form
import google.generativeai as genai
from PIL import Image
import io
import json
import re
import os
import uvicorn
from enum import Enum
app = FastAPI()
# Secure API key retrieval
api_key = "AIzaSyAQLgLNZmeCpSbToD--5PUT1ewXfGZkllc"
if not api_key:
raise RuntimeError("GOOGLE_API_KEY is not set in the environment variables.")
genai.configure(api_key=api_key)
class AnalysisType(str, Enum):
DETECT_ALLERGENS = "detect_allergens"
DETAILED_NUTRIENT_INFO = "detailed_nutrient_info"
LEARN_ABOUT_FOOD = "learn_about_food"
FUN_FACTS = "fun_facts"
GENERATE_DISH = "generate_dish"
@app.get("/")
def read_root():
return {"message": "Food Analysis API is running!"}
@app.head("/")
async def root_head():
return {} # Empty response for HEAD requests
@app.post("/analyze-food")
async def analyze_food_image(
file: UploadFile = File(...),
analysis_type: AnalysisType = Form(...)
):
"""Analyzes food items in the image based on the selected analysis type"""
# Read the uploaded image file
image_bytes = await file.read()
img = Image.open(io.BytesIO(image_bytes))
# Load the generative model
model = genai.GenerativeModel("gemini-2.0-flash") # Optimized for real-time image processing
# Define prompts based on analysis type
prompts = {
AnalysisType.DETECT_ALLERGENS: """
Identify all food items in the given image and list potential allergens.
Return the response in valid JSON format following this structure:
```json
{
"detected_food_items": [
{
"food_item": "Detected food name",
"quantity": "Approximate quantity",
"potential_allergens": ["allergen1", "allergen2"],
"allergen_severity": {
"allergen1": "high/medium/low",
"allergen2": "high/medium/low"
},
"common_cross_reactivity": ["other allergen1", "other allergen2"]
}
]
}
```
Ensure the response is valid JSON inside triple backticks.
""",
AnalysisType.DETAILED_NUTRIENT_INFO: """
Identify all food items in the given image and provide comprehensive nutritional information.
Return the response in valid JSON format following this structure:
```json
{
"detected_food_items": [
{
"food_item": "Detected food name",
"quantity": "Approximate quantity",
"nutritional_info": {
"calories_kcal": value,
"protein_g": value,
"carbohydrates_g": value,
"fat_g": value,
"fiber_g": value,
"sugar_g": value,
"sodium_mg": value,
"potassium_mg": value,
"calcium_mg": value,
"iron_mg": value,
"vitamins": {
"Vitamin A_mcg": value,
"Vitamin C_mg": value,
"Vitamin D_mcg": value,
"Vitamin E_mg": value,
"Vitamin K_mcg": value,
"Vitamin B1_mg": value,
"Vitamin B2_mg": value,
"Vitamin B3_mg": value,
"Vitamin B6_mg": value,
"Vitamin B12_mcg": value,
"Folate_mcg": value
},
"minerals": {
"Magnesium_mg": value,
"Zinc_mg": value,
"Selenium_mcg": value,
"Phosphorus_mg": value
}
},
"glycemic_index": value,
"macronutrient_ratio": {
"protein_percent": value,
"carbs_percent": value,
"fat_percent": value
}
}
],
"total_meal_nutrition": {
"calories_kcal": value,
"protein_g": value,
"carbohydrates_g": value,
"fat_g": value
}
}
```
Ensure the response is valid JSON inside triple backticks.
""",
AnalysisType.LEARN_ABOUT_FOOD: """
Identify all food items in the given image and provide educational information about them.
Return the response in valid JSON format following this structure:
```json
{
"detected_food_items": [
{
"food_item": "Detected food name",
"origin": "Geographic origin of the food",
"cultural_significance": "Brief description of cultural importance",
"history": "Brief history of the food",
"preparation_methods": ["method1", "method2"],
"key_ingredients": ["ingredient1", "ingredient2"],
"nutritional_highlights": ["highlight1", "highlight2"],
"health_benefits": ["benefit1", "benefit2"],
"interesting_facts": ["fact1", "fact2"]
}
]
}
```
Ensure the response is valid JSON inside triple backticks.
""",
AnalysisType.FUN_FACTS: """
Identify all food items in the given image and provide fun and interesting facts about them.
Return the response in valid JSON format following this structure:
```json
{
"detected_food_items": [
{
"food_item": "Detected food name",
"fun_facts": [
"Fun fact 1 about this food",
"Fun fact 2 about this food",
"Fun fact 3 about this food"
],
"did_you_know": "An interesting surprising fact",
"world_records": ["Any world records related to this food"],
"pop_culture_references": ["How this food appears in movies, TV, etc."],
"weird_traditions": ["Strange traditions involving this food"]
}
]
}
```
Ensure the response is valid JSON inside triple backticks.
""",
AnalysisType.GENERATE_DISH: """
Identify all food items in the given image and suggest creative dishes that can be made with them.
Return the response in valid JSON format following this structure:
```json
{
"detected_ingredients": ["ingredient1", "ingredient2"],
"suggested_dishes": [
{
"dish_name": "Creative dish name",
"cuisine_type": "Type of cuisine",
"difficulty_level": "easy/medium/hard",
"preparation_time_minutes": value,
"ingredients": {
"from_image": ["ingredient1", "ingredient2"],
"additional_needed": ["extra1", "extra2"]
},
"recipe_steps": [
"Step 1 description",
"Step 2 description"
],
"nutritional_highlights": ["highlight1", "highlight2"],
"serving_suggestions": ["suggestion1", "suggestion2"]
}
]
}
```
Ensure the response is valid JSON inside triple backticks.
"""
}
# Get the appropriate prompt
prompt = prompts.get(analysis_type)
if not prompt:
raise HTTPException(status_code=400, detail="Invalid analysis type")
# Get response from API
response = model.generate_content([prompt, img])
# Extract JSON response
try:
# Use regex to extract JSON block
match = re.search(r'```json\s*(\{.*?\})\s*```', response.text, re.DOTALL)
if match:
json_response = match.group(1) # Extract JSON content
return json.loads(json_response) # Convert to Python dictionary
else:
return {"error": "Response does not contain valid JSON format", "raw_response": response.text}
except json.JSONDecodeError:
return {"error": "Failed to parse JSON response", "raw_response": response.text}
# Keep the original analyze endpoint for backward compatibility
@app.post("/analyze")
async def analyze_food_image_original(file: UploadFile = File()):
"""Analyzes food items in the image using Gemini 1.5 Flash (original endpoint)"""
# Read the uploaded image file
image_bytes = await file.read()
img = Image.open(io.BytesIO(image_bytes))
# Load the generative model
model = genai.GenerativeModel("gemini-2.0-flash") # Optimized for real-time image processing
# Updated structured JSON prompt
prompt = """
Identify all food items in the given image and determine their approximate quantity. Then, provide nutritional information
in valid JSON format following this structure:
```json
{
"detected_food_items": [
{
"food_item": "Detected food name",
"quantity": "Approximate quantity (e.g., 1 bowl, 2 slices, half a chapati)",
"nutritional_info": {
"calories_kcal": value,
"protein_g": value,
"fiber_g": value,
"vitamins": {
"Vitamin A_mcg": value,
"Vitamin C_mg": value,
"Vitamin D_mcg": value,
"Vitamin E_mg": value,
"Vitamin K_mcg": value,
"Vitamin B1_mg": value,
"Vitamin B2_mg": value,
"Vitamin B3_mg": value,
"Vitamin B6_mg": value,
"Vitamin B12_mcg": value,
"Folate_mcg": value
}
},
"health_benefits": [
"Brief description of health benefit 1",
"Brief description of health benefit 2"
]
}
]
}
```
- **Ensure the response is valid JSON** inside triple backticks (```json ... ```).
- **Include accurate nutritional values based on the given quantity.**
- **If exact values are unavailable, provide estimated values.**
- **Ensure proper formatting and completeness of data.**
"""
# Get response from API
response = model.generate_content([prompt, img])
# Extract JSON response
try:
# Use regex to extract JSON block
match = re.search(r'```json\s*(\{.*?\})\s*```', response.text, re.DOTALL)
if match:
json_response = match.group(1) # Extract JSON content
return json.loads(json_response) # Convert to Python dictionary
else:
return {"error": "Response does not contain valid JSON format", "raw_response": response.text}
except json.JSONDecodeError:
return {"error": "Failed to parse JSON response", "raw_response": response.text}
# Entry point for Render deployment
if __name__ == "__main__":
port = int(os.getenv("PORT", 7860)) # Render assigns PORT dynamically
uvicorn.run(app, host="0.0.0.0", port=port) |