added possibility to read img recipe
This commit is contained in:
parent
efa4cfe001
commit
65d7e65a40
BIN
ressources/imgs/recette_test.png
Normal file
BIN
ressources/imgs/recette_test.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 82 KiB |
79
src/model.py
79
src/model.py
@ -10,16 +10,16 @@ from typing import Any
|
|||||||
|
|
||||||
from mistralai import Mistral
|
from mistralai import Mistral
|
||||||
|
|
||||||
from .utils import create_message
|
from .utils import create_message, encode_image
|
||||||
|
|
||||||
|
|
||||||
def recipe_analysis(recipe: str, **kwargs) -> dict[str, Any]:
|
def text_recipe_analysis(recipe: str, **kwargs) -> dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
Analyze a recipe returning ingredients and preparation steps
|
Analyze a recipe in text returning ingredients and preparation steps
|
||||||
Call a MistralAI model with parameters in kwargs
|
Call a MistralAI model with parameters in kwargs
|
||||||
:param recipe:
|
:param recipe: recipe in text format
|
||||||
:param kwargs:
|
:param kwargs:
|
||||||
:return:
|
:return: dict containing ingredients and preparation method
|
||||||
"""
|
"""
|
||||||
task = f"""Your task is to analyze a recipe.
|
task = f"""Your task is to analyze a recipe.
|
||||||
For this analysis you have to extract ingredients and their quantitites.
|
For this analysis you have to extract ingredients and their quantitites.
|
||||||
@ -46,3 +46,72 @@ RECIPE:
|
|||||||
messages=[create_message("user", task)],
|
messages=[create_message("user", task)],
|
||||||
response_format={ "type": "json_object" }
|
response_format={ "type": "json_object" }
|
||||||
).choices[0].message.content)
|
).choices[0].message.content)
|
||||||
|
|
||||||
|
def img_recipe_analysis(recipe: str, **kwargs) -> dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Analyze a recipe in image returning ingredients and preparation steps
|
||||||
|
Call a MistralAI model with parameters in kwargs
|
||||||
|
:param recipe: path to image recipe file
|
||||||
|
:param kwargs:
|
||||||
|
:return: dict containing ingredients and preparation method
|
||||||
|
"""
|
||||||
|
|
||||||
|
img = encode_image(recipe)
|
||||||
|
|
||||||
|
client = Mistral(api_key=kwargs["api_key"])
|
||||||
|
ocr_answer = client.ocr.process(
|
||||||
|
model="mistral-ocr-2505",
|
||||||
|
document={
|
||||||
|
"type": "image_url",
|
||||||
|
"image_url":f"data:image/jpeg;base64,{img}"
|
||||||
|
},
|
||||||
|
include_image_base64=True
|
||||||
|
).pages
|
||||||
|
|
||||||
|
text = "".join([page.markdown for page in ocr_answer])
|
||||||
|
|
||||||
|
with open("test.md", 'w') as f:
|
||||||
|
f.write(text)
|
||||||
|
|
||||||
|
return text_recipe_analysis(text, **kwargs)
|
||||||
|
|
||||||
|
# def replace_ingredients(ingredients: list[str], **kwargs) -> dict[str, Any]:
|
||||||
|
# """
|
||||||
|
# Ask a model to find replacements for the ingredients indicated
|
||||||
|
# :param ingredients: List of ingredients to replace
|
||||||
|
# :param kwargs:
|
||||||
|
# :return: a dict with a list of replacements and indications about these replacements
|
||||||
|
# """
|
||||||
|
# task = """Your task is to find replacements for the ingredients that will be provided.
|
||||||
|
# You have to search the web to do so.
|
||||||
|
# For each ingredient, find 3 replacements.
|
||||||
|
# For each replacement give the name of the ingredient found and the quantity used to replace 1g of the ingredient.
|
||||||
|
# You also have to list the pros and the cons of using this replacement.
|
||||||
|
# You will return a list with each ingredient and its replacement.
|
||||||
|
#
|
||||||
|
# Your answer must be formatted so that it can be stored in a json format.
|
||||||
|
# The json must be containing the fields:
|
||||||
|
# - 'ingredient': that is the ingredient to be replaced.
|
||||||
|
#
|
||||||
|
# under 'ingredient', you will have the following field:
|
||||||
|
# - 'replacements': that is the list of 'replacement' ingredients.
|
||||||
|
#
|
||||||
|
# Each 'replacement' will have the fields:
|
||||||
|
# - 'name': that is the name of the particular replacement;
|
||||||
|
# - 'quantity': that is the quantity of the particular replacement;
|
||||||
|
# - 'pros': A list of different benefits for this particular replacement;
|
||||||
|
# - 'cons': A list of different drawbacks for this particular replacement;
|
||||||
|
#
|
||||||
|
# Here are the ingredients you hae to replace:
|
||||||
|
# """
|
||||||
|
#
|
||||||
|
# for ingredient in ingredients:
|
||||||
|
# task += f"- {ingredient};\n"
|
||||||
|
#
|
||||||
|
# client = Mistral(api_key=kwargs["api_key"])
|
||||||
|
# return json.loads(client.chat.complete(
|
||||||
|
# model=kwargs["model"],
|
||||||
|
# messages=[create_message("user", task)],
|
||||||
|
# tools=[{"type": "web_search"}],
|
||||||
|
# response_format={"type": "json_object"}
|
||||||
|
# ).choices[0].message.content)
|
||||||
15
src/utils.py
15
src/utils.py
@ -3,6 +3,8 @@
|
|||||||
:mail: pro@quentin-leblanc.ch
|
:mail: pro@quentin-leblanc.ch
|
||||||
:brief: Utility functions
|
:brief: Utility functions
|
||||||
"""
|
"""
|
||||||
|
import base64
|
||||||
|
|
||||||
|
|
||||||
def create_message(role: str, content: str) -> dict[str, str]:
|
def create_message(role: str, content: str) -> dict[str, str]:
|
||||||
"""
|
"""
|
||||||
@ -12,3 +14,16 @@ def create_message(role: str, content: str) -> dict[str, str]:
|
|||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
return {"role": role, "content": content}
|
return {"role": role, "content": content}
|
||||||
|
|
||||||
|
# from https://docs.mistral.ai/capabilities/vision/
|
||||||
|
def encode_image(image_path):
|
||||||
|
"""Encode the image to base64."""
|
||||||
|
try:
|
||||||
|
with open(image_path, "rb") as image_file:
|
||||||
|
return base64.b64encode(image_file.read()).decode('utf-8')
|
||||||
|
except FileNotFoundError:
|
||||||
|
print(f"Error: The file {image_path} was not found.")
|
||||||
|
return None
|
||||||
|
except Exception as e: # Added general exception handling
|
||||||
|
print(f"Error: {e}")
|
||||||
|
return None
|
||||||
Loading…
x
Reference in New Issue
Block a user