Spaces:
Sleeping
Sleeping
import base64 | |
from openai import OpenAI | |
import os | |
# Function to encode the image | |
def encode_image(image_path): | |
assert os.path.exists(image_path), "The image file does not exist." | |
with open(image_path, "rb") as image_file: | |
return base64.b64encode(image_file.read()).decode('utf-8') | |
def transcribe_image(image_path): | |
"""Transcribe handwritten text from an image using OCR.""" | |
# Initialize the OpenAI client | |
client = OpenAI() | |
# Encoding the image | |
base64_image = encode_image(image_path) | |
# Preparing the API call | |
response = client.chat.completions.create( | |
model="gpt-4o-mini", | |
messages=[ | |
{ | |
"role": "user", | |
"content": [ | |
{"type": "text", "text": "Please transcribe the handwritten text in this image. Return only the text content."}, | |
{ | |
"type": "image_url", | |
"image_url": {"url": f"data:image/jpeg;base64,{base64_image}"} | |
} | |
] | |
} | |
], | |
max_tokens=300 | |
) | |
transcribed_text = response.choices[0].message.content | |
return transcribed_text |