With the recent announcement of Mistral OCR, an old idea of using code to extract my son’s school menu has been revived.
The menu looks like this:
The goal is to extract the starter, main course, dessert, and snack for each day.
Manually extracting this information can be challenging because the document is designed to be visually appealing for humans, not machines. However, with Mistral’s new API, this task can now be accomplished with just a few lines of code.
Initializing the Model
from mistralai import Mistral
import os
api_key = os.environ["MISTRAL_API_KEY"]
client = Mistral(api_key=api_key)
model = "mistral-small-latest"
I chose the mistral-small-latest
model because it has vision capabilities.
Defining the Messages
Next, we need to prompt the model with what we want and provide the image as input.
messages = [
{
"role": "system",
"content": "Extract the list of menus for each day of the week"
},
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": "https://www.kremlinbicetre.fr/app/uploads/2025/02/Capture-decran-2025-02-12-a-13.54.09.png"
}
]
}
]
Structuring the Output
To structure the output, we’ll use Pydantic, a Python library for defining data models.
from typing import List
from pydantic import BaseModel
class Menu(BaseModel):
day: int
month: int
starter: str
main: str
dessert: str
snack: str
class Menus(BaseModel):
menus: List[Menu]
Calling the Model
Now, we can call the model and ask it to parse the output as a list of menus.
chat_response = client.chat.parse(
model=model,
messages=messages,
response_format=Menus,
temperature=0
)
The Result
Finally, we can iterate over the result and print the menu for each day.
for menu in chat_response.choices[0].message.parsed.menus:
print(f"{menu.day:02d}/{menu.month:02d}")
print(f"* Starter: {menu.starter}\n* Main: {menu.main}\n* Dessert: {menu.dessert}\n* Snack: {menu.snack}")
I did an export to an ICS and imported into Google Calendar:
It’s impressive that I was able to achieve this with just a few lines of code while few years ago I would have been struggling to do the same task.