File size: 1,188 Bytes
2353e80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import base64
import mimetypes
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage

vision_llm = ChatOpenAI(model="gpt-4o")

def extract_text(image_path: str) -> str:
    # Read the image
    with open(image_path, "rb") as file:
        image_bytes = file.read()

    # Convert to Base64
    encoded = base64.b64encode(
        image_bytes
    ).decode("utf-8")

    # Determine image MIME type
    mime_type, _ = mimetypes.guess_type(image_path)

    if mime_type not in ("image/png", "image/jpeg", "image/webp"):
        raise ValueError("Unsupported image format")

    # Create a multimodal message
    message = HumanMessage(
        content=[
            {
                "type": "text",
                "text": "Extract all text from this image. Return only the extracted text."
            },
            {
                "type": "image_url",
                "image_url": {
                    "url": (
                        f"data:{mime_type};base64,{encoded}"
                    )
                }
            }
        ]
    )

    # Call the vision model
    response = vision_llm.invoke([message])

    return str(response.content)