Skip to content

A curated collection of practical AI projects implementing OCR systems, RAG, AI agents, and other AI use cases.

audit: fix image_to_structured_data - add env comment, fix prerequisites, add docstrings, add demo screenshot

Tiioluwani220f66401d96
diff --git a/OCR/image_to_structured_data/.env.example b/OCR/image_to_structured_data/.env.example
index e6c1b53..95109a5 100644
--- a/OCR/image_to_structured_data/.env.example
+++ b/OCR/image_to_structured_data/.env.example
@@ -1 +1,3 @@
+# Mistral API key used for visual extraction with Mistral Large 3.
+# Get one free at https://console.mistral.ai
MISTRAL_API_KEY=your_mistral_key_here
\ No newline at end of file
diff --git a/OCR/image_to_structured_data/README.md b/OCR/image_to_structured_data/README.md
index 8e3da2e..db1961c 100644
--- a/OCR/image_to_structured_data/README.md
+++ b/OCR/image_to_structured_data/README.md
@@ -2,6 +2,10 @@
A high-fidelity, visual extraction framework that converts images into validated, structured JSON using Mistral Large 3 and Pydantic.
+## Demo
+
+![Demo](assets/demo.png)
+
## Overview
This project demonstrates how to move beyond raw text OCR to **Structured Visual Extraction**. Instead of getting a messy block of text, this tool uses the `instructor` library to force the VLM (Vision Language Model) to output data that fits a strict Pydantic schema.
@@ -37,7 +41,7 @@ Before you begin, ensure you have:
- Python 3.10 or higher (Recommended)
- API keys for:
- - [ ] [Mistral AI Console](https://console.mistral.ai/)
+ - [Mistral AI Console](https://console.mistral.ai/)
- Basic understanding of Pydantic and Multimodal LLMs.
## Installation
diff --git a/OCR/image_to_structured_data/app.py b/OCR/image_to_structured_data/app.py
index 43dd8b3..fbf0a9c 100644
--- a/OCR/image_to_structured_data/app.py
+++ b/OCR/image_to_structured_data/app.py
@@ -1,3 +1,8 @@
+"""
+Image-to-Structured-Data Extractor Streamlit app: upload an image and extract
+validated, structured JSON from it using Mistral Large 3 and Instructor.
+"""
+
import streamlit as st
from processor import extract_structured_data
from schemas import ProductCollection, InvoiceCollection
@@ -11,11 +16,13 @@ st.set_page_config(page_title="Mistral Vision Extractor", layout="wide")
st.title("📸 Image-to-Structured-Data")
st.write("Using **Mistral Large 3** for high-fidelity visual OCR and structured extraction.")
+api_key = os.getenv("MISTRAL_API_KEY", "")
+
with st.sidebar:
- # Use the MISTRAL_API_KEY from .env if available
- api_key = st.text_input("Mistral API Key", value=os.getenv("MISTRAL_API_KEY", ""), type="password")
+ if not api_key:
+ st.warning("MISTRAL_API_KEY is not set in your .env file.")
schema_choice = st.selectbox("Select Extraction Schema", ["Product", "Invoice"])
-
+
# These names now match the updated import above
schema_map = {
"Product": ProductCollection,
diff --git a/OCR/image_to_structured_data/assets/payment-receipt.png b/OCR/image_to_structured_data/assets/demo.png
similarity index 100%
rename from OCR/image_to_structured_data/assets/payment-receipt.png
rename to OCR/image_to_structured_data/assets/demo.png
diff --git a/OCR/image_to_structured_data/processor.py b/OCR/image_to_structured_data/processor.py
index 3786aaa..5eaf0bf 100644
--- a/OCR/image_to_structured_data/processor.py
+++ b/OCR/image_to_structured_data/processor.py
@@ -1,6 +1,6 @@
import base64
import instructor
-from mistralai import Mistral
+from mistralai.client import Mistral
from PIL import Image
import io
@@ -17,6 +17,7 @@ def process_and_encode_image(image_file, max_size=(2048, 2048)):
return base64.b64encode(buffered.getvalue()).decode('utf-8')
def extract_structured_data(image_file, schema_model, api_key: str):
+ """Send the image to Mistral Large 3 and return data validated against the given schema."""
client = instructor.from_mistral(Mistral(api_key=api_key))
base64_image = process_and_encode_image(image_file)
diff --git a/OCR/image_to_structured_data/schemas.py b/OCR/image_to_structured_data/schemas.py
index 84f1f26..284298f 100644
--- a/OCR/image_to_structured_data/schemas.py
+++ b/OCR/image_to_structured_data/schemas.py
@@ -2,10 +2,12 @@ from pydantic import BaseModel, Field
from typing import List, Optional
class ProductAttribute(BaseModel):
+ """A single key-value attribute describing a product."""
key: str
value: str
class StructuredProduct(BaseModel):
+ """A single product extracted from an image, with pricing and attributes."""
name: str
brand: Optional[str]
price: Optional[float]
@@ -19,6 +21,7 @@ class ProductCollection(BaseModel):
products: List[StructuredProduct]
class InvoiceData(BaseModel):
+ """A single invoice extracted from an image, with vendor, total, and line items."""
vendor_name: str
date: str
total_amount: float