Nikhil57 commited on
Commit
e9e6672
·
verified ·
1 Parent(s): ac707d9

Upload 5 files

Browse files
Files changed (5) hide show
  1. .env +2 -0
  2. .env.example +2 -0
  3. app.py +37 -0
  4. requirements.txt +0 -0
  5. utils.py +92 -0
.env ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ REPLICATE_API_TOKEN = "<Your_Replicate_APIKey>"
2
+ OPENAI_API_KEY="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"
.env.example ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ REPLICATE_API_TOKEN = ""
2
+ OPENAI_API_KEY=""
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from dotenv import load_dotenv
3
+ from utils import *
4
+
5
+
6
+ def main():
7
+ load_dotenv()
8
+
9
+ st.set_page_config(page_title="Invoice Extraction Bot")
10
+ st.title("Invoice Extraction Bot...💁 ")
11
+ st.subheader("I can help you in extracting invoice data")
12
+
13
+
14
+ # Upload the Invoices (pdf files)...
15
+ pdf = st.file_uploader("Upload invoices here, only PDF files allowed", type=["pdf"],accept_multiple_files=True)
16
+
17
+ submit=st.button("Extract Data")
18
+
19
+ if submit:
20
+ with st.spinner('Wait for it...'):
21
+ df=create_docs(pdf)
22
+ st.write(df.head())
23
+
24
+ data_as_csv= df.to_csv(index=False).encode("utf-8")
25
+ st.download_button(
26
+ "Download data as CSV",
27
+ data_as_csv,
28
+ "benchmark-tools.csv",
29
+ "text/csv",
30
+ key="download-tools-csv",
31
+ )
32
+ st.success("Hope I was able to save your time❤️")
33
+
34
+
35
+ #Invoking main function
36
+ if __name__ == '__main__':
37
+ main()
requirements.txt ADDED
Binary file (216 Bytes). 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