Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NameError in using ChatGPT OCR #863

Open
tanreinama opened this issue Feb 16, 2025 · 1 comment
Open

NameError in using ChatGPT OCR #863

tanreinama opened this issue Feb 16, 2025 · 1 comment

Comments

@tanreinama
Copy link

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;

#861

I would appreciate it if you could respond.

@shafixd
Copy link

shafixd commented Feb 17, 2025

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants