This commit is contained in:
urieljarethbusiness-cpu
2026-05-17 11:18:12 -06:00
parent 64b3d15b90
commit b964fcb2e6
8 changed files with 40 additions and 40 deletions
@@ -9,8 +9,7 @@ from backend.app.config import settings
class MistralClient:
endpoint = "https://api.mistral.ai/v1/chat/completions"
model = "pixtral-12b-2409"
endpoint = "https://api.mistral.ai/v1/ocr"
def ocr_image(self, image_path: Path) -> str:
if not settings.mistral_api_key:
@@ -23,20 +22,9 @@ class MistralClient:
"Authorization": f"Bearer {settings.mistral_api_key}",
},
json={
"model": self.model,
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Extract all text from this image and format it as Markdown. If there are tables, represent them using Markdown table syntax. Do not include commentary.",
},
{"type": "image_url", "image_url": data_url},
],
}
],
"max_tokens": 3000,
"model": settings.mistral_ocr_model,
"document": {"type": "image_url", "image_url": data_url},
"include_image_base64": False,
},
timeout=180,
)
@@ -45,7 +33,13 @@ class MistralClient:
if not response.ok:
raise RuntimeError(f"Mistral error {response.status_code}: {response.text}")
data = response.json()
return data["choices"][0]["message"]["content"]
pages = data.get("pages") or []
markdown_pages = [page.get("markdown", "").strip() for page in pages if page.get("markdown")]
if markdown_pages:
return "\n\n".join(markdown_pages)
if data.get("markdown"):
return data["markdown"].strip()
raise RuntimeError("Mistral OCR no devolvio Markdown")
def _image_data_url(self, image_path: Path) -> str:
encoded = base64.b64encode(image_path.read_bytes()).decode("ascii")