Download pdf_reader.py from student2222333051/summarizer_space: direct link, hf CLI and curl.
- Browser
- Download file 1.38 kB
-
https://huggingface.co/spaces/student2222333051/summarizer_space/resolve/main/pdf_reader.py
- Command line
-
hf download hf://spaces/student2222333051/summarizer_space/pdf_reader.py
-
curl -L -o pdf_reader.py https://huggingface.co/spaces/student2222333051/summarizer_space/resolve/main/pdf_reader.py
1.38 kB
| # pdf_reader.py | |
| import io | |
| from typing import Optional | |
| # Бірнеше кітапхана арқылы оқуды қолдаймыз — ең алдымен PyPDF2, егер қажет болса pdfminer.six | |
| try: | |
| from PyPDF2 import PdfReader | |
| _has_pypdf2 = True | |
| except Exception: | |
| _has_pypdf2 = False | |
| try: | |
| from pdfminer.high_level import extract_text as pdfminer_extract_text | |
| _has_pdfminer = True | |
| except Exception: | |
| _has_pdfminer = False | |
| def extract_text_from_pdf(pdf_bytes: bytes) -> str: | |
| """ | |
| PDF-тен мәтін алу. Бірнеше әдісті қолданып көреді. | |
| """ | |
| # 1) PyPDF2 | |
| if _has_pypdf2: | |
| try: | |
| reader = PdfReader(io.BytesIO(pdf_bytes)) | |
| text = "" | |
| for page in reader.pages: | |
| page_text = page.extract_text() | |
| if page_text: | |
| text += page_text + "\n" | |
| if text.strip(): | |
| return text | |
| except Exception: | |
| pass | |
| # 2) pdfminer.six (әдетте күрделі PDF-тарға жақсы) | |
| if _has_pdfminer: | |
| try: | |
| text = pdfminer_extract_text(io.BytesIO(pdf_bytes)) | |
| if text and text.strip(): | |
| return text | |
| except Exception: | |
| pass | |
| # Егер ештеңе шықпаса — бос қайтарың | |
| return "" | |