diff --git a/ressources/imgs/recette_test.png b/ressources/imgs/recette_test.png new file mode 100644 index 0000000..2803b68 Binary files /dev/null and b/ressources/imgs/recette_test.png differ diff --git a/src/model.py b/src/model.py index b510c7c..e049a4c 100644 --- a/src/model.py +++ b/src/model.py @@ -10,16 +10,16 @@ from typing import Any 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 - :param recipe: + :param recipe: recipe in text format :param kwargs: - :return: + :return: dict containing ingredients and preparation method """ task = f"""Your task is to analyze a recipe. For this analysis you have to extract ingredients and their quantitites. @@ -45,4 +45,73 @@ RECIPE: model=kwargs["model"], messages=[create_message("user", task)], response_format={ "type": "json_object" } - ).choices[0].message.content) \ No newline at end of file + ).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) \ No newline at end of file diff --git a/src/utils.py b/src/utils.py index aad5910..9f94f9c 100644 --- a/src/utils.py +++ b/src/utils.py @@ -3,6 +3,8 @@ :mail: pro@quentin-leblanc.ch :brief: Utility functions """ +import base64 + def create_message(role: str, content: str) -> dict[str, str]: """ @@ -11,4 +13,17 @@ def create_message(role: str, content: str) -> dict[str, str]: :param content: :return: """ - return {"role": role, "content": content} \ No newline at end of file + 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 \ No newline at end of file