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.exampleindex 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.aiMISTRAL_API_KEY=your_mistral_key_here\ No newline at end of filediff --git a/OCR/image_to_structured_data/README.md b/OCR/image_to_structured_data/README.mdindex 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+++## OverviewThis 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.## Installationdiff --git a/OCR/image_to_structured_data/app.py b/OCR/image_to_structured_data/app.pyindex 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 stfrom processor import extract_structured_datafrom 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 aboveschema_map = {"Product": ProductCollection,diff --git a/OCR/image_to_structured_data/assets/payment-receipt.png b/OCR/image_to_structured_data/assets/demo.pngsimilarity index 100%rename from OCR/image_to_structured_data/assets/payment-receipt.pngrename to OCR/image_to_structured_data/assets/demo.pngdiff --git a/OCR/image_to_structured_data/processor.py b/OCR/image_to_structured_data/processor.pyindex 3786aaa..5eaf0bf 100644--- a/OCR/image_to_structured_data/processor.py+++ b/OCR/image_to_structured_data/processor.py@@ -1,6 +1,6 @@import base64import instructor-from mistralai import Mistral+from mistralai.client import Mistralfrom PIL import Imageimport 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.pyindex 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, Fieldfrom typing import List, Optionalclass ProductAttribute(BaseModel):+ """A single key-value attribute describing a product."""key: strvalue: strclass StructuredProduct(BaseModel):+ """A single product extracted from an image, with pricing and attributes."""name: strbrand: 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: strdate: strtotal_amount: float