-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_handler.py
More file actions
37 lines (31 loc) · 1.28 KB
/
Copy pathfile_handler.py
File metadata and controls
37 lines (31 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from pypdf import PdfReader
from datetime import datetime,timezone
from docx import Document
from uuid import uuid4
from search import save_embedding_to_search
def read_docx(file_path):
document = Document(file_path)
return [paragraph.text for paragraph in document.paragraphs]
def read_pdf(file_path):
with open(file_path, "rb") as f:
reader = PdfReader(f)
return [page.extract_text() for page in reader.pages]
def convert_file_to_text(pdf_file_path, request_id, only_text=False):
page_list = []
if pdf_file_path.endswith(".docx"):
page_list = read_docx(pdf_file_path)
elif pdf_file_path.endswith(".pdf"):
page_list = read_pdf(pdf_file_path)
if only_text:
return ' '.join(page_list)
current_time = datetime.now(timezone.utc).isoformat() # Get current time in ISO format
all_pages_text = [{
"Seitenzahl": idx+1, "id": f"{uuid4()}", "Dateiname": pdf_file_path.split("/")[-1],
"text": page, "requestId": request_id,"timeStamp": current_time # Add current timestamp
} for idx, page in enumerate(page_list)]
return all_pages_text
def save_file_to_search(filename, request_id):
documents = convert_file_to_text(filename, request_id)
save_embedding_to_search(documents)
if __name__ == "__main__":
pass