File size: 8,510 Bytes
f745baf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3966ab6
 
 
 
 
 
 
 
 
 
 
 
 
f745baf
3966ab6
 
 
 
 
 
 
 
 
 
 
 
 
 
f745baf
3966ab6
 
 
 
f745baf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3966ab6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f745baf
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import base64
import json
import os
import requests

import anthropic
import openai
from dotenv import load_dotenv
from pathlib import Path
from llama_parse import LlamaParse
from llama_index.core import SimpleDirectoryReader
from unstructured.partition.auto import partition
from preprocessors.preprocessor import PdfPreprocessor
from postprocessors.postprocessor import ClaudePostprocessor, GPTPostprocessor

load_dotenv()


class Model:
    BASE_URL: str | None = None
    API_KEY: str | None = None
    MODEL: str | None = None

    def __init_subclass__(cls) -> None:
        """Initialize subclass."""
        super().__init_subclass__()

    def __init__(self):
        """Init self"""

    def extract(self, file_path: str) -> str:
        """Extract model.

        Args:
            file_path: path to file to extract

        Returns:
            str: output markdown
        """
        raise NotImplementedError("Model extract method is not implemented")

class AnyParserModel(Model):
    BASE_URL = "https://k7u1c342dc.execute-api.us-west-2.amazonaws.com/v1/extract"
    API_KEY = os.getenv('ANYPARSER_RT_API_KEY')

    def extract(self, file_path: str) -> str:
        """Extract data in real-time.

        Args:
            file_path (str): The path to the file to be parsed.

        Returns:
            str: The extracted data.
        """
        file_extension = Path(file_path).suffix.lower().lstrip(".")

        # Check if the file exists
        if not Path(file_path).is_file():
            return "Error: File does not exist", "File does not exist"

        if file_extension in ["pdf", "docx"]:
            # Encode the PDF file content in base64
            with open(file_path, "rb") as file:
                encoded_file = base64.b64encode(file.read()).decode("utf-8")
        else:
            return "Error: Unsupported file type", "Unsupported file type"

        # Create the JSON payload
        payload = {
            "file_content": encoded_file,
            "file_type": file_extension,
        }


        # Set the headers
        headers = {
            "Content-Type": "application/json",
            "x-api-key": self.API_KEY,
        }

        # Send the POST request
        response = requests.post(
            self.BASE_URL, headers=headers, data=json.dumps(payload), timeout=30
        )

        # Check if the request was successful
        if response.status_code == 200:
            try:
                response_data = response.json()
                response_list = []
                for text in response_data["markdown"]:
                    response_list.append(text)
                markdown_text = "\n".join(response_list)
                return markdown_text
            except json.JSONDecodeError:
                return "Error: Invalid JSON response", f"Response: {response.text}"
        else:
            return f"Error: {response.status_code}", f"Response: {response.text}"

class LlamaParseModel(Model):
    BASE_URL = None
    API_KEY = os.getenv('LLAMA_CLOUD_API_KEY')

    def __init__(self):
        """Init."""
        super().__init__()
        if not self.API_KEY:
            raise ValueError("The API key is required. Please set the LLAMA_CLOUD_API_KEY environment variable.")

    def extract(self, file_path: str) -> str:
        """Extract data in real-time.

        Args:
            file_path (str): The path to the file to be parsed.

        Returns:
            str: The extracted data.
        """
        try:
            parser = LlamaParse(
                result_type="markdown",
                num_workers=4,
                verbose=True,
                language="en",
            )

            file_extractor = {".pdf": parser}
            documents = SimpleDirectoryReader(input_files=[file_path], file_extractor=file_extractor).load_data()

            markdown = "\n\n".join([doc.text for doc in documents])

            return markdown
        except Exception as e:
            print(f"Error processing input: {str(e)}")
            return f"Error processing with LlamaParse: {str(e)}"

class UnstructuredModel(Model):
    BASE_URL = None
    API_KEY = None

    def __init__(self):
        """Init."""
        super().__init__()

    def extract(self, file_path: str) -> str:
        """Extract data in real-time.

        Args:
            file_path (str): The path to the file to be parsed.

        Returns:
            str: The extracted data.
        """
        try:

            elements = partition(file_path)

            parsed_text = "\n".join(str(element) for element in elements)

            markdown = parsed_text if parsed_text else "No content parsed"
            return markdown
        except Exception as e:
            return f"Error processing UnstructuredModel: {str(e)}"

class GPTModel(Model):
    BASE_URL = None
    API_KEY = os.getenv("OPENAI_API_KEY")
    MODEL = "gpt-4o-mini"
    REQUIRES_OPENAI = True

    def __init__(self):
        """Init."""
        super().__init__()
        if not self.API_KEY:
            raise ValueError(
                "The API key is required. Please set the OPENAI_API_KEY environment variable."
            )
        self._client = openai.OpenAI(api_key=self.API_KEY)


    def extract(self, file_path: str) -> str:
        """Extract data in real-time.

        Args:
            file_path (str): The path to the file to be parsed.

        Returns:
            str: The extracted data.
        """

        try:
            pdf_preprocessor = PdfPreprocessor()
            gpt_postprocessor = GPTPostprocessor()
            file_contents = pdf_preprocessor.run(file_path)
            contents = []
            for content in file_contents:
                contents.append(
                {
                "type": "image_url",
                "image_url": {
                    "url": f"data:image/jpeg;base64,{content}",
                },
                })

            messages = [
                {
                "role": "user",
                "content": [
                    {"type": "text", "text": "Convert this image to markdown"},
                    *contents,
                ],
                }
            ]

            response = self._client.chat.completions.create(
                model=self.MODEL,
                messages=messages,
            )

            return gpt_postprocessor.run(response.choices[0].message.content)
        except Exception as e:
            print(f"Error processing input: {str(e)}")
            return f"Error processing with GPTModel: {str(e)}"

class ClaudeModel(Model):
    BASE_URL = "http://103.114.163.134:3000/v1/"
    API_KEY = os.getenv("ANTHROPIC_API_KEY")
    MODEL = "claude-3-5-sonnet-20240620"
    REQUIRES_OPENAI = True

    def __init__(self):
        """Init."""
        super().__init__()
        if not self.API_KEY:
            raise ValueError(
                "The API key is required. Please set the ANTHROPIC_API_KEY environment variable."
            )
        self._client = anthropic.Anthropic(
            api_key=self.API_KEY,
        )


    def extract(self, file_path: str) -> str:
        """Extract data in real-time.

        Args:
            file_path (str): The path to the file to be parsed.

        Returns:
            str: The extracted data.
        """

        try:
            prompt = "Convert this image to markdown."
            pdf_preprocessor = PdfPreprocessor()
            claude_postprocessor = ClaudePostprocessor()
            file_contents = pdf_preprocessor.run(file_path)

            contents = []
            for content in file_contents:
                contents.append(
                    {
                        "type": "image",
                        "source": {
                            "type": "base64",
                            "media_type": "image/jpeg",
                            "data": content,
                        }
                    })

            messages = [
                {"role": "user", "content": [
                    {"type": "text", "text": prompt},
                    *contents,
                ]}
            ]

            response = self._client.messages.create(
                model="claude-3-5-sonnet-20240620", max_tokens=1024, messages=messages
            )
            print(response.content[0].text)
            return claude_postprocessor.run(response.content[0].text)
        except Exception as e:
            return f"Error processing ClaudeModel: {str(e)}"