1
This commit is contained in:
@@ -8,6 +8,7 @@ class Settings(BaseSettings):
|
||||
app_data_dir: Path = Path("./data")
|
||||
database_path: Path = Path("./data/app.db")
|
||||
mistral_api_key: str = ""
|
||||
mistral_ocr_model: str = "mistral-ocr-latest"
|
||||
deepgram_api_key: str = ""
|
||||
|
||||
model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8", extra="ignore")
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user