Report

Simple Data Toolkit - Python - Loading Images Through ChatGPT

October 28, 2024 at 12:32 AM UTC
This post has no media.
Profile picture
Franklin Powers

Simple Data Toolkit provides an API for passing images to ChatGPT and extracting related text. (At the time of this writing, the release of this is pending for complete support, but it is coming soon)



To generate a LaTeX document based on a series of images (effectively doing OCR) we can do the following:


from sdtk import com_sdtk_api_ChatGPTAPI
import os
import fnmatch
import re

texData = ""
texBegin = "n\begin{document}n"
texEnd = "n\end{document}"
header = ""
footer = ""
foundBody = False

# Get a list of all .png files in the current directory
png_files = [file for file in os.listdir('.') if fnmatch.fnmatch(file, '*.png')]

def result(text):
global texData
global header
global footer
global foundBody

text = text.replace("```latex", "").replace("```", "")
body_match = re.search(r'\begin{document}(.*?)\end{document}', text, re.DOTALL)

if body_match:
body_content = body_match.group(1).strip()
if len(texData) > 0:
texData = texData + "n" + "\newpage" + "n"
else:
header_match = re.search(r'^(.*?)\begin{document}', text, re.DOTALL)
header = header_match.group(1).strip()
footer_match = re.search(r'\end{document}(.*)$', text, re.DOTALL)
footer = footer_match.group(1).strip()

texData = texData + body_content
foundBody = True
else:
foundBody = False
print(text)

# Loop over each .png file
for file_name in png_files:
foundBody = False
while foundBody == False:
com_sdtk_api_ChatGPTAPI.instance().query(result, "Can you generate a LaTeX document to represent the text in this image and format it correctly and return only the LaTeX code?", file_name)
with open("output.tex", "w+") as file:
# Write the string to the file
file.write(header + texBegin + texData + texEnd + footer)

Any thoughts on Franklin's post?

To comment or reply, you need an Ortingo account.

Sign in or sign up

Here's what Ortingoers think of Franklin's post.

There are no comments on this post.