Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
dify
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ai-tech
dify
Commits
7a7fb8c6
Commit
7a7fb8c6
authored
Jun 21, 2023
by
jyong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improve pdf import
parent
a92b028a
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
7 additions
and
15 deletions
+7
-15
pdf.py
api/core/data_loader/loader/pdf.py
+5
-14
requirements.txt
api/requirements.txt
+2
-1
No files found.
api/core/data_loader/loader/pdf.py
View file @
7a7fb8c6
import
logging
from
typing
import
List
,
Optional
from
langchain.document_loaders
import
PyPDFium2Loader
from
langchain.document_loaders.base
import
BaseLoader
from
langchain.schema
import
Document
from
pypdf
import
PdfReader
...
...
@@ -42,20 +43,10 @@ class PdfLoader(BaseLoader):
return
[
Document
(
page_content
=
text
,
metadata
=
metadata
)]
except
FileNotFoundError
:
pass
documents
=
PyPDFium2Loader
(
file_path
=
self
.
_file_path
)
.
load
()
text_list
=
[]
with
open
(
self
.
_file_path
,
"rb"
)
as
fp
:
# Create a PDF object
pdf
=
PdfReader
(
fp
)
# Get the number of pages in the PDF document
num_pages
=
len
(
pdf
.
pages
)
# Iterate over every page
for
page
in
range
(
num_pages
):
# Extract the text from the page
page_text
=
pdf
.
pages
[
page
]
.
extract_text
()
text_list
.
append
(
page_text
)
for
document
in
documents
:
text_list
.
append
(
document
.
page_content
)
text
=
"
\n\n
"
.
join
(
text_list
)
# save plaintext file for caching
...
...
@@ -63,5 +54,5 @@ class PdfLoader(BaseLoader):
storage
.
save
(
plaintext_file_key
,
text
.
encode
(
'utf-8'
))
metadata
=
{
"source"
:
self
.
_file_path
}
return
[
Document
(
page_content
=
text
,
metadata
=
metadata
)]
return
documents
api/requirements.txt
View file @
7a7fb8c6
...
...
@@ -32,3 +32,4 @@ pypdf==3.8.1
openpyxl==3.1.2
chardet~=5.1.0
docx2txt==0.8
pypdfium2==4.16.0
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment