Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixing pdf parsing #3349

Merged
merged 3 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/api/quivr_api/modules/sync/utils/syncutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ async def process_sync_file(
]:
raise ValueError(f"Incompatible file extension for {downloaded_file}")

storage_path = f"{storage_path.split('.')[0]}.{storage_path.split('.')[-1]}"

response = await upload_file_storage(
downloaded_file.file_data,
storage_path,
Expand Down
12 changes: 7 additions & 5 deletions backend/api/quivr_api/modules/upload/controller/upload_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ async def upload_file(
brain_id=str(brain_id),
)
)
file_name = f"{str(uploadFile.filename).split('.')[0]}.{str(uploadFile.filename).split('.')[-1]}"

background_tasks.add_task(
maybe_send_telemetry, "upload_file", {"file_name": uploadFile.filename}
maybe_send_telemetry, "upload_file", {"file_name": file_name}
)

filename_with_brain_id = str(brain_id) + "/" + str(uploadFile.filename)
filename_with_brain_id = str(brain_id) + "/" + file_name
filename_with_brain_id = f"{filename_with_brain_id.split('.')[0]}.{filename_with_brain_id.split('.')[-1]}"

buff_reader = io.BufferedReader(uploadFile.file) # type: ignore
try:
Expand All @@ -110,9 +112,9 @@ async def upload_file(

knowledge_to_add = CreateKnowledgeProperties(
brain_id=brain_id,
file_name=uploadFile.filename,
file_name=file_name,
extension=os.path.splitext(
uploadFile.filename # pyright: ignore reportPrivateUsage=none
file_name # pyright: ignore reportPrivateUsage=none
)[-1].lower(),
source=integration if integration else "local",
source_link=integration_link, # FIXME: Should return the s3 link @chloedia
Expand All @@ -127,7 +129,7 @@ async def upload_file(
"process_file_task",
kwargs={
"file_name": filename_with_brain_id,
"file_original_name": uploadFile.filename,
"file_original_name": file_name,
"brain_id": brain_id,
"notification_id": upload_notification.id,
"knowledge_id": knowledge.id,
Expand Down
22 changes: 12 additions & 10 deletions backend/core/MegaParse/megaparse/Converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,22 +320,24 @@ async def convert(
else:
raise ValueError(f"Method {self.method} not supported")

if not gpt4o_cleaner:
return LangChainDocument(
page_content=parsed_md,
metadata={"filename": file_path.name, "type": "pdf"},
)
else:
if gpt4o_cleaner:
md_processor = MarkdownProcessor(
parsed_md,
strict=True,
remove_pagination=True,
)
md_cleaned = md_processor.process(gpt4o_cleaner=gpt4o_cleaner)
return LangChainDocument(
page_content=md_cleaned,
metadata={"filename": file_path.name, "type": "pdf"},
)
parsed_md = md_cleaned

if len(parsed_md) < 5 and self.strategy == "fast":
logger.debug(f"Switching to auto strategy for {file_path.name}")
self.strategy = "auto"
return await self.convert(file_path, model, gpt4o_cleaner=gpt4o_cleaner)

return LangChainDocument(
page_content=parsed_md,
metadata={"filename": file_path.name, "type": "pdf"},
)

def save_md(self, md_content: str, file_path: Path | str) -> None:
with open(file_path, "w") as f:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def processor_metadata(self):
async def process_file_inner(self, file: QuivrFile) -> list[Document]:
mega_parse = MegaParse(file_path=file.path, config=self.megaparse_config) # type: ignore
document: Document = await mega_parse.aload()
print("\n\n document: ", document.page_content)
if len(document.page_content) > self.splitter_config.chunk_size:
docs = self.text_splitter.split_documents([document])
for doc in docs:
Expand Down
3 changes: 3 additions & 0 deletions backend/worker/quivr_worker/celery_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ def is_being_executed(task_name: str) -> bool:
running currently.
"""
active_tasks = celery.control.inspect().active()
if not active_tasks:
return False

for worker, running_tasks in active_tasks.items():
for task in running_tasks:
if task["name"] == task_name: # type: ignore
Expand Down
Loading