Upload 51 files
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +9 -0
- LICENSE +201 -0
- doc_ai/doc_ocr.py +131 -0
- doc_ai/pdf_data.csv +0 -0
- docs/001-010.pdf +3 -0
- docs/011-020.pdf +0 -0
- docs/021-030.pdf +0 -0
- docs/031-040.pdf +0 -0
- docs/041-050.pdf +0 -0
- docs/051-060.pdf +0 -0
- docs/061-070.pdf +0 -0
- docs/071-080.pdf +0 -0
- docs/081-090.pdf +0 -0
- docs/091-100.pdf +0 -0
- docs/101-110.pdf +0 -0
- docs/111-120.pdf +0 -0
- docs/121-130.pdf +0 -0
- docs/131-140.pdf +0 -0
- docs/141-150.pdf +0 -0
- docs/151-160.pdf +0 -0
- docs/161-170.pdf +0 -0
- docs/171-180.pdf +0 -0
- docs/181-190.pdf +0 -0
- docs/191-200.pdf +0 -0
- docs/201-210.pdf +0 -0
- docs/211-220.pdf +0 -0
- docs/221-230.pdf +0 -0
- docs/231-240.pdf +0 -0
- docs/241-250.pdf +0 -0
- docs/251-260.pdf +0 -0
- docs/261-270.pdf +0 -0
- docs/271-280.pdf +0 -0
- docs/281-290.pdf +0 -0
- docs/291-300.pdf +0 -0
- docs/301-310.pdf +0 -0
- docs/311-320.pdf +3 -0
- docs/321-330.pdf +3 -0
- docs/331-340.pdf +3 -0
- docs/341-350.pdf +3 -0
- docs/351-360.pdf +3 -0
- docs/361-370.pdf +3 -0
- docs/371-380.pdf +3 -0
- langchain_src/__pycache__/qna_chain.cpython-310.pyc +0 -0
- langchain_src/qna_chain.py +75 -0
- langchain_src/vector_db.py +33 -0
- main.py +35 -0
- requirements.txt +11 -1
- vector_db/9c519eb7-0910-44d1-adc0-4915e9534e0a/data_level0.bin +3 -0
- vector_db/9c519eb7-0910-44d1-adc0-4915e9534e0a/header.bin +3 -0
- vector_db/9c519eb7-0910-44d1-adc0-4915e9534e0a/length.bin +3 -0
.gitattributes
CHANGED
@@ -33,3 +33,12 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
docs/001-010.pdf filter=lfs diff=lfs merge=lfs -text
|
37 |
+
docs/311-320.pdf filter=lfs diff=lfs merge=lfs -text
|
38 |
+
docs/321-330.pdf filter=lfs diff=lfs merge=lfs -text
|
39 |
+
docs/331-340.pdf filter=lfs diff=lfs merge=lfs -text
|
40 |
+
docs/341-350.pdf filter=lfs diff=lfs merge=lfs -text
|
41 |
+
docs/351-360.pdf filter=lfs diff=lfs merge=lfs -text
|
42 |
+
docs/361-370.pdf filter=lfs diff=lfs merge=lfs -text
|
43 |
+
docs/371-380.pdf filter=lfs diff=lfs merge=lfs -text
|
44 |
+
vector_db/chroma.sqlite3 filter=lfs diff=lfs merge=lfs -text
|
LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Apache License
|
2 |
+
Version 2.0, January 2004
|
3 |
+
http://www.apache.org/licenses/
|
4 |
+
|
5 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6 |
+
|
7 |
+
1. Definitions.
|
8 |
+
|
9 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
10 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
11 |
+
|
12 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13 |
+
the copyright owner that is granting the License.
|
14 |
+
|
15 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
16 |
+
other entities that control, are controlled by, or are under common
|
17 |
+
control with that entity. For the purposes of this definition,
|
18 |
+
"control" means (i) the power, direct or indirect, to cause the
|
19 |
+
direction or management of such entity, whether by contract or
|
20 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22 |
+
|
23 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24 |
+
exercising permissions granted by this License.
|
25 |
+
|
26 |
+
"Source" form shall mean the preferred form for making modifications,
|
27 |
+
including but not limited to software source code, documentation
|
28 |
+
source, and configuration files.
|
29 |
+
|
30 |
+
"Object" form shall mean any form resulting from mechanical
|
31 |
+
transformation or translation of a Source form, including but
|
32 |
+
not limited to compiled object code, generated documentation,
|
33 |
+
and conversions to other media types.
|
34 |
+
|
35 |
+
"Work" shall mean the work of authorship, whether in Source or
|
36 |
+
Object form, made available under the License, as indicated by a
|
37 |
+
copyright notice that is included in or attached to the work
|
38 |
+
(an example is provided in the Appendix below).
|
39 |
+
|
40 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41 |
+
form, that is based on (or derived from) the Work and for which the
|
42 |
+
editorial revisions, annotations, elaborations, or other modifications
|
43 |
+
represent, as a whole, an original work of authorship. For the purposes
|
44 |
+
of this License, Derivative Works shall not include works that remain
|
45 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46 |
+
the Work and Derivative Works thereof.
|
47 |
+
|
48 |
+
"Contribution" shall mean any work of authorship, including
|
49 |
+
the original version of the Work and any modifications or additions
|
50 |
+
to that Work or Derivative Works thereof, that is intentionally
|
51 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53 |
+
the copyright owner. For the purposes of this definition, "submitted"
|
54 |
+
means any form of electronic, verbal, or written communication sent
|
55 |
+
to the Licensor or its representatives, including but not limited to
|
56 |
+
communication on electronic mailing lists, source code control systems,
|
57 |
+
and issue tracking systems that are managed by, or on behalf of, the
|
58 |
+
Licensor for the purpose of discussing and improving the Work, but
|
59 |
+
excluding communication that is conspicuously marked or otherwise
|
60 |
+
designated in writing by the copyright owner as "Not a Contribution."
|
61 |
+
|
62 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63 |
+
on behalf of whom a Contribution has been received by Licensor and
|
64 |
+
subsequently incorporated within the Work.
|
65 |
+
|
66 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67 |
+
this License, each Contributor hereby grants to You a perpetual,
|
68 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69 |
+
copyright license to reproduce, prepare Derivative Works of,
|
70 |
+
publicly display, publicly perform, sublicense, and distribute the
|
71 |
+
Work and such Derivative Works in Source or Object form.
|
72 |
+
|
73 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74 |
+
this License, each Contributor hereby grants to You a perpetual,
|
75 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76 |
+
(except as stated in this section) patent license to make, have made,
|
77 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78 |
+
where such license applies only to those patent claims licensable
|
79 |
+
by such Contributor that are necessarily infringed by their
|
80 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
81 |
+
with the Work to which such Contribution(s) was submitted. If You
|
82 |
+
institute patent litigation against any entity (including a
|
83 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84 |
+
or a Contribution incorporated within the Work constitutes direct
|
85 |
+
or contributory patent infringement, then any patent licenses
|
86 |
+
granted to You under this License for that Work shall terminate
|
87 |
+
as of the date such litigation is filed.
|
88 |
+
|
89 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
90 |
+
Work or Derivative Works thereof in any medium, with or without
|
91 |
+
modifications, and in Source or Object form, provided that You
|
92 |
+
meet the following conditions:
|
93 |
+
|
94 |
+
(a) You must give any other recipients of the Work or
|
95 |
+
Derivative Works a copy of this License; and
|
96 |
+
|
97 |
+
(b) You must cause any modified files to carry prominent notices
|
98 |
+
stating that You changed the files; and
|
99 |
+
|
100 |
+
(c) You must retain, in the Source form of any Derivative Works
|
101 |
+
that You distribute, all copyright, patent, trademark, and
|
102 |
+
attribution notices from the Source form of the Work,
|
103 |
+
excluding those notices that do not pertain to any part of
|
104 |
+
the Derivative Works; and
|
105 |
+
|
106 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107 |
+
distribution, then any Derivative Works that You distribute must
|
108 |
+
include a readable copy of the attribution notices contained
|
109 |
+
within such NOTICE file, excluding those notices that do not
|
110 |
+
pertain to any part of the Derivative Works, in at least one
|
111 |
+
of the following places: within a NOTICE text file distributed
|
112 |
+
as part of the Derivative Works; within the Source form or
|
113 |
+
documentation, if provided along with the Derivative Works; or,
|
114 |
+
within a display generated by the Derivative Works, if and
|
115 |
+
wherever such third-party notices normally appear. The contents
|
116 |
+
of the NOTICE file are for informational purposes only and
|
117 |
+
do not modify the License. You may add Your own attribution
|
118 |
+
notices within Derivative Works that You distribute, alongside
|
119 |
+
or as an addendum to the NOTICE text from the Work, provided
|
120 |
+
that such additional attribution notices cannot be construed
|
121 |
+
as modifying the License.
|
122 |
+
|
123 |
+
You may add Your own copyright statement to Your modifications and
|
124 |
+
may provide additional or different license terms and conditions
|
125 |
+
for use, reproduction, or distribution of Your modifications, or
|
126 |
+
for any such Derivative Works as a whole, provided Your use,
|
127 |
+
reproduction, and distribution of the Work otherwise complies with
|
128 |
+
the conditions stated in this License.
|
129 |
+
|
130 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131 |
+
any Contribution intentionally submitted for inclusion in the Work
|
132 |
+
by You to the Licensor shall be under the terms and conditions of
|
133 |
+
this License, without any additional terms or conditions.
|
134 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135 |
+
the terms of any separate license agreement you may have executed
|
136 |
+
with Licensor regarding such Contributions.
|
137 |
+
|
138 |
+
6. Trademarks. This License does not grant permission to use the trade
|
139 |
+
names, trademarks, service marks, or product names of the Licensor,
|
140 |
+
except as required for reasonable and customary use in describing the
|
141 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
142 |
+
|
143 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144 |
+
agreed to in writing, Licensor provides the Work (and each
|
145 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147 |
+
implied, including, without limitation, any warranties or conditions
|
148 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150 |
+
appropriateness of using or redistributing the Work and assume any
|
151 |
+
risks associated with Your exercise of permissions under this License.
|
152 |
+
|
153 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
154 |
+
whether in tort (including negligence), contract, or otherwise,
|
155 |
+
unless required by applicable law (such as deliberate and grossly
|
156 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157 |
+
liable to You for damages, including any direct, indirect, special,
|
158 |
+
incidental, or consequential damages of any character arising as a
|
159 |
+
result of this License or out of the use or inability to use the
|
160 |
+
Work (including but not limited to damages for loss of goodwill,
|
161 |
+
work stoppage, computer failure or malfunction, or any and all
|
162 |
+
other commercial damages or losses), even if such Contributor
|
163 |
+
has been advised of the possibility of such damages.
|
164 |
+
|
165 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
167 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168 |
+
or other liability obligations and/or rights consistent with this
|
169 |
+
License. However, in accepting such obligations, You may act only
|
170 |
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171 |
+
of any other Contributor, and only if You agree to indemnify,
|
172 |
+
defend, and hold each Contributor harmless for any liability
|
173 |
+
incurred by, or claims asserted against, such Contributor by reason
|
174 |
+
of your accepting any such warranty or additional liability.
|
175 |
+
|
176 |
+
END OF TERMS AND CONDITIONS
|
177 |
+
|
178 |
+
APPENDIX: How to apply the Apache License to your work.
|
179 |
+
|
180 |
+
To apply the Apache License to your work, attach the following
|
181 |
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182 |
+
replaced with your own identifying information. (Don't include
|
183 |
+
the brackets!) The text should be enclosed in the appropriate
|
184 |
+
comment syntax for the file format. We also recommend that a
|
185 |
+
file or class name and description of purpose be included on the
|
186 |
+
same "printed page" as the copyright notice for easier
|
187 |
+
identification within third-party archives.
|
188 |
+
|
189 |
+
Copyright [yyyy] [name of copyright owner]
|
190 |
+
|
191 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192 |
+
you may not use this file except in compliance with the License.
|
193 |
+
You may obtain a copy of the License at
|
194 |
+
|
195 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
196 |
+
|
197 |
+
Unless required by applicable law or agreed to in writing, software
|
198 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200 |
+
See the License for the specific language governing permissions and
|
201 |
+
limitations under the License.
|
doc_ai/doc_ocr.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import annotations
|
2 |
+
|
3 |
+
import glob
|
4 |
+
import osa
|
5 |
+
import re
|
6 |
+
import textwrap
|
7 |
+
|
8 |
+
from google.api_core.client_options import ClientOptions
|
9 |
+
from google.cloud import documentai
|
10 |
+
import pandas as pd
|
11 |
+
import os
|
12 |
+
|
13 |
+
def create_processor(
|
14 |
+
project_id: str, location: str, processor_display_name: str
|
15 |
+
) -> documentai.Processor:
|
16 |
+
client = documentai.DocumentProcessorServiceClient(client_options=client_options)
|
17 |
+
|
18 |
+
# The full resource name of the location
|
19 |
+
# e.g.: projects/project_id/locations/location
|
20 |
+
parent = client.common_location_path(project_id, location)
|
21 |
+
|
22 |
+
# Create a processor
|
23 |
+
return client.create_processor(
|
24 |
+
parent=parent,
|
25 |
+
processor=documentai.Processor(
|
26 |
+
display_name=processor_display_name, type_="OCR_PROCESSOR"
|
27 |
+
),
|
28 |
+
)
|
29 |
+
|
30 |
+
def process_document(
|
31 |
+
processor_name: str,
|
32 |
+
file_path: str,
|
33 |
+
) -> documentai.Document:
|
34 |
+
client = documentai.DocumentProcessorServiceClient(client_options=client_options)
|
35 |
+
|
36 |
+
# Read the file into memory
|
37 |
+
with open(file_path, "rb") as image:
|
38 |
+
image_content = image.read()
|
39 |
+
|
40 |
+
# Load Binary Data into Document AI RawDocument Object
|
41 |
+
raw_document = documentai.RawDocument(
|
42 |
+
content=image_content, mime_type="application/pdf"
|
43 |
+
)
|
44 |
+
|
45 |
+
# Configure the process request
|
46 |
+
request = documentai.ProcessRequest(name=processor_name, raw_document=raw_document)
|
47 |
+
|
48 |
+
result = client.process_document(request=request)
|
49 |
+
|
50 |
+
return result.document
|
51 |
+
|
52 |
+
def layout_to_text(layout: documentai.Document.Page.Layout, text: str) -> str:
|
53 |
+
"""
|
54 |
+
Document AI identifies text in different parts of the document by their
|
55 |
+
offsets in the entirety of the document"s text. This function converts
|
56 |
+
offsets to a string.
|
57 |
+
"""
|
58 |
+
# If a text segment spans several lines, it will
|
59 |
+
# be stored in different text segments.
|
60 |
+
return "".join(
|
61 |
+
text[int(segment.start_index) : int(segment.end_index)]
|
62 |
+
for segment in layout.text_anchor.text_segments
|
63 |
+
)
|
64 |
+
|
65 |
+
def pdf_processor(processor_name: str, extracted_data) -> list[dict]:
|
66 |
+
# Loop through each PDF file in the "docai" directory.
|
67 |
+
for path in glob.glob("docs/*.pdf"):
|
68 |
+
# Extract the file name and type from the path.
|
69 |
+
file_name, file_type = os.path.splitext(path)
|
70 |
+
|
71 |
+
print(f"Processing {file_name}")
|
72 |
+
# Process the document.
|
73 |
+
document = process_document(processor_name, file_path=path)
|
74 |
+
|
75 |
+
if not document:
|
76 |
+
print("Processing did not complete successfully.")
|
77 |
+
continue
|
78 |
+
|
79 |
+
# Split the text into chunks based on paragraphs.
|
80 |
+
document_chunks = [
|
81 |
+
layout_to_text(paragraph.layout, document.text)
|
82 |
+
for page in document.pages
|
83 |
+
for paragraph in page.paragraphs
|
84 |
+
]
|
85 |
+
|
86 |
+
# Can also split into chunks by page or blocks.
|
87 |
+
# document_chunks = [page.text for page in wrapped_document.pages]
|
88 |
+
# document_chunks = [block.text for page in wrapped_document.pages for block in page.blocks]
|
89 |
+
|
90 |
+
# Loop through each chunk and create a dictionary with metadata and content.
|
91 |
+
for chunk_number, chunk_content in enumerate(document_chunks, start=1):
|
92 |
+
# Append the chunk information to the extracted_data list.
|
93 |
+
extracted_data.append(
|
94 |
+
{
|
95 |
+
"file_name": file_name,
|
96 |
+
"file_type": file_type,
|
97 |
+
"chunk_number": chunk_number,
|
98 |
+
"content": chunk_content,
|
99 |
+
}
|
100 |
+
)
|
101 |
+
return extracted_data
|
102 |
+
|
103 |
+
|
104 |
+
if __name__ == "__main__":
|
105 |
+
project_id = "iglintdb"
|
106 |
+
location = "us"
|
107 |
+
processor_display_name = "knowledge-base-ocr-processor-test-1"
|
108 |
+
client_options = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
|
109 |
+
processor = create_processor(project_id, location, processor_display_name)
|
110 |
+
|
111 |
+
# If you already have a Document AI Processor in your project, assign the full processor resource name here.
|
112 |
+
processor_name = processor.name
|
113 |
+
chunk_size = 5000
|
114 |
+
extracted_data: list[dict] = []
|
115 |
+
|
116 |
+
extracted_data = pdf_processor(processor_name, extracted_data)
|
117 |
+
|
118 |
+
# Convert extracted_data to a sorted Pandas DataFrame
|
119 |
+
pdf_data = (
|
120 |
+
pd.DataFrame.from_dict(extracted_data)
|
121 |
+
.sort_values(by=["file_name"])
|
122 |
+
.reset_index(drop=True)
|
123 |
+
)
|
124 |
+
|
125 |
+
pdf_data.head()
|
126 |
+
|
127 |
+
pdf_data.to_csv("doc_ai/pdf_data.csv", index=False)
|
128 |
+
|
129 |
+
|
130 |
+
|
131 |
+
|
doc_ai/pdf_data.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
docs/001-010.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5a4d04741875624be1b5836c0e98106b6f30912d459852eed44e0aea47df40af
|
3 |
+
size 1299890
|
docs/011-020.pdf
ADDED
Binary file (219 kB). View file
|
|
docs/021-030.pdf
ADDED
Binary file (217 kB). View file
|
|
docs/031-040.pdf
ADDED
Binary file (215 kB). View file
|
|
docs/041-050.pdf
ADDED
Binary file (237 kB). View file
|
|
docs/051-060.pdf
ADDED
Binary file (240 kB). View file
|
|
docs/061-070.pdf
ADDED
Binary file (245 kB). View file
|
|
docs/071-080.pdf
ADDED
Binary file (215 kB). View file
|
|
docs/081-090.pdf
ADDED
Binary file (265 kB). View file
|
|
docs/091-100.pdf
ADDED
Binary file (212 kB). View file
|
|
docs/101-110.pdf
ADDED
Binary file (286 kB). View file
|
|
docs/111-120.pdf
ADDED
Binary file (339 kB). View file
|
|
docs/121-130.pdf
ADDED
Binary file (258 kB). View file
|
|
docs/131-140.pdf
ADDED
Binary file (335 kB). View file
|
|
docs/141-150.pdf
ADDED
Binary file (294 kB). View file
|
|
docs/151-160.pdf
ADDED
Binary file (243 kB). View file
|
|
docs/161-170.pdf
ADDED
Binary file (245 kB). View file
|
|
docs/171-180.pdf
ADDED
Binary file (244 kB). View file
|
|
docs/181-190.pdf
ADDED
Binary file (236 kB). View file
|
|
docs/191-200.pdf
ADDED
Binary file (265 kB). View file
|
|
docs/201-210.pdf
ADDED
Binary file (224 kB). View file
|
|
docs/211-220.pdf
ADDED
Binary file (236 kB). View file
|
|
docs/221-230.pdf
ADDED
Binary file (224 kB). View file
|
|
docs/231-240.pdf
ADDED
Binary file (205 kB). View file
|
|
docs/241-250.pdf
ADDED
Binary file (254 kB). View file
|
|
docs/251-260.pdf
ADDED
Binary file (214 kB). View file
|
|
docs/261-270.pdf
ADDED
Binary file (275 kB). View file
|
|
docs/271-280.pdf
ADDED
Binary file (274 kB). View file
|
|
docs/281-290.pdf
ADDED
Binary file (265 kB). View file
|
|
docs/291-300.pdf
ADDED
Binary file (301 kB). View file
|
|
docs/301-310.pdf
ADDED
Binary file (703 kB). View file
|
|
docs/311-320.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f37c8f6e348577ccbc0abe749c602f73cc1ef90bf129e239d91d2298c5fc6109
|
3 |
+
size 3607018
|
docs/321-330.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:16461b40264993463a042fcb0353d266999c861fcf9011a5f9d70366b74a1ef9
|
3 |
+
size 1169269
|
docs/331-340.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:438366fd4ba95727a15b47f1cc955a04977b77385c94b50fa697c641ffc44eec
|
3 |
+
size 4605479
|
docs/341-350.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:05c3aa2c48ed06000db605b7930c32047576652ac9470a5ffa1ff23129626e53
|
3 |
+
size 2712345
|
docs/351-360.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:60935f8f2b1831ac3169b91c9250a6aa167646947a9bab08c867eeec4aa15b34
|
3 |
+
size 2556978
|
docs/361-370.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c9867a6701f00ee71b4e0800d10782b699b37c2a163a5118671ee9f3900c4b25
|
3 |
+
size 1044114
|
docs/371-380.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f92006f4329e7a51c4d1b2988e4b0add66a9575bff7d017e2eb62e01008e7df6
|
3 |
+
size 1140799
|
langchain_src/__pycache__/qna_chain.cpython-310.pyc
ADDED
Binary file (2.27 kB). View file
|
|
langchain_src/qna_chain.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.retrievers.multi_query import MultiQueryRetriever
|
2 |
+
from langchain_openai import ChatOpenAI
|
3 |
+
from langchain_community.document_transformers import LongContextReorder
|
4 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
5 |
+
from langchain.schema import Document
|
6 |
+
from langchain_chroma import Chroma
|
7 |
+
from langchain_openai import OpenAIEmbeddings
|
8 |
+
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
9 |
+
from langchain.chains import create_retrieval_chain
|
10 |
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
11 |
+
from langchain_core.messages import HumanMessage
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
def generate_response(input, history):
|
16 |
+
|
17 |
+
vector_db = Chroma(persist_directory="vector_db", embedding_function=OpenAIEmbeddings())
|
18 |
+
|
19 |
+
# Multi-query retriever
|
20 |
+
retriever = vector_db.as_retriever(
|
21 |
+
search_type="similarity_score_threshold", search_kwargs={"score_threshold": 0.7, "k": 10}
|
22 |
+
)
|
23 |
+
|
24 |
+
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.2)
|
25 |
+
retriever = MultiQueryRetriever.from_llm(
|
26 |
+
retriever=retriever, llm=llm
|
27 |
+
)
|
28 |
+
|
29 |
+
unique_docs = retriever.invoke(input)
|
30 |
+
reordering = LongContextReorder()
|
31 |
+
reordered_docs = reordering.transform_documents(unique_docs)
|
32 |
+
|
33 |
+
SYSTEM_TEMPLATE = """
|
34 |
+
Answer the user's questions based on the below context.
|
35 |
+
The context will always be relevant to the question.
|
36 |
+
Use chat history if required to answer the question.
|
37 |
+
Always be very descriptive, unless stated otherwise.
|
38 |
+
Explain as if you are a master at this subject.
|
39 |
+
<context>
|
40 |
+
{context}
|
41 |
+
</context>
|
42 |
+
<chat_history>
|
43 |
+
{chat_history}
|
44 |
+
</chat_history>
|
45 |
+
"""
|
46 |
+
|
47 |
+
question_answering_prompt = ChatPromptTemplate.from_messages(
|
48 |
+
[
|
49 |
+
(
|
50 |
+
"system",
|
51 |
+
SYSTEM_TEMPLATE,
|
52 |
+
),
|
53 |
+
MessagesPlaceholder(variable_name="messages"),
|
54 |
+
]
|
55 |
+
)
|
56 |
+
|
57 |
+
document_chain = create_stuff_documents_chain(llm, question_answering_prompt)
|
58 |
+
|
59 |
+
reponse = document_chain.invoke(
|
60 |
+
{
|
61 |
+
"context": reordered_docs,
|
62 |
+
"messages": [
|
63 |
+
HumanMessage(content=input)
|
64 |
+
],
|
65 |
+
"chat_history": history
|
66 |
+
}
|
67 |
+
)
|
68 |
+
|
69 |
+
return reponse
|
70 |
+
|
71 |
+
|
72 |
+
if __name__ == "__main__":
|
73 |
+
prompt = "How should my MDPE pipe cross a Nallah?"
|
74 |
+
response = generate_response(prompt)
|
75 |
+
print(response)
|
langchain_src/vector_db.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
2 |
+
from langchain.schema import Document
|
3 |
+
from langchain_community.vectorstores import Chroma
|
4 |
+
from langchain_openai import OpenAIEmbeddings
|
5 |
+
import pandas as pd
|
6 |
+
|
7 |
+
def load_csv_data(file_path: str):
|
8 |
+
|
9 |
+
df = pd.read_csv(file_path)
|
10 |
+
texts = ""
|
11 |
+
for i in range(len(df)):
|
12 |
+
texts += df.iloc[i]["content"]
|
13 |
+
|
14 |
+
# Split the texts into smaller chunks
|
15 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
16 |
+
chunk_size=1000,
|
17 |
+
chunk_overlap=200
|
18 |
+
)
|
19 |
+
split_texts = text_splitter.split_text(texts) # Split the concatenated text into smaller chunks
|
20 |
+
|
21 |
+
documents = [Document(page_content=text) for text in split_texts] # Create Document instances
|
22 |
+
|
23 |
+
return documents # Return the created documents
|
24 |
+
|
25 |
+
def create_vector_db(docs: list[Document]):
|
26 |
+
persist_directory = "vector_db"
|
27 |
+
vector_db = Chroma.from_documents(docs, embedding=OpenAIEmbeddings(), persist_directory=persist_directory)
|
28 |
+
return vector_db
|
29 |
+
|
30 |
+
if __name__ == "__main__":
|
31 |
+
docs = load_csv_data("doc_ai/pdf_data.csv")
|
32 |
+
vector_db = create_vector_db(docs)
|
33 |
+
|
main.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_src.qna_chain import generate_response
|
2 |
+
import gradio as gr
|
3 |
+
import time
|
4 |
+
|
5 |
+
title = """<h1 align="center">ChatIGL</h1>"""
|
6 |
+
description = """<br><br><h3 align="center">This is a chat model, which can currently answer questions to IGL Docs provided.</h3>"""
|
7 |
+
|
8 |
+
|
9 |
+
with gr.Blocks(theme="glass") as demo:
|
10 |
+
|
11 |
+
gr.HTML(title)
|
12 |
+
|
13 |
+
chatbot = gr.Chatbot(type="messages", height=750)
|
14 |
+
msg = gr.Textbox()
|
15 |
+
clear = gr.ClearButton([msg, chatbot])
|
16 |
+
|
17 |
+
def respond(message, chat_history):
|
18 |
+
bot_message = generate_response(message, chat_history)
|
19 |
+
chat_history.append({"role": "user", "content": message})
|
20 |
+
chat_history.append({"role": "assistant", "content": bot_message})
|
21 |
+
return "", chat_history
|
22 |
+
|
23 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
24 |
+
|
25 |
+
gr.Examples([
|
26 |
+
["How should a MDPE pipe cross a Nallah?"],
|
27 |
+
["At what depth should a MDPE pipe be laid?"],
|
28 |
+
|
29 |
+
], inputs=msg, label= "Examples"
|
30 |
+
)
|
31 |
+
|
32 |
+
gr.HTML(description)
|
33 |
+
|
34 |
+
|
35 |
+
demo.launch()
|
requirements.txt
CHANGED
@@ -1 +1,11 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
google-cloud-aiplatform==1.35.0
|
2 |
+
google-cloud-documentai==2.20.1
|
3 |
+
backoff==2.2.1
|
4 |
+
pandas==2.2.3
|
5 |
+
tenacity==9.0.0
|
6 |
+
osa==0.2.3
|
7 |
+
langchain-community==0.3.3
|
8 |
+
langchain-openai==0.2.3
|
9 |
+
chromadb==0.5.15
|
10 |
+
langchain-chroma==0.1.4
|
11 |
+
gradio==5.1.0
|
vector_db/9c519eb7-0910-44d1-adc0-4915e9534e0a/data_level0.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f18abd8c514282db82706e52b0a33ed659cd534e925a6f149deb7af9ce34bd8e
|
3 |
+
size 6284000
|
vector_db/9c519eb7-0910-44d1-adc0-4915e9534e0a/header.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:effaa959ce2b30070fdafc2fe82096fc46e4ee7561b75920dd3ce43d09679b21
|
3 |
+
size 100
|
vector_db/9c519eb7-0910-44d1-adc0-4915e9534e0a/length.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:fc19b1997119425765295aeab72d76faa6927d4f83985d328c26f20468d6cc76
|
3 |
+
size 4000
|