Spaces:
Running
Running
File size: 1,689 Bytes
c30b4ba cd9c484 c30b4ba | 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 | from flask import Flask, request, jsonify
import google.generativeai as genai
import PIL.Image
import io
import os
from dotenv import load_dotenv
app = Flask(__name__)
load_dotenv()
# Configure Gemini API - get key from https://makersuite.google.com/app/apikey
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
genai.configure(api_key=GOOGLE_API_KEY)
# Initialize the model - UPDATED MODEL NAME HERE
model = genai.GenerativeModel('gemini-2.5-flash')
def analyze_food_image(image_content) -> str:
"""
Analyze image using Gemini API and return food description
"""
try:
prompt = """
Look at this food image and:
1. Identify the main dish/food item
2. List visible ingredients or components, including individual words/strings of the main dish
3. Return ONLY a simple description in this format: [main dish], [ingredients]
For example: "pizza, pizza, cheese, tomatoes, basil" or "chocolate cake, chocolate, cake, frosting, berries"
"""
# Convert bytes to PIL Image
image_bytes = image_content.read()
image = PIL.Image.open(io.BytesIO(image_bytes))
# Generate response
response = model.generate_content([prompt, image])
# Clean and format the response
description = response.text.strip().lower()
description = description.replace('"', '').replace("'", '')
print(description) # For testing purpose
return description if description else "food dish"
except Exception as e:
print(f"Error in analysis: {str(e)}")
return f"food dish (Error: {str(e)})"
if __name__ == '__main__':
pass |