Spaces:
Sleeping
Sleeping
File size: 1,107 Bytes
6d9f20a |
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 38 39 40 41 42 43 |
from embeddings import set_embedding
from embeddings import get_chunks
from user_data import user_doc,get_docs_by_user
from response import get_answer
from PyPDF2 import PdfReader
import os
import shutil
def extract_pdf_with_user(user_id: str,pdf_path: str, name: str) -> tuple:
# Set new PDF path based on provided name
dir_path = os.path.dirname(pdf_path)
new_pdf_path = os.path.join(dir_path, f"{name}.pdf")
if pdf_path != new_pdf_path:
shutil.copy(pdf_path, new_pdf_path)
reader = PdfReader(new_pdf_path)
text = ""
for page in reader.pages:
text += page.extract_text() or ""
set_embedding(text,new_pdf_path,user_id)
user_doc(user_id, new_pdf_path)
return get_docs_by_user(user_id)
##return (user_id, text,pdf_path)
def get_final_aswer(query: str, user_id: str,pdf_path: str) -> list:
chunks = get_chunks(query,user_id,pdf_path)
full_text = ""
for chunk in chunks:
full_text += chunk
final_answer = get_answer(query,full_text)
return final_answer
|