markitdown icon indicating copy to clipboard operation
markitdown copied to clipboard

NameError in using ChatGPT OCR

Open tanreinama opened this issue 1 year ago • 1 comments

When run the code in the sample to convert an image to Markdown, an error occurs.

from markitdown import MarkItDown
from openai import OpenAI

client = OpenAI()
md = MarkItDown(llm_client=client, llm_model="gpt-4o")
result = md.convert("example.jpg")
print(result.text_content)

I submitted a related PR;

https://github.com/microsoft/markitdown/pull/861

I would appreciate it if you could respond.

tanreinama avatar Feb 16 '25 04:02 tanreinama

from openai import OpenAI import base64

Initialize OpenAI client

client = OpenAI()

def analyze_image(image_path): # Encode image to base64 with open(image_path, "rb") as image_file: base64_image = base64.b64encode(image_file.read()).decode("utf-8")

# Send request to GPT-4 Vision
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "Analyze this image and describe it in Markdown format."},
                {
                    "type": "image_url",
                    "image_url": f"data:image/jpeg;base64,{base64_image}",
                },
            ],
        }
    ],
    max_tokens=1000,
)

return response.choices[0].message.content

Example usage

result = analyze_image("example.jpg") print(result)

shafixd avatar Feb 17 '25 07:02 shafixd