Copilot commented on code in PR #356: URL: https://github.com/apache/hugegraph-ai/pull/356#discussion_r3337408857
########## 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: `PdfReader(full_path)` opens the PDF file internally and can keep the underlying file handle open until the reader is garbage-collected. Since `read_documents()` may be called repeatedly (and multiple PDFs can be uploaded), this can leak file descriptors over time. Open the file with a context manager and pass the binary stream to `PdfReader` so it is always closed, including on early error paths (encrypted / no-text PDFs). -- 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]
