fix pdf parser bug
This commit is contained in:
parent
ee84c4f391
commit
e968a3d9f8
@ -7,6 +7,7 @@ Flask_Cors>=4.0.1
|
||||
llama_index>=0.11.8
|
||||
openai>=1.44.1
|
||||
paddleocr>=2.8.1
|
||||
paddlepaddle>=2.4.0
|
||||
pandas>=2.2.2
|
||||
Pillow>=10.4.0
|
||||
pymilvus>=2.4.4
|
||||
@ -19,4 +20,5 @@ zhipuai>=2.1.2
|
||||
neo4j>=5.22.0
|
||||
sentencepiece==0.1.99
|
||||
llama-index-readers-file
|
||||
opencv-python-headless
|
||||
docx2txt
|
||||
@ -1,6 +1,7 @@
|
||||
import os
|
||||
import json
|
||||
import time
|
||||
from src.plugins import pdf2txt
|
||||
from src.utils import hashstr, setup_logger, is_text_pdf
|
||||
from src.models.embedding import get_embedding_model
|
||||
|
||||
@ -108,7 +109,7 @@ class DataBaseManager:
|
||||
"file_id": "file_" + hashstr(file + str(time.time())),
|
||||
"filename": os.path.basename(file),
|
||||
"path": file,
|
||||
"type": file.split(".")[-1],
|
||||
"type": file.split(".")[-1].lower(),
|
||||
"status": "waiting",
|
||||
"created_at": time.time()
|
||||
}
|
||||
@ -122,7 +123,12 @@ class DataBaseManager:
|
||||
db.files[idx]["status"] = "processing"
|
||||
|
||||
try:
|
||||
nodes = chunk(new_file["path"], params=params)
|
||||
if new_file["type"] == "pdf":
|
||||
texts = self.read_text(new_file["path"])
|
||||
nodes = chunk(texts, params=params)
|
||||
else:
|
||||
nodes = chunk(new_file["path"], params=params)
|
||||
|
||||
self.knowledge_base.add_documents(
|
||||
docs=[node.text for node in nodes],
|
||||
collection_name=db.metaname,
|
||||
|
||||
@ -18,7 +18,7 @@ def chunk(text_or_path, params=None):
|
||||
|
||||
if os.path.isfile(text_or_path) and "uploads" in text_or_path:
|
||||
parser = SimpleFileNodeParser()
|
||||
file_type = Path(text_or_path).suffix
|
||||
file_type = Path(text_or_path).suffix.lower()
|
||||
if file_type in [".txt", ".json", ".md"]:
|
||||
docs = FlatReader().load_data(Path(text_or_path))
|
||||
elif file_type in [".docx"]:
|
||||
|
||||
@ -4,8 +4,9 @@ import fitz # fitz就是pip install PyMuPDF
|
||||
from PIL import Image
|
||||
from copy import deepcopy
|
||||
from tqdm import tqdm
|
||||
from pathlib import Path
|
||||
from argparse import ArgumentParser
|
||||
from src.utils import logger
|
||||
from src.utils import logger, is_text_pdf
|
||||
import numpy as np # Added import for numpy
|
||||
|
||||
GOLBAL_STATE = {}
|
||||
@ -15,6 +16,9 @@ def pdf2txt(pdf_path, return_text=False):
|
||||
if not os.path.exists(pdf_path):
|
||||
raise FileNotFoundError(f"File not found: {pdf_path}")
|
||||
|
||||
if is_text_pdf(pdf_path):
|
||||
return pdfreader(pdf_path)
|
||||
|
||||
# Importing these modules here to avoid unnecessary imports in other files
|
||||
from paddleocr import PPStructure, save_structure_res
|
||||
from paddleocr.ppstructure.recovery.recovery_to_doc import sorted_layout_boxes, convert_info_docx
|
||||
@ -108,6 +112,29 @@ def convert_imgs(pdf_path, output_dir):
|
||||
def get_state(task_id):
|
||||
return GOLBAL_STATE.get(task_id, {})
|
||||
|
||||
|
||||
def pdfreader(file_path):
|
||||
"""读取PDF文件并返回text文本"""
|
||||
assert os.path.exists(file_path), "File not found"
|
||||
assert file_path.endswith(".pdf"), "File format not supported"
|
||||
|
||||
from llama_index.readers.file import PDFReader
|
||||
doc = PDFReader().load_data(file=Path(file_path))
|
||||
|
||||
# 简单的拼接起来之后返回纯文本
|
||||
text = "\n\n".join([d.get_content() for d in doc])
|
||||
return text
|
||||
|
||||
def plainreader(file_path):
|
||||
"""读取普通文本文件并返回text文本"""
|
||||
assert os.path.exists(file_path), "File not found"
|
||||
|
||||
with open(file_path, "r") as f:
|
||||
text = f.read()
|
||||
return text
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = ArgumentParser()
|
||||
parser.add_argument('--pdf-path', type=str, required=True, help='Path to the PDF file')
|
||||
|
||||
@ -118,7 +118,7 @@ def upload_file():
|
||||
if file:
|
||||
upload_dir = os.path.join(startup.config.save_dir, "data/uploads")
|
||||
os.makedirs(upload_dir, exist_ok=True)
|
||||
filename = f"{hashstr(file.filename, 4, with_salt=True)}_{file.filename}"
|
||||
filename = f"{hashstr(file.filename, 4, with_salt=True)}_{file.filename}".lower()
|
||||
file_path = os.path.join(upload_dir, filename)
|
||||
file.save(file_path)
|
||||
return jsonify({'message': 'File successfully uploaded', 'file_path': file_path}), 200
|
||||
|
||||
Loading…
Reference in New Issue
Block a user