Spaces:
Runtime error
Runtime error
init
Browse filesFiles changed (22)
- .dockerignoreAdded+172
- .envAdded+23
- .env_sampleAdded+25
- .gitignoreAdded+169
- DockerfileAdded+22
- README.md+18-1
- data/dietary_restrictions.txtAdded+14
- data/medical_diagnoses.txtAdded+31
- gradio_ui.pyAdded+123
- image_generator.pyAdded+43
- llm.pyAdded+38
- log_util.pyAdded+23
- meal_image_search.pyAdded+81
- prompts/alter_recipe_for_healthier.prompt.txtAdded+29
- prompts/get_image_from_recipe.prompt.txtAdded+11
- prompts/get_recipe_from_image.prompt.txtAdded+20
- prompts/validate_is_meal.prompt.txtAdded+12
- recipe_generator.pyAdded+76
- requirements.txtAdded+7
- templates/recipe.htmlAdded+93
- time_it.pyAdded+55
- util.pyAdded+11
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172# copied verbatim from .gitignore, with changes ##
# Byte-compiled / optimized / DLL files__pycache__/*.py[cod]*$py.class
# C extensions*.so
# Distribution / packaging.Pythonbuild/develop-eggs/dist/downloads/eggs/.eggs/lib/lib64/parts/sdist/var/wheels/share/python-wheels/*.egg-info/.installed.cfg*.eggMANIFEST
# PyInstaller# Usually these files are written by a python script from a template# before PyInstaller builds the exe, so as to inject date/other infos into it.*.manifest*.spec
# Installer logspip-log.txtpip-delete-this-directory.txt
# Unit test / coverage reportshtmlcov/.tox/.nox/.coverage.coverage.*.cachenosetests.xmlcoverage.xml*.cover*.py,cover.hypothesis/.pytest_cache/cover/
# Translations*.mo*.pot
# Django stuff:*.loglocal_settings.pydb.sqlite3db.sqlite3-journal
# Flask stuff:instance/.webassets-cache
# Scrapy stuff:.scrapy
# Sphinx documentationdocs/_build/
# PyBuilder.pybuilder/target/
# Jupyter Notebook.ipynb_checkpoints
# IPythonprofile_default/ipython_config.py
# pyenv# For a library or package, you might want to ignore these files since the code is# intended to run in multiple environments; otherwise, check them in:# .python-version
# pipenv# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.# However, in case of collaboration, if having platform-specific dependencies or dependencies# having no cross-platform support, pipenv may install dependencies that don't work, or not# install all needed dependencies.#Pipfile.lock
# poetry# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.# This is especially recommended for binary packages to ensure reproducibility, and is more# commonly ignored for libraries.# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control#poetry.lock
# pdm# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.#pdm.lock# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it# in version control.# https://pdm.fming.dev/#use-with-ide.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm__pypackages__/
# Celery stuffcelerybeat-schedulecelerybeat.pid
# SageMath parsed files*.sage.py
# Environments##.env.venvenv/venv/ENV/env.bak/venv.bak/
# Spyder project settings.spyderproject.spyproject
# Rope project settings.ropeproject
# mkdocs documentation/site
# mypy.mypy_cache/.dmypy.jsondmypy.json
# Pyre type checker.pyre/
# pytype static type analyzer.pytype/
# Cython debug symbolscython_debug/
# PyCharm# JetBrains specific template is maintained in a separate JetBrains.gitignore that can# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore# and can be added to the global gitignore or merged into this file. For a more nuclear# option (not recommended) you can uncomment the following to ignore the entire idea folder.#.idea/
.vscode/codealike.json
# app specifictest_data/
## for DockerDockerfile.gitignore1234567891011121314151617181920212223######################################################################################################
# SambaNova Hosted Models reference: https://docs.sambanova.ai/cloud/docs/get-started/supported-models
######################################################################################################
LLAMA_API_BASE_URL=https://api.sambanova.ai/v1
## 1. Llama Text LLM
LLAMA_TEXT_MODEL=Meta-Llama-3.3-70B-Instruct
LLAMA_TEXT_MAX_TOKENS=131072
## 2. Llama Vision LLM
LLAMA_VISION_MODEL=Llama-3.2-11B-Vision-Instruct
LLAMA_VISION_MAX_TOKENS=4096
######################################################################################################
# Nebius Hosted Models reference: https://studio.nebius.com/models/text2image
######################################################################################################
IMAGE_GEN_API_BASE_URL=https://api.studio.nebius.ai/v1
## 1. Image Gen Model
IMAGE_GEN_MODEL=black-forest-labs/flux-schnell
IMAGE_GEN_MAX_PROMPT_LEN=200012345678910111213141516171819202122232425######################################################################################################
# SambaNova Hosted Models reference: https://docs.sambanova.ai/cloud/docs/get-started/supported-models
######################################################################################################
LLAMA_API_BASE_URL=https://api.sambanova.ai/v1
LLAMA_API_KEY=xxx
## 1. Llama Text LLM
LLAMA_TEXT_MODEL=Meta-Llama-3.3-70B-Instruct
LLAMA_TEXT_MAX_TOKENS=131072
## 2. Llama Vision LLM
LLAMA_VISION_MODEL=Llama-3.2-11B-Vision-Instruct
LLAMA_VISION_MAX_TOKENS=4096
######################################################################################################
# Nebius Hosted Models reference: https://studio.nebius.com/models/text2image
######################################################################################################
IMAGE_GEN_API_BASE_URL=https://api.studio.nebius.ai/v1
IMAGE_GEN_API_KEY=xxx
## 1. Image Gen Model
IMAGE_GEN_MODEL=black-forest-labs/flux-schnell
IMAGE_GEN_MAX_PROMPT_LEN=2000123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169# Byte-compiled / optimized / DLL files__pycache__/*.py[cod]*$py.class
# C extensions*.so
# Distribution / packaging.Pythonbuild/develop-eggs/dist/downloads/eggs/.eggs/lib/lib64/parts/sdist/var/wheels/share/python-wheels/*.egg-info/.installed.cfg*.eggMANIFEST
# PyInstaller# Usually these files are written by a python script from a template# before PyInstaller builds the exe, so as to inject date/other infos into it.*.manifest*.spec
# Installer logspip-log.txtpip-delete-this-directory.txt
# Unit test / coverage reportshtmlcov/.tox/.nox/.coverage.coverage.*.cachenosetests.xmlcoverage.xml*.cover*.py,cover.hypothesis/.pytest_cache/cover/
# Translations*.mo*.pot
# Django stuff:*.loglocal_settings.pydb.sqlite3db.sqlite3-journal
# Flask stuff:instance/.webassets-cache
# Scrapy stuff:.scrapy
# Sphinx documentationdocs/_build/
# PyBuilder.pybuilder/target/
# Jupyter Notebook.ipynb_checkpoints
# IPythonprofile_default/ipython_config.py
# pyenv# For a library or package, you might want to ignore these files since the code is# intended to run in multiple environments; otherwise, check them in:# .python-version
# pipenv# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.# However, in case of collaboration, if having platform-specific dependencies or dependencies# having no cross-platform support, pipenv may install dependencies that don't work, or not# install all needed dependencies.#Pipfile.lock
# poetry# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.# This is especially recommended for binary packages to ensure reproducibility, and is more# commonly ignored for libraries.# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control#poetry.lock
# pdm# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.#pdm.lock# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it# in version control.# https://pdm.fming.dev/#use-with-ide.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm__pypackages__/
# Celery stuffcelerybeat-schedulecelerybeat.pid
# SageMath parsed files*.sage.py
env.bak/venv.bak/
# Environments# .env (allow .env to be committed).venvenv/venv/ENV/env.bak/venv.bak/
# Spyder project settings.spyderproject.spyproject
# Rope project settings.ropeproject
# mkdocs documentation/site
# mypy.mypy_cache/.dmypy.jsondmypy.json
# Pyre type checker.pyre/
# pytype static type analyzer.pytype/
# Cython debug symbolscython_debug/
# PyCharm# JetBrains specific template is maintained in a separate JetBrains.gitignore that can# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore# and can be added to the global gitignore or merged into this file. For a more nuclear# option (not recommended) you can uncomment the following to ignore the entire idea folder.#.idea/
.vscode/codealike.json
# app specifictest_data/12345678910111213141516171819202122FROM python:3.12-slim-bookworm
# Set the working directory in the containerWORKDIR /app
# Add the requirements file to the containerADD requirements.txt .
# Install the app dependenciesRUN pip install --no-cache-dir -r requirements.txt
# Copy the source code into the containerCOPY . .
# Set env variablesENV PYTHONUNBUFFERED=1
RUN --mount=type=secret,id=LLAMA_API_KEY,mode=0444,required=trueRUN --mount=type=secret,id=IMAGE_GEN_API_KEY,mode=0444,required=true
# Command to run the app on container startupCMD ["python", "-u", "/app/gradio_ui.py"]8 unmodified lines91011121213141516171819202122232425262728298 unmodified linesshort_description: An agent which generates a new 💓health-ai💓 recipe for you!---
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference# HealthAI Chef
An agent which generates a new 💓health-ai💓 recipe for you from just an image of a meal, based on your dietary restrictions and medical diagnoses! For example, you loved eating hamburgers before, but now you have high cholesterol and need to eat vegetarian. Upload your favorite burger photo and the chef agent will scan it using Llama 3 Vision, figure out the recipe for it, use Llama 3 Text to generate a new recipe based on your health needs, and use an image gen model to generate an appetizing picture of the new recipe for you. The agent will show you the old and new pictures and recipes side by side, and even explain what changes it made. Also, if you don't have a meal photo handy, you can use the agent's internet search feature to find one, thanks to Llama's tool calling support.
## Tech Stack
The agent is built in Python 3.12 using the following technologies:
- Llama 3.3 70B Text Instruct hosted on SambaNova- Llama 3.2 11B Vision Instruct hosted on SambaNova- Black Forest Labs Flux Schnell Image Gen hosted on Nebius- LangChain- LangGraph- Gradio- DuckDuckGo Search- Jinja
_Note: these are all open-source._1234567891011121314Dairy-Free
Egg-Free
Gluten-Free
Halal
Kosher
Low-Carb
Low-FODMAP
Low-Sodium
Nut-Free
Paleo
Pescatarian
Soy-Free
Vegan
Vegetarian12345678910111213141516171819202122232425262728293031Anemia
Arthritis
Asthma
Celiac Disease
Diabetes (Type 1)
Diabetes (Type 2)
Digestive Issues
Diverticulitis
Egg Allergy
Fatty Liver Disease (NAFLD/NASH)
Fish Allergy
GERD (Gastroesophageal Reflux Disease)
Gout
Heart Disease
High Blood Pressure
High Cholesterol
Hyperlipidemia
Hypertension
Hypothyroidism
Kidney Stones
Lactose Intolerance
Milk Allergy
Osteoporosis
Peanut Allergy
Prediabetes
Renal Disease
Shellfish Allergy
Soy Allergy
Tree Nut Allergy
Underweight
Wheat Allergy123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123import gradio as gr
from jinja2 import Environment, FileSystemLoader
from log_util import loggerfrom meal_image_search import search_meal_imagefrom recipe_generator import get_altered_recipe, get_image_from_recipe, get_recipe_from_imagefrom util import yield_lines_from_file
BRAND = 'HealthAI'TITLE = f'{BRAND} Chef'
env = Environment(loader=FileSystemLoader('templates'))template = env.get_template('recipe.html')
DISCLAIMER = 'This recipe is a healthier option <em>generated by AI</em>. A registered dietitian can provide expert dietary advice 😀'
async def process_image(file): if not file: yield None, None return
image_path = file.name yield image_path, None
try: recipe = await get_recipe_from_image(image_path) html = template.render(recipe=recipe) yield image_path, html
except Exception as e: logger.error(e) yield None, None if 'not a meal' in str(e).lower(): raise gr.Error("This image doesn't contain a meal.") raise gr.Error("Sorry, this image can't be processed.")
async def get_new_recipe(orig_recipe: str, restrictions: list[str], diagnoses: list[str]): if not orig_recipe or (not restrictions and not diagnoses): yield None, None
if not orig_recipe: raise gr.Error('Please upload a meal pic first.')
if not restrictions and not diagnoses: raise gr.Error('Please select dietary restrictions or medical diagnoses, then try again. I will give you a new recipe based on your selections!')
try: recipe = await get_altered_recipe(orig_recipe, restrictions, diagnoses) recipe['disclaimer'] = DISCLAIMER html = template.render(recipe=recipe) yield html, None
except Exception as e: logger.error(e) yield None, None raise gr.Error(f"Sorry, a new recipe can't be made.")
try: image_url = get_image_from_recipe(recipe) yield html, image_url
except Exception as e: logger.error(e) yield html, None
def search_image(search_text: str) -> str: if not search_text: return None
try: image_url = search_meal_image(search_text) if image_url: return image_url
except Exception as e: logger.error(e)
raise gr.Error(f"Sorry, can't find a meal pic for {search_text}.")
with gr.Blocks(title=TITLE, theme=gr.themes.Monochrome(), css=''' footer {visibility: hidden}
/* make container full width */ .gradio-container { width: 100% !important; /* flll width */ max-width: 100% !important; /* prevent max-width restriction */ margin: 5px 0px 5px 0px !important; /* top, right, bottom, left */ } '''.strip()) as demo:
with gr.Row(): gr.Markdown(f'# {TITLE} 🍽️') new_recipe_button = gr.Button(f'Get {BRAND} Recipe 😋', scale=0)
file_select = gr.File(label='Upload Meal Pic', container=True, file_types=['.jpg', '.jpeg', '.png', '.gif', '.webp'])
search_text = gr.Textbox(placeholder='Or enter a meal to search for an image of it', submit_btn=True, container=False, interactive=True, max_lines=1)
with gr.Row(): restrictions_dropdown = gr.Dropdown(label='Dietary Restrictions', choices=yield_lines_from_file('dietary_restrictions.txt'), interactive=True, multiselect=True, value=None) diagnoses_dropdown = gr.Dropdown(label='Medical Diagnoses', choices=yield_lines_from_file('medical_diagnoses.txt'), interactive=True, multiselect=True, value=None)
with gr.Row(): orig_meal = gr.Image(label='Original Meal', interactive=False) new_meal = gr.Image(label=f'{BRAND} Meal', interactive=False)
with gr.Row(): orig_recipe = gr.HTML(label='Original Recipe', container=True, show_label=True) new_recipe = gr.HTML(label=f'{BRAND} Recipe', container=True, show_label=True)
search_text.submit(search_image, inputs=search_text, outputs=file_select)
file_select.change( process_image, inputs=file_select, outputs=[orig_meal, orig_recipe] )
new_recipe_button.click(fn=get_new_recipe, inputs=[orig_recipe, restrictions_dropdown, diagnoses_dropdown], outputs=[new_recipe, new_meal])
demo.launch(server_name='0.0.0.0')12345678910111213141516171819202122232425262728293031323334353637383940414243import os
from dotenv import load_dotenv
from langchain_core.prompts import PromptTemplate
from openai import OpenAI
from log_util import logger
from time_it import time_it
from util import load_prompt
load_dotenv()
IMAGE_GEN_API_BASE_URL = os.getenv('IMAGE_GEN_API_BASE_URL')
IMAGE_GEN_API_KEY = os.getenv('IMAGE_GEN_API_KEY')
IMAGE_GEN_MODEL = os.getenv('IMAGE_GEN_MODEL')
IMAGE_GEN_MAX_PROMPT_LEN = int(os.getenv('IMAGE_GEN_MAX_PROMPT_LEN'))
IMAGE_GEN_OPTIONS = {
'response_extension': 'png',
'width': 1024,
'height': 1024,
'num_inference_steps': int(os.getenv('NUM_INFERENCE_STEPS', '16')),
'negative_prompt': '',
'seed': -1
}
@time_it
def generate_image(prompt_file: str, input: dict) -> str:
prompt = load_prompt(prompt_file)
if len(prompt) > IMAGE_GEN_MAX_PROMPT_LEN:
logger.info(f'Prompt length {len(prompt)} exceeds {IMAGE_GEN_MAX_PROMPT_LEN} characters, will be truncated.')
prompt = prompt[:IMAGE_GEN_MAX_PROMPT_LEN]
prompt_template = PromptTemplate.from_template(prompt)
prompt = prompt_template.invoke(input).to_string()
images_client = OpenAI(base_url=IMAGE_GEN_API_BASE_URL, api_key=IMAGE_GEN_API_KEY).images
response = images_client.generate(model=IMAGE_GEN_MODEL, prompt=prompt, response_format='url', extra_body=IMAGE_GEN_OPTIONS)
image_url = response.data[0].url
logger.info(f'{image_url=}')
return image_url1234567891011121314151617181920212223242526272829303132333435363738import os
from dotenv import load_dotenvfrom langchain_openai import ChatOpenAI
from log_util import loggerfrom time_it import time_it_async
load_dotenv()
LLAMA_API_BASE_URL = os.getenv('LLAMA_API_BASE_URL')LLAMA_API_KEY = os.getenv('LLAMA_API_KEY')
LLAMA_TEXT_MODEL = os.getenv('LLAMA_TEXT_MODEL')LLAMA_TEXT_MAX_TOKENS = int(os.getenv('LLAMA_TEXT_MAX_TOKENS'))
LLAMA_VISION_MODEL = os.getenv('LLAMA_VISION_MODEL')LLAMA_VISION_MAX_TOKENS = int(os.getenv('LLAMA_VISION_MAX_TOKENS'))
TEMPERATURE = float(os.getenv('TEMPERATURE', '0.2'))
TOKEN_FACTOR_FOR_PROMPT = 0.75
def get_text_llm() -> ChatOpenAI: return _get_llm(LLAMA_TEXT_MODEL, LLAMA_TEXT_MAX_TOKENS)
def get_vision_llm() -> ChatOpenAI: return _get_llm(LLAMA_VISION_MODEL, LLAMA_VISION_MAX_TOKENS)
def _get_llm(model: str, max_tokens: int) -> ChatOpenAI: return ChatOpenAI(base_url=LLAMA_API_BASE_URL, api_key=LLAMA_API_KEY, model=model, max_tokens=int(max_tokens*TOKEN_FACTOR_FOR_PROMPT), temperature=TEMPERATURE)
@time_it_asyncasync def invoke_llm_async(chain, input: dict | None = {}): response = await chain.ainvoke(input) logger.info(f'{response=}') return response1234567891011121314151617181920212223import loggingimport os
from dotenv import load_dotenv
load_dotenv()
PROJECT_NAME = 'healthai-chef'
default_log_args = { 'level': logging.DEBUG if int(os.getenv('DEBUG', '0')) else logging.INFO, 'format': '%(asctime)s [%(levelname)s] %(filename)s:%(lineno)d - %(message)s', 'datefmt': '%d-%b-%y %H:%M:%S', 'force': True,}logging.basicConfig(**default_log_args)logger = logging.getLogger(PROJECT_NAME)
# disable 3rd party logsLOGGERS_TO_DISABLE = []for logger_name in LOGGERS_TO_DISABLE: logging.getLogger(logger_name).setLevel(logging.CRITICAL + 1)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081import json
import os
from dotenv import load_dotenv
from duckduckgo_search import DDGS
from langchain_core.messages.tool import BaseMessage, ToolMessage
from langchain_core.prompts import PromptTemplate
from langchain_core.tools import tool
from langgraph.graph import END, MessageGraph
from langgraph.prebuilt import ToolNode
from typing import TypedDict
from llm import get_text_llm
from log_util import logger
from time_it import time_it
from util import load_prompt
load_dotenv()
MAX_IMAGE_SEARCH_RESULTS = int(os.getenv('MAX_IMAGE_SEARCH_RESULTS', '3'))
class ImageSearchResult(TypedDict):
title: str
url: str
@time_it
def search_meal_image(meal: str) -> str:
prompt = load_prompt('validate_is_meal.prompt.txt')
llm = get_text_llm()
tools = [search_meal_images]
def is_meal_router(messages: list[BaseMessage]) -> str:
if messages[-1].content.lower() == 'yes':
return 'is_meal'
return END
graph = MessageGraph()
graph.add_node('validate_is_meal', llm)
graph.add_conditional_edges('validate_is_meal', is_meal_router)
graph.add_node('is_meal', llm.bind_tools(tools))
graph.add_edge('is_meal', 'call_tools')
graph.add_node('call_tools', ToolNode(tools))
graph.add_edge('call_tools', END)
graph.set_entry_point('validate_is_meal')
prompt_template = PromptTemplate.from_template(prompt)
prompt = prompt_template.format(phrase=meal)
workflow = graph.compile()
messages: list = workflow.invoke(prompt)
tool_messages = [message for message in messages if isinstance(message, ToolMessage)]
if tool_messages and tool_messages[0].content:
meal_images: list[ImageSearchResult] = json.loads(tool_messages[0].content)
if meal_images:
meal_image_url = meal_images[0]['url']
logger.info(f'{meal_image_url=}')
return meal_image_url
return None
@tool
def search_meal_images(meal: str) -> list[ImageSearchResult]:
'''Searches for images of the given meal.'''
return search_images(meal)
@time_it
def search_images(keywords: str, max_results: int | None=MAX_IMAGE_SEARCH_RESULTS) -> list[ImageSearchResult]:
results = DDGS().images(
keywords=keywords,
region='wt-wt',
safesearch='on',
size=None,
color='color',
type_image='photo',
layout=None,
license_image=None,
max_results=max_results,
)
logger.info(f'{keywords=}: {results=}')
results = [ImageSearchResult(title=result['title'], url=result['image']) for result in results]
return results1234567891011121314151617181920212223242526272829<system>
You are a highly experienced registered dietitian, providing evidence-based nutritional recommendations.
</system>
Human:
Given the provided recipe, alter it to be appropriate for your patient with the dietary restrictions and medical diagnoses below.
Return your answer of the altered recipe as a JSON object only (no comments and not an array) with these properties:
- name (string)
- ingredients (array of string)
- instructions (array of string, do NOT number the instructions)
- meal_type (string: "lunch", "dinner", or "breakfast")
- serves (string: "individual", "couple", or "family")
- notes (string: a description of what you changed)
For each ingredient specify the quantity or amount.
<recipe>
{recipe}
</recipe>
<dietary_restrictions>
{dietary_restrictions}
</dietary_restrictions>
<medical_diagnoses>
{medical_diagnoses}
</medical_diagnoses>1234567891011<system>You are an expert chef and food photographer.</system>
Human:
Given the recipe below, generate an appetizing, realistic image of the meal with no text.
<recipe>{recipe}</recipe>1234567891011121314151617181920<system>
You are a highly experienced registered dietitian, providing evidence-based nutritional recommendations.
</system>
Human:
Generate a recipe from the provided image of a prepared meal.
Return your answer as a JSON object only (no comments and not an array) with these properties:
- name (string)
- ingredients (array of string)
- instructions (array of string, do NOT number the instructions)
- meal_type (string: "lunch", "dinner", or "breakfast")
- serves (string: "individual", "couple", or "family")
For each ingredient specify the quantity or amount.
**Do not give any instruction beginning with "Serves as" because this is a recurring bug of yours.**
If the image is not a meal, return the string "Not a meal" (and do not return JSON in this case).123456789101112<system>
You are an expert on North American cuisine.
</system>
Human:
Respond "yes" or "no" whether the phrase below describes a common, well-known meal eaten at home or restaurants (including fast food) in North America.
No justification is needed for your answer, so do not respond with any other words except "yes" or "no".
<phrase>
{phrase}
</phrase>12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576import base64
from langchain_core.messages import HumanMessage
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate
from image_generator import generate_image
from llm import get_text_llm, get_vision_llm, invoke_llm_async
from time_it import time_it_async
from util import UTF8_ENCODING, load_prompt
async def get_recipe_from_image(image_path: str) -> dict:
recipe: dict = await _analyze_image_from_path(image_path, 'get_recipe_from_image.prompt.txt')
return recipe
async def get_altered_recipe(orig_recipe: dict, restrictions: list[str], diagnoses: list[str]) -> dict:
recipe: dict = await _generate_text('alter_recipe_for_healthier.prompt.txt',
{'recipe': orig_recipe, 'dietary_restrictions': restrictions, 'medical_diagnoses': diagnoses})
if 'recipe' in recipe:
recipe = recipe['recipe']
return recipe
async def _analyze_image_from_path(image_path: str, prompt_file: str) -> dict | str:
with open(image_path, 'rb') as image_file:
image_data = image_file.read()
image_format = _get_image_format(image_path)
return await _analyze_image_from_bytes(image_data, prompt_file, image_format)
@time_it_async
async def _analyze_image_from_bytes(image_data: bytes, prompt_file: str, image_format: str, output_parser_type=JsonOutputParser) -> dict | str:
img_base64 = base64.b64encode(image_data).decode(UTF8_ENCODING)
prompt = load_prompt(prompt_file)
prompt_template = ChatPromptTemplate.from_messages([
HumanMessage(
content=[
{'type': 'text', 'text': prompt},
{
'type': 'image_url',
'image_url': {
'url': f'data:image/{image_format};base64,{img_base64}',
'detail': 'high'
}
}
]
),
])
llm = get_vision_llm()
chain = prompt_template | llm | output_parser_type()
response = await invoke_llm_async(chain)
return response
def _get_image_format(image_path: str) -> str:
file_ext = image_path.split('.')[-1].lower()
match file_ext:
case 'jpg' | 'jpeg':
return 'jpeg'
case 'png' | 'gif' | 'webp':
return file_ext
case _:
raise ValueError(f'Unsupported image format for {image_path=}')
async def _generate_text(prompt_file: str, input: dict) -> dict | str:
prompt = load_prompt(prompt_file)
prompt_template = PromptTemplate.from_template(prompt)
llm = get_text_llm()
chain = prompt_template | llm | JsonOutputParser()
response = await invoke_llm_async(chain, input)
return response
def get_image_from_recipe(recipe: dict) -> str:
recipe_for_image_gen = {k: v for k, v in recipe.items() if k in {'name', 'ingredients', 'instructions', 'meal_type', 'serves'}}
image_url = generate_image('get_image_from_recipe.prompt.txt', {'recipe': recipe_for_image_gen})
return image_url1234567duckduckgo_search==7.5.2
gradio==5.21.0
jinja2==3.1.6
langchain_core==0.3.45
langchain_openai==0.3.8
langgraph==0.3.11
python-dotenv==1.0.1123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293<!DOCTYPE html><html><head> <style> body { font-family: 'Arial', sans-serif; line-height: 1.6; margin: 0; padding: 20px; }
h1 { color: #333; font-size: 2.5em; margin-bottom: 1em; text-align: center; }
h2 { color: #555; font-size: 1.8em; margin-top: 0.5em; margin-bottom: 1em; }
h3 { color: #666; font-size: 1.2em; margin-bottom: 0.5em; }
ul, ol { padding-left: 30px; }
li { margin-bottom: 0.5em; }
p { margin-bottom: 1em; }
strong { font-weight: bold; }
a { color: #007bff; /* Blue link color */ text-decoration: none; }
a:hover { text-decoration: underline; }
.recipe-container { border: 1px solid #ddd; padding: 20px; margin-bottom: 2em; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); border-radius: 5px; } </style></head><body> <div class="recipe-container"> <h2>{{ recipe.name }}</h2> <p><strong>Meal suggestion:</strong> {{ recipe.meal_type }}</p> <h3>Ingredients:</h3> <ul> {% for ingredient in recipe.ingredients %} <li>{{ ingredient }}</li> {% endfor %} </ul> <h3>Instructions:</h3> <ol> {% for instruction in recipe.instructions %} <li>{{ instruction }}</li> {% endfor %} </ol> {% if recipe.serves %} <p><strong>Serves:</strong> {{ recipe.serves }}</p> {% endif %} {% if recipe.notes %} <p><strong>Notes:</strong> {{ recipe.notes }}</p> {% endif %} {% if recipe.disclaimer %} <p><strong>Disclaimer:</strong> {{ recipe.disclaimer }}</p> {% endif %} </div></body></html>12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455import datetimeimport inspectimport osimport time
from functools import lru_cache, wraps
def time_it(func): ''' Decorator to measure and print the execution time of a synchronous function, including when an error is raised. ''' source_file, first_line_number = _get_function_location(func, line_offset=1) # get line number of function definition AFTER line with decorator
@wraps(func) def wrapper(*args, **kwargs): start_time = time.time() try: return func(*args, **kwargs) finally: _log_execution_time(start_time, func, source_file, first_line_number)
return wrapper
def time_it_async(func): ''' Decorator to measure and print the execution time of an asynchronous function, including when an error is raised. ''' source_file, first_line_number = _get_function_location(func, line_offset=1) # get line number of function definition AFTER line with decorator
@wraps(func) async def wrapper(*args, **kwargs): start_time = time.time() try: return await func(*args, **kwargs) finally: _log_execution_time(start_time, func, source_file, first_line_number)
return wrapper
@lru_cache(maxsize=None)def _get_function_location(func, line_offset: int) -> tuple[str, int]: try: source_file = os.path.basename(inspect.getsourcefile(func)) first_line_number = inspect.getsourcelines(func)[1] + line_offset return source_file, first_line_number except OSError: return None, None
def _log_execution_time(start_time: float, func, source_file: str | None, first_line_number: int | None) -> None: end_time = time.time() execution_time = end_time - start_time timestamp = datetime.datetime.now().strftime('%d-%b-%y %H:%M:%S') message = f'[{func.__name__}] took {execution_time:.4f} sec' source_line_ref = f'{source_file}:{first_line_number}' if source_file and first_line_number else '' print(f'{timestamp} [TIME] {source_line_ref} - {message}')1234567891011UTF8_ENCODING = 'utf-8'
def load_prompt(file_name: str) -> str: with open(f'prompts/{file_name}', 'r', encoding=UTF8_ENCODING) as f: return f.read()
def yield_lines_from_file(file_path: str): with open(f'data/{file_path}', 'r', encoding=UTF8_ENCODING) as f: for line in f: if line: # includes newline yield line.strip().dockerignoreAdded+172
.envAdded+23
.env_sampleAdded+25
.gitignoreAdded+169
DockerfileAdded+22
README.md+18-1
data/dietary_restrictions.txtAdded+14
data/medical_diagnoses.txtAdded+31
gradio_ui.pyAdded+123
image_generator.pyAdded+43
llm.pyAdded+38
log_util.pyAdded+23
meal_image_search.pyAdded+81
prompts/alter_recipe_for_healthier.prompt.txtAdded+29
prompts/get_image_from_recipe.prompt.txtAdded+11
prompts/get_recipe_from_image.prompt.txtAdded+20
prompts/validate_is_meal.prompt.txtAdded+12
recipe_generator.pyAdded+76
requirements.txtAdded+7
templates/recipe.htmlAdded+93
time_it.pyAdded+55
util.pyAdded+11