Team Ai
Apppublic

Multimedika/Bot_Development

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
reader.py39 linesDownload Raw Back to service
1from typing import Optional, List2from fastapi import UploadFile3from fastapi.responses import JSONResponse4from llama_index.core.readers.base import BaseReader5from llama_index.core.schema import Document6from PyPDF2 import PdfReader7 8 9class Reader(BaseReader):10    async def read_from_uploadfile(self, file: UploadFile) -> List[Document]:11        try:12            file_content = await file.read()13 14            # Initialize PdfReader with file-like object15            reader = PdfReader(file.file)16            total_pages = reader.pages17            print("Total pages: ", len(total_pages))18 19            # Extract text from each page and store it in a list20            documents = []21            for page_num, page in enumerate(total_pages, start=1):22                text = page.extract_text() or ""  # Extract text or use empty if none23                if text.strip():  # Only add non-empty text as a document24                    documents.append(25                        Document(text=text.strip(), metadata={"page": page_num})26                    )27                else:28                    # Handle the case where a page is empty but should still be accounted for29                    documents.append(Document(text="", metadata={"page": page_num}))30 31            print("Number of documents: ", len(documents))32 33            return documents34 35        except Exception as e:36            return JSONResponse(37                status_code=500, content=f"Failed to process the uploaded file: {e}"38            )39