Skip to content

Mistral OCR API Error #144

@aceyLvy

Description

@aceyLvy
import base64
import requests
import os
from mistralai import Mistral

def encode_image(image_path):
    """Encode the image to base64."""
    try:
        with open(image_path, "rb") as image_file:
            return base64.b64encode(image_file.read()).decode('utf-8')
    except FileNotFoundError:
        print(f"Error: The file {image_path} was not found.")
        return None
    except Exception as e:  # Added general exception handling
        print(f"Error: {e}")
        return None

        
image_dir = r"D:\code\Omnidocbench-data\Mistral OCR\images\images"
md_list = os.listdir("md")
for image_path in os.listdir(image_dir):
    if image_path.split(".jpg")[0] + ".md" in md_list:
        print(f"The file {image_path} already exists.")
        continue
    # Getting the base64 string
    base64_image = encode_image(os.path.join(image_dir, image_path))

    api_key = "nNIv4************FwMY"
    client = Mistral(api_key=api_key)
    ocr_response = client.ocr.process(
            model="mistral-ocr-latest",
            document={
            "type": "image_url",
            "image_url": f"data:image/jpeg;base64,{base64_image}" 
        }
    )
    print(ocr_response)
   
    markdown_content = ocr_response.pages[0].markdown
    
    
    output_file = os.path.join("md",image_path.split(".jpg")[0] + ".md")
    with open(output_file, "w", encoding="utf-8") as fw:
        fw.write(markdown_content)

Hello, I used the above code to run, but it returned the following error. Please help me solve my confusion.
{"object":"error","message":"Service unavailable.","type":"internal_server_error","param":null,"code":"1000"}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions