Spaces:
Runtime error
Runtime error
Commit
·
d8a0a3d
1
Parent(s):
ecd571b
Upload 5 files
Browse files- .env.example +2 -0
- Invoice/invoice_1001329.pdf +0 -0
- Invoice/invoice_2001321.pdf +0 -0
- Invoice/invoice_3452334.pdf +0 -0
- utils.py +92 -0
.env.example
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
REPLICATE_API_TOKEN = ""
|
| 2 |
+
OPENAI_API_KEY=""
|
Invoice/invoice_1001329.pdf
ADDED
|
Binary file (23.3 kB). View file
|
|
|
Invoice/invoice_2001321.pdf
ADDED
|
Binary file (23.3 kB). View file
|
|
|
Invoice/invoice_3452334.pdf
ADDED
|
Binary file (22.3 kB). View file
|
|
|
utils.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.llms import OpenAI
|
| 2 |
+
from pypdf import PdfReader
|
| 3 |
+
from langchain.llms.openai import OpenAI
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import re
|
| 6 |
+
import replicate
|
| 7 |
+
from langchain.prompts import PromptTemplate
|
| 8 |
+
|
| 9 |
+
#Extract Information from PDF file
|
| 10 |
+
def get_pdf_text(pdf_doc):
|
| 11 |
+
text = ""
|
| 12 |
+
pdf_reader = PdfReader(pdf_doc)
|
| 13 |
+
for page in pdf_reader.pages:
|
| 14 |
+
text += page.extract_text()
|
| 15 |
+
return text
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
#Function to extract data from text
|
| 20 |
+
def extracted_data(pages_data):
|
| 21 |
+
|
| 22 |
+
template = """Extract all the following values : invoice no., Description, Quantity, date,
|
| 23 |
+
Unit price , Amount, Total, email, phone number and address from this data: {pages}
|
| 24 |
+
|
| 25 |
+
Expected output: remove any dollar symbols {{'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'}}
|
| 26 |
+
"""
|
| 27 |
+
prompt_template = PromptTemplate(input_variables=["pages"], template=template)
|
| 28 |
+
|
| 29 |
+
llm = OpenAI(temperature=.7)
|
| 30 |
+
full_response=llm(prompt_template.format(pages=pages_data))
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
#The below code will be used when we want to use LLAMA 2 model, we will use Replicate for hosting our model...
|
| 34 |
+
|
| 35 |
+
#output = replicate.run('replicate/llama-2-70b-chat:2c1608e18606fad2812020dc541930f2d0495ce32eee50074220b87300bc16e1',
|
| 36 |
+
#input={"prompt":prompt_template.format(pages=pages_data) ,
|
| 37 |
+
#"temperature":0.1, "top_p":0.9, "max_length":512, "repetition_penalty":1})
|
| 38 |
+
|
| 39 |
+
#full_response = ''
|
| 40 |
+
#for item in output:
|
| 41 |
+
#full_response += item
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
#print(full_response)
|
| 45 |
+
return full_response
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
# iterate over files in
|
| 49 |
+
# that user uploaded PDF files, one by one
|
| 50 |
+
def create_docs(user_pdf_list):
|
| 51 |
+
|
| 52 |
+
df = pd.DataFrame({'Invoice no.': pd.Series(dtype='str'),
|
| 53 |
+
'Description': pd.Series(dtype='str'),
|
| 54 |
+
'Quantity': pd.Series(dtype='str'),
|
| 55 |
+
'Date': pd.Series(dtype='str'),
|
| 56 |
+
'Unit price': pd.Series(dtype='str'),
|
| 57 |
+
'Amount': pd.Series(dtype='int'),
|
| 58 |
+
'Total': pd.Series(dtype='str'),
|
| 59 |
+
'Email': pd.Series(dtype='str'),
|
| 60 |
+
'Phone number': pd.Series(dtype='str'),
|
| 61 |
+
'Address': pd.Series(dtype='str')
|
| 62 |
+
})
|
| 63 |
+
|
| 64 |
+
for filename in user_pdf_list:
|
| 65 |
+
|
| 66 |
+
print(filename)
|
| 67 |
+
raw_data=get_pdf_text(filename)
|
| 68 |
+
#print(raw_data)
|
| 69 |
+
#print("extracted raw data")
|
| 70 |
+
|
| 71 |
+
llm_extracted_data=extracted_data(raw_data)
|
| 72 |
+
#print("llm extracted data")
|
| 73 |
+
#Adding items to our list - Adding data & its metadata
|
| 74 |
+
|
| 75 |
+
pattern = r'{(.+)}'
|
| 76 |
+
match = re.search(pattern, llm_extracted_data, re.DOTALL)
|
| 77 |
+
|
| 78 |
+
if match:
|
| 79 |
+
extracted_text = match.group(1)
|
| 80 |
+
# Converting the extracted text to a dictionary
|
| 81 |
+
data_dict = eval('{' + extracted_text + '}')
|
| 82 |
+
print(data_dict)
|
| 83 |
+
else:
|
| 84 |
+
print("No match found.")
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
df=df.append([data_dict], ignore_index=True)
|
| 88 |
+
print("********************DONE***************")
|
| 89 |
+
#df=df.append(save_to_dataframe(llm_extracted_data), ignore_index=True)
|
| 90 |
+
|
| 91 |
+
df.head()
|
| 92 |
+
return df
|