nw9663644-eng commented on code in PR #356: URL: https://github.com/apache/hugegraph-ai/pull/356#discussion_r3339092180
########## hugegraph-llm/src/hugegraph_llm/utils/vector_index_utils.py: ########## @@ -28,6 +29,36 @@ from hugegraph_llm.models.embeddings.init_embedding import Embeddings +def read_pdf_text(full_path: str) -> str: + try: + reader = PdfReader(full_path) + + if reader.is_encrypted: + raise gr.Error( + "Encrypted PDF files are not supported. " + "Please upload an unencrypted PDF." + ) + + page_texts = [] + for page in reader.pages: + page_text = page.extract_text() or "" + if page_text.strip(): + page_texts.append(page_text) + + text = "\n".join(page_texts).strip() + if not text: + raise gr.Error( + "No extractable text was found in this PDF. " + "Scanned-image PDFs are not supported without OCR." + ) + + return text Review Comment: I updated `read_pdf_text()` to open PDF files with a context manager and pass the binary stream to `PdfReader`, so the file handle is closed after extraction, including error paths. I also fixed the PDF text join syntax and verified the updated files with py_compile. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
