chriscob21's picture
Update utils.py
fff0084 verified
Raw
History Blame Contribute Delete
3.79 kB
from openai import OpenAI
#from langchain_openai import OpenAI
from pypdf import PdfReader
import PyPDF2
import pandas as pd
import re
import replicate
from langchain.prompts import PromptTemplate
#Extract Information from PDF file
def get_pdf_text(pdf_doc):
text = ""
pdf_reader = PyPDF2.PdfReader(pdf_doc)
for page in pdf_reader.pages:
text += page.extract_text()
return text
#Function to extract data from text
def extracted_data(pages_data):
template = """Extract all the following values : invoice no., Description, Quantity, date, Unit price , Amount, Total, email, phone number and address from this data: {pages}
Expected output: remove any dollar symbols and the object must be in JSON format between curly brackets. this is the format {{'Invoice no.': '1001329','Description': 'Office Chair','Quantity': '2','Date': '5/4/2023','Unit price': '1100.00','Amount': '2200.00','Total': '2200.00','Email': 'Santoshvarma0988@gmail.com','Phone number': '9999999999','Address': 'Mumbai, India'}}
"""
prompt_template = PromptTemplate(input_variables=["pages"], template=template)
#llm = OpenAI(temperature=0.7)
#full_response= llm.invoke(prompt_template.format(pages=pages_data))
#The below code will be used when we want to use LLAMA 2 model, we will use Replicate for hosting our model....
output = replicate.run('meta/llama-2-7b-chat:13c3cdee13ee059ab779f0291d29054dab00a47dad8261375654de5540165fb0',
input={"prompt":prompt_template.format(pages=pages_data) ,"temperature":0.1, "top_p":0.9, "max_new_tokens": 250,"max_length":500, "repetition_penalty":1})
full_response = ''
for item in output: #Uncomment this if you want to use LLAMA 2 model
full_response += item
print(full_response)
return full_response
# iterate over files in
# that user uploaded PDF files, one by one
def create_docs(user_pdf_list):
df = pd.DataFrame({'Invoice no.': pd.Series(dtype='str'),
'Description': pd.Series(dtype='str'),
'Quantity': pd.Series(dtype='str'),
'Date': pd.Series(dtype='str'),
'Unit price': pd.Series(dtype='str'),
'Amount': pd.Series(dtype='int'),
'Total': pd.Series(dtype='str'),
'Email': pd.Series(dtype='str'),
'Phone number': pd.Series(dtype='str'),
'Address': pd.Series(dtype='str')})
for filename in user_pdf_list:
print(filename)
raw_data=get_pdf_text(filename)
print(raw_data)
print("extracted raw data")
llm_extracted_data=extracted_data(raw_data)
print("llm extracted data")
#Adding items to our list - Adding data & its metadata
pattern = r'{(.+)}'
match = re.search(pattern, llm_extracted_data, re.DOTALL)
# = re.compile(r'{(.+)}', re.DOTALL)
#match = pattern.search(llm_extracted_data)
data_dict = {}
#data_dict = {'text':llm_extracted_data} # Initialize data_dict with an empty dictionary
if match:
extracted_text = match.group(1)
# Converting the extracted text to a dictionary
#data_dict = json.loads(extracted_text)
data_dict = eval('{' + extracted_text + '}')
print(data_dict)
else:
print("No match found.")
#df = pd.concat([df, pd.DataFrame(data_dict, index=[0])], ignore_index=True)
df=df._append(data_dict, ignore_index=True)
print("********************DONE***************")
#df = pd.concat([df, save_to_dataframe(llm_extracted_data)], ignore_index=True)
df.head()
return df