Farid Karimli
commited on
Commit
·
0339679
1
Parent(s):
0958f93
Initial GPT4o mini PDF reader implementation
Browse files
code/modules/dataloader/pdf_readers/gpt.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
import os
|
3 |
+
import requests
|
4 |
+
|
5 |
+
from openai import OpenAI
|
6 |
+
from pdf2image import convert_from_path
|
7 |
+
from langchain.schema import Document
|
8 |
+
|
9 |
+
|
10 |
+
class GPTParser:
|
11 |
+
"""
|
12 |
+
This class uses OpenAI's GPT-4o mini model to parse PDFs and extract text, images and equations.
|
13 |
+
It is the most advanced parser in the system and is able to handle complex formats and layouts
|
14 |
+
"""
|
15 |
+
|
16 |
+
def __init__(self):
|
17 |
+
self.client = OpenAI()
|
18 |
+
self.api_key = os.getenv("OPENAI_API_KEY")
|
19 |
+
self.prompt = """
|
20 |
+
The provided documents are images of PDFs of lecture slides of deep learning material.
|
21 |
+
They contain LaTeX equations, images, and text.
|
22 |
+
The goal is to extract the text, images and equations from the slides and convert everything to markdown format. Some of the equations may be complicated.
|
23 |
+
The markdown should be clean and easy to read, and any math equation should be converted to LaTeX, between $$.
|
24 |
+
For images, give a description and if you can, a source. Separate each page with '---'.
|
25 |
+
Just respond with the markdown.
|
26 |
+
"""
|
27 |
+
|
28 |
+
def parse(self, pdf_path):
|
29 |
+
images = convert_from_path(pdf_path)
|
30 |
+
for i, image in enumerate(images):
|
31 |
+
image.save(f'output/images/page{i}.jpg', 'JPEG')
|
32 |
+
|
33 |
+
encoded_images = [self.encode_image(
|
34 |
+
f'output/images/page{im}.jpg') for im in range(len(images))]
|
35 |
+
|
36 |
+
chunks = [encoded_images[i:i + 5] for i in range(0, len(encoded_images), 5)]
|
37 |
+
|
38 |
+
headers = {
|
39 |
+
"Content-Type": "application/json",
|
40 |
+
"Authorization": f"Bearer {self.api_key}"
|
41 |
+
}
|
42 |
+
|
43 |
+
output = ""
|
44 |
+
for chunk_num, chunk in enumerate(chunks):
|
45 |
+
print(f"Processing chunk {chunk_num + 1}/{len(chunks)})")
|
46 |
+
|
47 |
+
content = [{"type": "image_url", "image_url": {
|
48 |
+
"url": f"data:image/jpeg;base64,{image}"}} for image in chunk]
|
49 |
+
|
50 |
+
content.insert(0, {"type": "text", "text": self.prompt})
|
51 |
+
|
52 |
+
payload = {
|
53 |
+
"model": "gpt-4o-mini",
|
54 |
+
"messages": [
|
55 |
+
{
|
56 |
+
"role": "user",
|
57 |
+
"content": content
|
58 |
+
}
|
59 |
+
],
|
60 |
+
}
|
61 |
+
|
62 |
+
response = requests.post(
|
63 |
+
"https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
64 |
+
|
65 |
+
resp = response.json()
|
66 |
+
print("Response", resp)
|
67 |
+
|
68 |
+
chunk_output = resp['choices'][0]['message']['content']
|
69 |
+
|
70 |
+
output += chunk_output + "\n---\n"
|
71 |
+
|
72 |
+
output = output.split("\n---\n")
|
73 |
+
|
74 |
+
documents = [
|
75 |
+
Document(
|
76 |
+
page_content=page,
|
77 |
+
metadata={"source": pdf_path, "page": i}
|
78 |
+
) for i, page in enumerate(output)
|
79 |
+
]
|
80 |
+
return documents
|
81 |
+
|
82 |
+
def encode_image(self, image_path):
|
83 |
+
with open(image_path, "rb") as image_file:
|
84 |
+
return base64.b64encode(image_file.read()).decode('utf-8')
|