Edamam API and dish volume estimation
Build, push image, and notify Watchtower / build-image (push) Successful in 2m33s
Build, push image, and notify Watchtower / notify (push) Successful in 9s

This commit is contained in:
2026-07-25 16:31:50 +02:00
parent a0af426c78
commit 6a42e4a772
8 changed files with 1135 additions and 4 deletions
+2
View File
@@ -0,0 +1,2 @@
from APIs.aichat import *
from APIs.edamam import *
+102
View File
@@ -0,0 +1,102 @@
import os
import base64
import json
import urllib.request
import urllib.error
API_HOST = os.getenv("OPENAI_API_HOST", "https://chat.matthiasg.dev/ollama")
AI_MODEL = os.getenv("OPENAI_MODEL", "llava:7b-v1.6-mistral-q4_1")
AI_MODEL_THINK = os.getenv("OPENAI_MODEL_THINK", "True").lower() in ("true", "1", "t")
OPENAPI_TOKEN = os.getenv("OPENAI_API_TOKEN", None)
OPENAPI_ENDPOINT = "/api/generate"
print(f"Using API Host: {API_HOST}")
print(f"Using API Model: {AI_MODEL}")
print(f"Using API Model Think: {AI_MODEL_THINK}")
print(f"Using API Token: {'Yes' if OPENAPI_TOKEN else 'No'} {OPENAPI_TOKEN[:5] + '...' if OPENAPI_TOKEN else ''}")
def call_api(body: dict, endpoint: str = OPENAPI_ENDPOINT) -> str:
"""Call the API with the given endpoint and body dict."""
url = f"{API_HOST}{endpoint}"
headers = {
"Content-Type": "application/json",
}
if OPENAPI_TOKEN:
headers["Authorization"] = f"Bearer {OPENAPI_TOKEN}"
json_data = json.dumps(body).encode("utf-8")
req = urllib.request.Request(url, data=json_data, headers=headers, method="POST")
try:
with urllib.request.urlopen(req) as response:
return response.read().decode("utf-8")
except urllib.error.HTTPError as e:
error_body = e.read().decode("utf-8")
raise Exception(f"Error calling API: HTTP {e.code} - {error_body}")
except urllib.error.URLError as e:
raise Exception(f"Failed to reach server: {e.reason}")
def generate(
model: str = AI_MODEL,
prompt: str = "",
images: list[str] = None,
output_format: str = None,
system_message: str = None,
keep_alive: bool = True,
should_think: bool = AI_MODEL_THINK,
) -> str:
"""
Generate a response for a given prompt with a provided model via the Ollama/OpenAI API.
Handles base64 encoding for local image file paths and structures the request body.
"""
if images is None:
images = []
# Transform image file paths to base64 strings
encoded_images = []
for img_path in images:
if os.path.isfile(img_path):
with open(img_path, "rb") as image_file:
encoded_images.append(base64.b64encode(image_file.read()).decode("utf-8"))
else:
# If it's already a base64 string or an invalid path, keep as-is
encoded_images.append(img_path)
body = {
"model": model,
"prompt": prompt,
"images": encoded_images,
"think": should_think,
"stream": False,
}
if system_message is not None:
body["system"] = system_message
if output_format is not None:
try:
body["format"] = json.loads(output_format)
except json.JSONDecodeError:
body["format"] = output_format
if not keep_alive:
body["keep_alive"] = "0m"
response_text = call_api(body)
try:
decoded_response = json.loads(response_text)
except json.JSONDecodeError as e:
raise Exception(f"Error decoding JSON response: {e}")
return decoded_response.get("response", "")
if __name__ == "__main__":
# Example usage:
result = generate(
prompt="Explain what you see in the image or answer this prompt.",
should_think=AI_MODEL_THINK,
)
print(result)
+937
View File
@@ -0,0 +1,937 @@
import os
import requests
from typing import Dict, Any, Optional
EDAMAM_APP_ID__FOOD = os.getenv("EDAMAM_APP_ID__FOOD", None)
EDAMAM_APP_KEY__FOOD = os.getenv("EDAMAM_APP_KEY__FOOD", None)
class EdamamAPI:
"""Wrapper around Edamam Food Database API 2.0 (Vision & Nutrients)"""
BASE_URL = "https://api.edamam.com/api/vision/v1/nutrients"
def __init__(self):
pass
def analyze_dish_image(self, image_file_path: str):
url = "https://api.edamam.com/api/vision/v1/nutrients"
# Query parameters as specified by the YAML schema
params = {
"app_id": EDAMAM_APP_ID__FOOD,
"app_key": EDAMAM_APP_KEY__FOOD,
"beta": "true" # Recommended since vision is in Beta
}
print("params:", params)
# Specify the media type in the header
headers = {
"Content-Type": "image/jpeg"
}
# Read and send the raw binary bytes directly in data (NOT files= or json=)
with open(image_file_path, "rb") as image_file:
raw_image_bytes = image_file.read()
response = requests.post(
url,
params=params,
headers=headers,
data=raw_image_bytes
)
print(f"Edamam API response : {response.json()}")
response.raise_for_status()
return response.json()
# Returns something like:
"""
{
"combined": {
"problems": [],
"recipe": {
"calories": 306,
"cautions": [
"GLUTEN",
"WHEAT",
"MILK",
"SULFITES",
"FODMAP"
],
"dietLabels": [],
"healthLabels": [
"EGG_FREE",
"PEANUT_FREE",
"TREE_NUT_FREE",
"SOY_FREE",
"FISH_FREE",
"SHELLFISH_FREE",
"CRUSTACEAN_FREE",
"CELERY_FREE",
"MUSTARD_FREE",
"SESAME_FREE",
"LUPINE_FREE",
"MOLLUSK_FREE",
"ALCOHOL_FREE"
],
"ingredientLines": [
"90 g (3 oz) Beef chunks",
"100 g (0.75 cup) Potatoes, cubed",
"40 g (0.33 cup) Carrots, sliced",
"30 g (0.25 cup) Green beans",
"120 g (0.5 cup) Brown gravy or stew sauce",
"Salt and pepper to taste"
],
"label": "Beef Stew with Vegetables (Frozen Meal)",
"totalDaily": {
"CA": {
"label": "Calcium",
"quantity": 9.808827779627787,
"unit": "%"
},
"CHOCDF": {
"label": "Carbs",
"quantity": 9.07220373540042,
"unit": "%"
},
"CHOCDF.net": {
"label": "Carbohydrates (net)",
"quantity": 0.0,
"unit": "g"
},
"CHOLE": {
"label": "Cholesterol",
"quantity": 20.837056709550403,
"unit": "%"
},
"ENERC_KCAL": {
"label": "Energy",
"quantity": 13.100053025164515,
"unit": "%"
},
"FASAT": {
"label": "Saturated",
"quantity": 17.148508115376796,
"unit": "%"
},
"FAT": {
"label": "Fat",
"quantity": 14.902089676480887,
"unit": "%"
},
"FE": {
"label": "Iron",
"quantity": 21.951689781236098,
"unit": "%"
},
"FIBTG": {
"label": "Fiber",
"quantity": 13.858671762525935,
"unit": "%"
},
"FOLDFE": {
"label": "Folate equivalent (total)",
"quantity": 10.817828789332737,
"unit": "%"
},
"K": {
"label": "Potassium",
"quantity": 28.904445650775536,
"unit": "%"
},
"MG": {
"label": "Magnesium",
"quantity": 15.863016599088612,
"unit": "%"
},
"NA": {
"label": "Sodium",
"quantity": 60.46248,
"unit": "%"
},
"NIA": {
"label": "Niacin (B3)",
"quantity": 66.37252070251446,
"unit": "%"
},
"P": {
"label": "Phosphorus",
"quantity": 25.034580972170218,
"unit": "%"
},
"PROCNT": {
"label": "Protein",
"quantity": 21.91938193709794,
"unit": "%"
},
"RIBF": {
"label": "Riboflavin (B2)",
"quantity": 73.88477475545037,
"unit": "%"
},
"THIA": {
"label": "Thiamin (B1)",
"quantity": 84.12049444050538,
"unit": "%"
},
"TOCPHA": {
"label": "Vitamin E",
"quantity": 12.990953399638641,
"unit": "%"
},
"VITA_RAE": {
"label": "Vitamin A",
"quantity": 45.87938825251855,
"unit": "%"
},
"VITB12": {
"label": "Vitamin B12",
"quantity": 113.07016020542191,
"unit": "%"
},
"VITB6A": {
"label": "Vitamin B6",
"quantity": 90.1530888403073,
"unit": "%"
},
"VITC": {
"label": "Vitamin C",
"quantity": 35.18207820897409,
"unit": "%"
},
"VITD": {
"label": "Vitamin D",
"quantity": 13.66228503481425,
"unit": "%"
},
"VITK1": {
"label": "Vitamin K",
"quantity": 27.534449511962567,
"unit": "%"
},
"ZN": {
"label": "Zinc",
"quantity": 55.091388643252344,
"unit": "%"
}
},
"totalNutrients": {
"CA": {
"label": "Calcium",
"quantity": 127.51476113516124,
"unit": "mg"
},
"CHOCDF": {
"label": "Carbs",
"quantity": 26.573051753721295,
"unit": "g"
},
"CHOCDF.net": {
"label": "Carbohydrates (net)",
"quantity": 21.30675648396144,
"unit": "g"
},
"CHOLE": {
"label": "Cholesterol",
"quantity": 73.23964970198097,
"unit": "mg"
},
"ENERC_KCAL": {
"label": "Energy",
"quantity": 306.9669925121675,
"unit": "kcal"
},
"FAMS": {
"label": "Monounsaturated",
"quantity": 5.622055495052523,
"unit": "g"
},
"FAPU": {
"label": "Polyunsaturated",
"quantity": 1.5770619917608195,
"unit": "g"
},
"FASAT": {
"label": "Saturated",
"quantity": 4.0183241641356675,
"unit": "g"
},
"FAT": {
"label": "Fat",
"quantity": 11.639773878137946,
"unit": "g"
},
"FATRN": {
"label": "Trans",
"quantity": 0.24391676668519283,
"unit": "g"
},
"FE": {
"label": "Iron",
"quantity": 3.9513041606224975,
"unit": "mg"
},
"FIBTG": {
"label": "Fiber",
"quantity": 5.266295269759856,
"unit": "g"
},
"FOLAC": {
"label": "Folic acid",
"quantity": 18.024043373983595,
"unit": "\u00b5g"
},
"FOLDFE": {
"label": "Folate equivalent (total)",
"quantity": 43.271315157330946,
"unit": "\u00b5g"
},
"FOLFD": {
"label": "Folate (food)",
"quantity": 40.54129806334046,
"unit": "\u00b5g"
},
"K": {
"label": "Potassium",
"quantity": 982.7511521263683,
"unit": "mg"
},
"MG": {
"label": "Magnesium",
"quantity": 66.62466971617216,
"unit": "mg"
},
"NA": {
"label": "Sodium",
"quantity": 906.9372000000001,
"unit": "mg"
},
"NIA": {
"label": "Niacin (B3)",
"quantity": 10.619603312402313,
"unit": "mg"
},
"P": {
"label": "Phosphorus",
"quantity": 312.93226215212775,
"unit": "mg"
},
"PROCNT": {
"label": "Protein",
"quantity": 25.68129586205237,
"unit": "g"
},
"RIBF": {
"label": "Riboflavin (B2)",
"quantity": 0.9605020718208549,
"unit": "mg"
},
"SUGAR": {
"label": "Sugars",
"quantity": 8.053516658227332,
"unit": "g"
},
"SUGAR.added": {
"label": "Sugars, added",
"quantity": 2.3940423830596225,
"unit": "g"
},
"Sugar.alcohol": {
"label": "Sugar alcohol",
"quantity": 0.0,
"unit": "g"
},
"THIA": {
"label": "Thiamin (B1)",
"quantity": 1.0094459332860646,
"unit": "mg"
},
"TOCPHA": {
"label": "Vitamin E",
"quantity": 1.9486430099457963,
"unit": "mg"
},
"VITA_RAE": {
"label": "Vitamin A",
"quantity": 412.914494272667,
"unit": "\u00b5g"
},
"VITB12": {
"label": "Vitamin B12",
"quantity": 2.7136838449301255,
"unit": "\u00b5g"
},
"VITB6A": {
"label": "Vitamin B6",
"quantity": 1.5326025102852243,
"unit": "mg"
},
"VITC": {
"label": "Vitamin C",
"quantity": 31.66387038807668,
"unit": "mg"
},
"VITD": {
"label": "Vitamin D",
"quantity": 2.73245700696285,
"unit": "\u00b5g"
},
"VITK1": {
"label": "Vitamin K",
"quantity": 33.04133941435508,
"unit": "\u00b5g"
},
"WATER": {
"label": "Water",
"quantity": 321.1285604946806,
"unit": "g"
},
"ZN": {
"label": "Zinc",
"quantity": 6.060052750757758,
"unit": "mg"
}
},
"totalWeight": 390.7575715150228,
"translations": [
{
"id": "EGG_FREE",
"label": "Egg-Free",
"type": "health"
},
{
"id": "PEANUT_FREE",
"label": "Peanut-Free",
"type": "health"
},
{
"id": "TREE_NUT_FREE",
"label": "Tree-Nut-Free",
"type": "health"
},
{
"id": "SOY_FREE",
"label": "Soy-Free",
"type": "health"
},
{
"id": "FISH_FREE",
"label": "Fish-Free",
"type": "health"
},
{
"id": "SHELLFISH_FREE",
"label": "Shellfish-Free",
"type": "health"
},
{
"id": "CRUSTACEAN_FREE",
"label": "Crustacean-Free",
"type": "health"
},
{
"id": "CELERY_FREE",
"label": "Celery-Free",
"type": "health"
},
{
"id": "MUSTARD_FREE",
"label": "Mustard-Free",
"type": "health"
},
{
"id": "SESAME_FREE",
"label": "Sesame-Free",
"type": "health"
},
{
"id": "LUPINE_FREE",
"label": "Lupine-Free",
"type": "health"
},
{
"id": "MOLLUSK_FREE",
"label": "Mollusk-Free",
"type": "health"
},
{
"id": "ALCOHOL_FREE",
"label": "Alcohol-Free",
"type": "health"
},
{
"id": "GLUTEN",
"label": "Gluten",
"type": "caution"
},
{
"id": "WHEAT",
"label": "Wheat",
"type": "caution"
},
{
"id": "MILK",
"label": "Milk",
"type": "caution"
},
{
"id": "SULFITES",
"label": "Sulfites",
"type": "caution"
},
{
"id": "FODMAP",
"label": "FODMAP",
"type": "caution"
}
],
"uri": "http://www.edamam.com/ontologies/edamam.owl#recipe_e59b15e71cd34bf4b7c02c2f545e40f3",
"yield": 1.0
}
},
"dishes": [
{
"problems": [],
"recipe": {
"calories": 306,
"cautions": [
"GLUTEN",
"WHEAT",
"MILK",
"SULFITES",
"FODMAP"
],
"dietLabels": [],
"healthLabels": [
"EGG_FREE",
"PEANUT_FREE",
"TREE_NUT_FREE",
"SOY_FREE",
"FISH_FREE",
"SHELLFISH_FREE",
"CRUSTACEAN_FREE",
"CELERY_FREE",
"MUSTARD_FREE",
"SESAME_FREE",
"LUPINE_FREE",
"MOLLUSK_FREE",
"ALCOHOL_FREE"
],
"ingredientLines": [
"90 g (3 oz) Beef chunks",
"100 g (0.75 cup) Potatoes, cubed",
"40 g (0.33 cup) Carrots, sliced",
"30 g (0.25 cup) Green beans",
"120 g (0.5 cup) Brown gravy or stew sauce",
"Salt and pepper to taste"
],
"label": "Beef Stew with Vegetables (Frozen Meal)",
"totalDaily": {
"CA": {
"label": "Calcium",
"quantity": 9.808827779627787,
"unit": "%"
},
"CHOCDF": {
"label": "Carbs",
"quantity": 9.07220373540042,
"unit": "%"
},
"CHOCDF.net": {
"label": "Carbohydrates (net)",
"quantity": 0.0,
"unit": "g"
},
"CHOLE": {
"label": "Cholesterol",
"quantity": 20.837056709550403,
"unit": "%"
},
"ENERC_KCAL": {
"label": "Energy",
"quantity": 13.100053025164515,
"unit": "%"
},
"FASAT": {
"label": "Saturated",
"quantity": 17.148508115376796,
"unit": "%"
},
"FAT": {
"label": "Fat",
"quantity": 14.902089676480887,
"unit": "%"
},
"FE": {
"label": "Iron",
"quantity": 21.951689781236098,
"unit": "%"
},
"FIBTG": {
"label": "Fiber",
"quantity": 13.858671762525935,
"unit": "%"
},
"FOLDFE": {
"label": "Folate equivalent (total)",
"quantity": 10.817828789332737,
"unit": "%"
},
"K": {
"label": "Potassium",
"quantity": 28.904445650775536,
"unit": "%"
},
"MG": {
"label": "Magnesium",
"quantity": 15.863016599088612,
"unit": "%"
},
"NA": {
"label": "Sodium",
"quantity": 60.46248,
"unit": "%"
},
"NIA": {
"label": "Niacin (B3)",
"quantity": 66.37252070251446,
"unit": "%"
},
"P": {
"label": "Phosphorus",
"quantity": 25.034580972170218,
"unit": "%"
},
"PROCNT": {
"label": "Protein",
"quantity": 21.91938193709794,
"unit": "%"
},
"RIBF": {
"label": "Riboflavin (B2)",
"quantity": 73.88477475545037,
"unit": "%"
},
"THIA": {
"label": "Thiamin (B1)",
"quantity": 84.12049444050538,
"unit": "%"
},
"TOCPHA": {
"label": "Vitamin E",
"quantity": 12.990953399638641,
"unit": "%"
},
"VITA_RAE": {
"label": "Vitamin A",
"quantity": 45.87938825251855,
"unit": "%"
},
"VITB12": {
"label": "Vitamin B12",
"quantity": 113.07016020542191,
"unit": "%"
},
"VITB6A": {
"label": "Vitamin B6",
"quantity": 90.1530888403073,
"unit": "%"
},
"VITC": {
"label": "Vitamin C",
"quantity": 35.18207820897409,
"unit": "%"
},
"VITD": {
"label": "Vitamin D",
"quantity": 13.66228503481425,
"unit": "%"
},
"VITK1": {
"label": "Vitamin K",
"quantity": 27.534449511962567,
"unit": "%"
},
"ZN": {
"label": "Zinc",
"quantity": 55.091388643252344,
"unit": "%"
}
},
"totalNutrients": {
"CA": {
"label": "Calcium",
"quantity": 127.51476113516124,
"unit": "mg"
},
"CHOCDF": {
"label": "Carbs",
"quantity": 26.573051753721295,
"unit": "g"
},
"CHOCDF.net": {
"label": "Carbohydrates (net)",
"quantity": 21.30675648396144,
"unit": "g"
},
"CHOLE": {
"label": "Cholesterol",
"quantity": 73.23964970198097,
"unit": "mg"
},
"ENERC_KCAL": {
"label": "Energy",
"quantity": 306.9669925121675,
"unit": "kcal"
},
"FAMS": {
"label": "Monounsaturated",
"quantity": 5.622055495052523,
"unit": "g"
},
"FAPU": {
"label": "Polyunsaturated",
"quantity": 1.5770619917608195,
"unit": "g"
},
"FASAT": {
"label": "Saturated",
"quantity": 4.0183241641356675,
"unit": "g"
},
"FAT": {
"label": "Fat",
"quantity": 11.639773878137946,
"unit": "g"
},
"FATRN": {
"label": "Trans",
"quantity": 0.24391676668519283,
"unit": "g"
},
"FE": {
"label": "Iron",
"quantity": 3.9513041606224975,
"unit": "mg"
},
"FIBTG": {
"label": "Fiber",
"quantity": 5.266295269759856,
"unit": "g"
},
"FOLAC": {
"label": "Folic acid",
"quantity": 18.024043373983595,
"unit": "\u00b5g"
},
"FOLDFE": {
"label": "Folate equivalent (total)",
"quantity": 43.271315157330946,
"unit": "\u00b5g"
},
"FOLFD": {
"label": "Folate (food)",
"quantity": 40.54129806334046,
"unit": "\u00b5g"
},
"K": {
"label": "Potassium",
"quantity": 982.7511521263683,
"unit": "mg"
},
"MG": {
"label": "Magnesium",
"quantity": 66.62466971617216,
"unit": "mg"
},
"NA": {
"label": "Sodium",
"quantity": 906.9372000000001,
"unit": "mg"
},
"NIA": {
"label": "Niacin (B3)",
"quantity": 10.619603312402313,
"unit": "mg"
},
"P": {
"label": "Phosphorus",
"quantity": 312.93226215212775,
"unit": "mg"
},
"PROCNT": {
"label": "Protein",
"quantity": 25.68129586205237,
"unit": "g"
},
"RIBF": {
"label": "Riboflavin (B2)",
"quantity": 0.9605020718208549,
"unit": "mg"
},
"SUGAR": {
"label": "Sugars",
"quantity": 8.053516658227332,
"unit": "g"
},
"SUGAR.added": {
"label": "Sugars, added",
"quantity": 2.3940423830596225,
"unit": "g"
},
"Sugar.alcohol": {
"label": "Sugar alcohol",
"quantity": 0.0,
"unit": "g"
},
"THIA": {
"label": "Thiamin (B1)",
"quantity": 1.0094459332860646,
"unit": "mg"
},
"TOCPHA": {
"label": "Vitamin E",
"quantity": 1.9486430099457963,
"unit": "mg"
},
"VITA_RAE": {
"label": "Vitamin A",
"quantity": 412.914494272667,
"unit": "\u00b5g"
},
"VITB12": {
"label": "Vitamin B12",
"quantity": 2.7136838449301255,
"unit": "\u00b5g"
},
"VITB6A": {
"label": "Vitamin B6",
"quantity": 1.5326025102852243,
"unit": "mg"
},
"VITC": {
"label": "Vitamin C",
"quantity": 31.66387038807668,
"unit": "mg"
},
"VITD": {
"label": "Vitamin D",
"quantity": 2.73245700696285,
"unit": "\u00b5g"
},
"VITK1": {
"label": "Vitamin K",
"quantity": 33.04133941435508,
"unit": "\u00b5g"
},
"WATER": {
"label": "Water",
"quantity": 321.1285604946806,
"unit": "g"
},
"ZN": {
"label": "Zinc",
"quantity": 6.060052750757758,
"unit": "mg"
}
},
"totalWeight": 390.7575715150228,
"translations": [
{
"id": "EGG_FREE",
"label": "Egg-Free",
"type": "health"
},
{
"id": "PEANUT_FREE",
"label": "Peanut-Free",
"type": "health"
},
{
"id": "TREE_NUT_FREE",
"label": "Tree-Nut-Free",
"type": "health"
},
{
"id": "SOY_FREE",
"label": "Soy-Free",
"type": "health"
},
{
"id": "FISH_FREE",
"label": "Fish-Free",
"type": "health"
},
{
"id": "SHELLFISH_FREE",
"label": "Shellfish-Free",
"type": "health"
},
{
"id": "CRUSTACEAN_FREE",
"label": "Crustacean-Free",
"type": "health"
},
{
"id": "CELERY_FREE",
"label": "Celery-Free",
"type": "health"
},
{
"id": "MUSTARD_FREE",
"label": "Mustard-Free",
"type": "health"
},
{
"id": "SESAME_FREE",
"label": "Sesame-Free",
"type": "health"
},
{
"id": "LUPINE_FREE",
"label": "Lupine-Free",
"type": "health"
},
{
"id": "MOLLUSK_FREE",
"label": "Mollusk-Free",
"type": "health"
},
{
"id": "ALCOHOL_FREE",
"label": "Alcohol-Free",
"type": "health"
},
{
"id": "GLUTEN",
"label": "Gluten",
"type": "caution"
},
{
"id": "WHEAT",
"label": "Wheat",
"type": "caution"
},
{
"id": "MILK",
"label": "Milk",
"type": "caution"
},
{
"id": "SULFITES",
"label": "Sulfites",
"type": "caution"
},
{
"id": "FODMAP",
"label": "FODMAP",
"type": "caution"
}
],
"uri": "http://www.edamam.com/ontologies/edamam.owl#recipe_e59b15e71cd34bf4b7c02c2f545e40f3",
"yield": 1.0
}
}
]
}
"""