A complete system for tracking user food preferences based on food waste analysis using OpenAI Vision API and MongoDB.
- πΈ Analyze food waste from images using OpenAI GPT-4o Vision
- π€ Track individual user food preferences (likes/dislikes)
- π Store meal history and waste statistics
- π Automatically update preferences without duplicates
- π RESTful API for easy integration
- food_waste_analyzer_v2.py - Analyzes food images using OpenAI Vision API
- user_preference_manager.py - MongoDB-based preference management
- api.py - Flask REST API for client integration
- Python 3.8+
- MongoDB (local or cloud)
- OpenAI API key
# Install dependencies
pip install -r requirements_full.txt
# Start MongoDB (if running locally)
mongod
# Set your OpenAI API key in food_waste_analyzer_v2.py
# Line 7: openai.api_key = "your-api-key-here"
# Run the API server
python api.pyThe API will start on http://localhost:5000
Analyze a meal and update user preferences.
Endpoint: POST /api/user/preferences/update
Request Body:
{
"user_id": "user123",
"waste_analysis": {
"original_meal": {
"name": "Loaded Fries",
"description": "Fries topped with cheese and jalapenos"
},
"thrown_away": [
{
"item": "fries",
"quantity": "1/4 cup",
"percentage_of_original": "30%"
}
],
"eaten": [
{
"item": "fries",
"quantity": "2/3 cup",
"percentage_of_original": "70%"
}
],
"food_preferences": {
"likely_dislikes": ["toppings"],
"likely_likes": ["fries"],
"insights": "Person prefers plain fries"
},
"waste_summary": {
"total_waste_percentage": "35%",
"waste_value": "medium"
}
}
}Response:
{
"success": true,
"user": {
"user_id": "user123",
"liked_foods": ["fries", "chicken", "rice"],
"disliked_foods": ["toppings", "broccoli"],
"meal_count": 5,
"total_waste_percentage": 28.5,
"updated_at": "2026-01-31T10:30:00"
},
"message": "Preferences updated successfully"
}Get comprehensive user information.
Endpoint: GET /api/user/<user_id>/summary
Response:
{
"success": true,
"summary": {
"user_id": "user123",
"user_name": null,
"liked_foods": ["fries", "chicken", "rice"],
"disliked_foods": ["toppings", "broccoli"],
"total_meals_analyzed": 5,
"average_waste_percentage": 28.5,
"recent_meals": [
{
"meal_name": "Loaded Fries",
"timestamp": "2026-01-31T10:30:00",
"waste_percentage": "35%"
}
]
}
}Retrieve user's meal history.
Endpoint: GET /api/user/<user_id>/history?limit=10
Response:
{
"success": true,
"history": [
{
"user_id": "user123",
"timestamp": "2026-01-31T10:30:00",
"original_meal": {
"name": "Loaded Fries",
"description": "..."
},
"thrown_away": [...],
"eaten": [...]
}
],
"count": 1
}Create a new user.
Endpoint: POST /api/user/create
Request Body:
{
"user_id": "user123",
"user_name": "John Doe"
}Get basic user info.
Endpoint: GET /api/user/<user_id>
Delete user and all their data.
Endpoint: DELETE /api/user/<user_id>
Check API status.
Endpoint: GET /api/health
from food_waste_analyzer_v2 import analyze_food_waste
import requests
# Analyze image
result = analyze_food_waste("leftover_meal.jpg")
print(result)import requests
# Send to API
response = requests.post(
'http://localhost:5000/api/user/preferences/update',
json={
'user_id': 'user123',
'waste_analysis': result
}
)
print(response.json())response = requests.get('http://localhost:5000/api/user/user123/summary')
print(response.json()){
user_id: "user123", // Unique identifier
user_name: "John Doe", // Optional
liked_foods: ["fries", ...], // Array of liked foods
disliked_foods: ["broccoli", ...], // Array of disliked foods
meal_count: 5, // Total meals analyzed
total_waste_percentage: 28.5, // Average waste across all meals
created_at: ISODate("..."),
updated_at: ISODate("...")
}{
user_id: "user123",
timestamp: ISODate("..."),
original_meal: {...},
thrown_away: [...],
eaten: [...],
food_preferences: {...},
waste_summary: {...}
}- Foods are normalized (lowercase, trimmed)
- Set-based logic prevents duplicates
- If a food moves from dislike to like, it's automatically removed from dislikes
- Average waste percentage calculated across all meals
- Meal count tracking
- Historical data preservation
- Preferences update based on new data
- If someone starts liking something they disliked, it updates automatically
- All changes are tracked in meal history
# Create user
curl -X POST http://localhost:5000/api/user/create \
-H "Content-Type: application/json" \
-d '{"user_id": "test_user", "user_name": "Test User"}'
# Update preferences
curl -X POST http://localhost:5000/api/user/preferences/update \
-H "Content-Type: application/json" \
-d @example_request.json
# Get summary
curl http://localhost:5000/api/user/test_user/summary
# Get history
curl http://localhost:5000/api/user/test_user/history?limit=5export MONGODB_URI="mongodb://localhost:27017/"
export DB_NAME="food_preferences"
export OPENAI_API_KEY="your-api-key"All endpoints return consistent error responses:
{
"success": false,
"error": "Error message here"
}HTTP Status Codes:
- 200: Success
- 201: Created
- 400: Bad Request
- 404: Not Found
- 409: Conflict (user already exists)
- 500: Internal Server Error
MIT