File size: 3,557 Bytes
ec6dd69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# MIT License
#
# Copyright (c) 2024 dataforgood
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# Standard imports
import logging

# External imports
import os
import uuid
from io import StringIO
from pathlib import Path

import pandas as pd
from unstructured_client import UnstructuredClient
from unstructured_client.models import shared


class UnstructuredAPI:
    def __init__(self, **kwargs: dict) -> dict:
        """
        Builds a pdf page parser, looking for tables using
        the unstructured.io api.
        The kwargs given to the constructor are directly propagated
        to the partition_pdf function.
        You are free to define any parameter partition_pdf recognizes
        """
        self.kwargs = kwargs
        self.type = "unstructured_api"

    def __call__(self, pdf_filepath: str) -> dict:
        logging.info("\nKicking off extraction stage...")
        logging.info(f"Extraction type: {self.type}, with params: {self.kwargs}")

        s = UnstructuredClient(api_key_auth=os.getenv("UNSTRUCTURED_API_KEY"))

        with Path(pdf_filepath).open("rb") as f:
            # Note that this currently only supports a single file
            files = shared.Files(
                content=f.read(),
                file_name=pdf_filepath,
            )

        req = shared.PartitionParameters(
            files=files,
            strategy="hi_res",
            pdf_infer_table_structure="True",
            **self.kwargs,
        )

        try:
            resp = s.general.partition(req)
        except Exception as e:
            print(e)
        else:
            tables_list = []
            for el in resp.elements:
                if el["type"] == "Table":
                    # Enclose in try block to ignore case when pandas can't read the table
                    # Happens when the html is incorrectly formatted
                    try:
                        table = pd.read_html(StringIO(el["metadata"]["text_as_html"]))[
                            0
                        ]
                    except Exception:
                        logging.info(
                            "Html table discarded. Pandas couldn't read the table.",
                        )
                    else:
                        tables_list.append(table)

            # Create asset
            new_asset = {
                "id": uuid.uuid4(),
                "type": "unstructured_api",
                "params": self.kwargs,
                "tables": tables_list,
            }

            return new_asset