formpilot-demo / tests /quick_ocr_mistral_api_test.py
afulara's picture
Auto‑deploy from GitHub
0c0a4f7 verified
#!/usr/bin/env python3
import os, json
from mistralai import Mistral
from dotenv import load_dotenv
# ── Load your .env so the API key is available ──
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
load_dotenv(os.path.join(PROJECT_ROOT, ".env"))
# ── Init the client ──
api_key = os.getenv("MISTRAL_OCR_KEY")
if not api_key:
raise RuntimeError("MISTRAL_OCR_KEY not set in .env")
client = Mistral(api_key=api_key)
# ── Call the public example PNG → receipt.png ──
example_url = (
"https://raw.githubusercontent.com/"
"mistralai/cookbook/refs/heads/main/mistral/ocr/receipt.png"
)
resp = client.ocr.process(
model="mistral-ocr-latest",
document={"type": "image_url", "image_url": example_url},
)
print("➤ Raw response JSON:")
print(json.dumps(resp, indent=2))