uc / functions.py
greatIdea's picture
Update functions.py
abc88d8
from datetime import datetime
#from pypdf import PdfReader
from langchain.chains import LLMChain, RetrievalQA
#def pdf_to_text(pdf_file,name='Readme'):
# """
# Function that reads a pdf file and saves it to txt format.
# ---------------------------------------------------------
# Parameters:
# pdf_file: str.
# contains file name or path to the file in .pdf format
# name: str.
# contains the name to save the .txt file with
# Default: Readme.txt
# ---------------------------------------------------------
# Returns:
# testo: list.
# list with the text extracted on each page of the .pdf file
# ---------------------------------------------------------
# """
# testo=[]
# reader=PdfReader(pdf_file)
# for i in range(0,len(reader.pages)):
# text = reader.pages[i].extract_text()
# testo.append(text)
# try:
# with open(f'{name}.txt', 'w', encoding='utf-8') as f:
# f.writelines(testo)
# f.close()
# except Exception as e:
# print(f"passed on exception because {e}")
# return testo
def today():
today= datetime.today()
return today.strftime('%Y-%m-%d %H:%M:%S')
def ask(query):
result = RetrievalQA({"query": query})
return result['result']
def write_text_file(content, file_path):
try:
with open(file_path, 'w') as file:
file.write(content)
return True
except Exception as e:
print(f"Error occurred while writing the file: {e}")
return False