trustgraph-ocr 2.11.7__tar.gz → 3.0.0rc2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: trustgraph-ocr
3
- Version: 2.11.7
3
+ Version: 3.0.0rc2
4
4
  Summary: TrustGraph provides a means to run a pipeline of flexible AI processing components in a flexible means to achieve a processing pipeline.
5
5
  Author-email: "trustgraph.ai" <security@trustgraph.ai>
6
6
  Project-URL: Homepage, https://github.com/trustgraph-ai/trustgraph
@@ -8,7 +8,7 @@ Classifier: Programming Language :: Python :: 3
8
8
  Classifier: Operating System :: OS Independent
9
9
  Requires-Python: >=3.10
10
10
  Description-Content-Type: text/markdown
11
- Requires-Dist: trustgraph-base<2.12,>=2.11
11
+ Requires-Dist: trustgraph-base<3.1,>=3.0.0a0
12
12
  Requires-Dist: pulsar-client
13
13
  Requires-Dist: prometheus-client
14
14
  Requires-Dist: boto3
@@ -10,7 +10,7 @@ description = "TrustGraph provides a means to run a pipeline of flexible AI proc
10
10
  readme = "README.md"
11
11
  requires-python = ">=3.10"
12
12
  dependencies = [
13
- "trustgraph-base>=2.11,<2.12",
13
+ "trustgraph-base>=3.0.0a0,<3.1",
14
14
  "pulsar-client",
15
15
  "prometheus-client",
16
16
  "boto3",
@@ -0,0 +1,49 @@
1
+
2
+ """
3
+ Simple decoder, accepts PDF documents on input, outputs pages from the
4
+ PDF document as text as separate output objects.
5
+
6
+ Supports both inline document data and fetching from librarian via Pulsar
7
+ for large documents.
8
+ """
9
+
10
+ import logging
11
+ import pytesseract
12
+ from pdf2image import convert_from_bytes
13
+
14
+ from ... base import DocumentDecoderBase
15
+
16
+ logger = logging.getLogger(__name__)
17
+
18
+ default_ident = "document-decoder"
19
+
20
+
21
+ class Processor(DocumentDecoderBase):
22
+
23
+ COMPONENT_NAME = "tesseract-ocr-decoder"
24
+ COMPONENT_VERSION = "1.0.0"
25
+
26
+ def __init__(self, **params):
27
+ super(Processor, self).__init__(**params)
28
+ logger.info("PDF OCR processor initialized")
29
+
30
+ def decode_pages(self, blob, mime_type=None):
31
+
32
+ pages = convert_from_bytes(blob)
33
+
34
+ for ix, page in enumerate(pages):
35
+ page_num = ix + 1
36
+
37
+ try:
38
+ text = pytesseract.image_to_string(page, lang='eng')
39
+ except Exception as e:
40
+ logger.warning(f"Page {page_num} did not OCR: {e}")
41
+ continue
42
+
43
+ if text and text.strip():
44
+ yield (text, page_num, "text")
45
+
46
+
47
+ def run():
48
+
49
+ Processor.launch(default_ident, __doc__)
@@ -0,0 +1 @@
1
+ __version__ = "3.0.0rc2"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: trustgraph-ocr
3
- Version: 2.11.7
3
+ Version: 3.0.0rc2
4
4
  Summary: TrustGraph provides a means to run a pipeline of flexible AI processing components in a flexible means to achieve a processing pipeline.
5
5
  Author-email: "trustgraph.ai" <security@trustgraph.ai>
6
6
  Project-URL: Homepage, https://github.com/trustgraph-ai/trustgraph
@@ -8,7 +8,7 @@ Classifier: Programming Language :: Python :: 3
8
8
  Classifier: Operating System :: OS Independent
9
9
  Requires-Python: >=3.10
10
10
  Description-Content-Type: text/markdown
11
- Requires-Dist: trustgraph-base<2.12,>=2.11
11
+ Requires-Dist: trustgraph-base<3.1,>=3.0.0a0
12
12
  Requires-Dist: pulsar-client
13
13
  Requires-Dist: prometheus-client
14
14
  Requires-Dist: boto3
@@ -1,4 +1,4 @@
1
- trustgraph-base<2.12,>=2.11
1
+ trustgraph-base<3.1,>=3.0.0a0
2
2
  pulsar-client
3
3
  prometheus-client
4
4
  boto3
@@ -1,189 +0,0 @@
1
-
2
- """
3
- Simple decoder, accepts PDF documents on input, outputs pages from the
4
- PDF document as text as separate output objects.
5
-
6
- Supports both inline document data and fetching from librarian via Pulsar
7
- for large documents.
8
- """
9
-
10
- import base64
11
- import logging
12
- import pytesseract
13
- from pdf2image import convert_from_bytes
14
-
15
- from ... schema import Document, TextDocument, Metadata
16
- from ... schema import Triples
17
- from ... base import FlowProcessor, ConsumerSpec, ProducerSpec, LibrarianSpec
18
-
19
- from ... provenance import (
20
- document_uri, page_uri as make_page_uri, derived_entity_triples,
21
- set_graph, GRAPH_SOURCE,
22
- )
23
-
24
- # Component identification for provenance
25
- COMPONENT_NAME = "tesseract-ocr-decoder"
26
- COMPONENT_VERSION = "1.0.0"
27
-
28
- # Module logger
29
- logger = logging.getLogger(__name__)
30
-
31
- default_ident = "document-decoder"
32
-
33
- class Processor(FlowProcessor):
34
-
35
- def __init__(self, **params):
36
-
37
- id = params.get("id", default_ident)
38
-
39
- super(Processor, self).__init__(
40
- **params | {
41
- "id": id,
42
- }
43
- )
44
-
45
- self.register_specification(
46
- ConsumerSpec(
47
- name = "input",
48
- schema = Document,
49
- handler = self.on_message,
50
- )
51
- )
52
-
53
- self.register_specification(
54
- ProducerSpec(
55
- name = "output",
56
- schema = TextDocument,
57
- )
58
- )
59
-
60
- self.register_specification(
61
- ProducerSpec(
62
- name = "triples",
63
- schema = Triples,
64
- )
65
- )
66
-
67
- self.register_specification(
68
- LibrarianSpec()
69
- )
70
-
71
- logger.info("PDF OCR processor initialized")
72
-
73
- async def on_message(self, msg, consumer, flow):
74
-
75
- logger.info("PDF message received")
76
-
77
- v = msg.value()
78
-
79
- logger.info(f"Decoding {v.metadata.id}...")
80
-
81
- # Check MIME type if fetching from librarian
82
- if v.document_id:
83
- doc_meta = await flow.librarian.fetch_document_metadata(
84
- document_id=v.document_id,
85
- )
86
- if doc_meta and doc_meta.kind and doc_meta.kind != "application/pdf":
87
- logger.error(
88
- f"Unsupported MIME type: {doc_meta.kind}. "
89
- f"Tesseract OCR decoder only handles application/pdf. "
90
- f"Ignoring document {v.metadata.id}."
91
- )
92
- return
93
-
94
- # Get PDF content - fetch from librarian or use inline data
95
- if v.document_id:
96
- logger.info(f"Fetching document {v.document_id} from librarian...")
97
- content = await flow.librarian.fetch_document_content(
98
- document_id=v.document_id,
99
- )
100
- if isinstance(content, str):
101
- content = content.encode('utf-8')
102
- blob = base64.b64decode(content)
103
- logger.info(f"Fetched {len(blob)} bytes from librarian")
104
- else:
105
- blob = base64.b64decode(v.data)
106
-
107
- # Get the source document ID
108
- source_doc_id = v.document_id or v.metadata.id
109
-
110
- try:
111
- pages = convert_from_bytes(blob)
112
- except Exception as e:
113
- logger.error(
114
- f"Failed to decode PDF {source_doc_id}: "
115
- f"{type(e).__name__}: {e}"
116
- )
117
- return
118
-
119
- for ix, page in enumerate(pages):
120
-
121
- page_num = ix + 1 # 1-indexed
122
-
123
- try:
124
- text = pytesseract.image_to_string(page, lang='eng')
125
- except Exception as e:
126
- logger.warning(f"Page {page_num} did not OCR: {e}")
127
- continue
128
-
129
- logger.debug(f"Processing page {page_num}")
130
-
131
- # Generate unique page ID
132
- pg_uri = make_page_uri()
133
- page_doc_id = pg_uri
134
- page_content = text.encode("utf-8")
135
-
136
- # Save page as child document in librarian
137
- await flow.librarian.save_child_document(
138
- doc_id=page_doc_id,
139
- parent_id=source_doc_id,
140
- content=page_content,
141
- document_type="page",
142
- title=f"Page {page_num}",
143
- )
144
-
145
- # Emit provenance triples
146
- doc_uri = document_uri(source_doc_id)
147
-
148
- prov_triples = derived_entity_triples(
149
- entity_uri=pg_uri,
150
- parent_uri=doc_uri,
151
- component_name=COMPONENT_NAME,
152
- component_version=COMPONENT_VERSION,
153
- label=f"Page {page_num}",
154
- page_number=page_num,
155
- )
156
-
157
- await flow("triples").send(Triples(
158
- metadata=Metadata(
159
- id=pg_uri,
160
- root=v.metadata.root,
161
- collection=v.metadata.collection,
162
- ),
163
- triples=set_graph(prov_triples, GRAPH_SOURCE),
164
- ))
165
-
166
- # Forward page document ID to chunker
167
- # Chunker will fetch content from librarian
168
- r = TextDocument(
169
- metadata=Metadata(
170
- id=pg_uri,
171
- root=v.metadata.root,
172
- collection=v.metadata.collection,
173
- ),
174
- document_id=page_doc_id,
175
- text=b"", # Empty, chunker will fetch from librarian
176
- )
177
-
178
- await flow("output").send(r)
179
-
180
- logger.info("PDF decoding complete")
181
-
182
- @staticmethod
183
- def add_args(parser):
184
-
185
- FlowProcessor.add_args(parser)
186
-
187
- def run():
188
-
189
- Processor.launch(default_ident, __doc__)
@@ -1 +0,0 @@
1
- __version__ = "2.11.7"