Update
Browse files- backend.py +56 -0
backend.py
CHANGED
@@ -9,3 +9,59 @@ from langchain_google_genai import GoogleGenerativeAI
|
|
9 |
|
10 |
os.environ["GOOGLE_API_KEY"] = "AIzaSyCYGj5e2eAQbUi9HtuMaW0LDSnDuxLG54U"
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
os.environ["GOOGLE_API_KEY"] = "AIzaSyCYGj5e2eAQbUi9HtuMaW0LDSnDuxLG54U"
|
11 |
|
12 |
+
|
13 |
+
class InvoicePipeline:
|
14 |
+
|
15 |
+
def __init__(self, paths):
|
16 |
+
self._paths = paths
|
17 |
+
self._llm = GoogleGenerativeAI(model="gemini-1.5-pro", google_api_key=api_key)
|
18 |
+
self._prompt_template = self._get_default_prompt_template()
|
19 |
+
# This funcition will help in extracting and run the code, and will produce a dataframe for us
|
20 |
+
def run(self) -> pd.DataFrame:
|
21 |
+
# We have defined the way the data has to be returned
|
22 |
+
df = pd.DataFrame(
|
23 |
+
"Invoice ID": pd.Series(dtype = "int"),
|
24 |
+
"DESCRIPTION": pd.Series(dtype = "str"),
|
25 |
+
"Issue Data": pd.Series(dtype = "str"),
|
26 |
+
"UNIT PRICE": pd.Series(dtype = "str"),
|
27 |
+
"AMOUNT": pd.Series(dtype = "int"),
|
28 |
+
"Bill For": pd.Series(dtype = "str"),
|
29 |
+
"From": pd.Series(dtype =" str"),
|
30 |
+
"Terms": pd.Series(dtype = "str")}
|
31 |
+
)
|
32 |
+
|
33 |
+
for path in self._paths:
|
34 |
+
raw_text = self._get_raw_text_from_pdf(path) # This function needs to be created
|
35 |
+
llm_resp = self._extract_data_from_llm(raw_text) #
|
36 |
+
data = self._parse_response(llm_resp)
|
37 |
+
df = pd.concat([df, pd.DataFrame([data])], ignore_index = True)
|
38 |
+
|
39 |
+
return df
|
40 |
+
|
41 |
+
# The default template that the machine will take
|
42 |
+
def _get_default_prompt_template(self) -> PromptTemplate:
|
43 |
+
template = """Extract all the following values: Invoice ID, DESCRIPTION, Issue Data,UNIT PRICE, AMOUNT, Bill for, From and Terms for: {pages}
|
44 |
+
|
45 |
+
Expected Outcome: remove any dollar symbols {{"Invoice ID":"12341234", "DESCRIPTION": "UNIT PRICE", "AMOUNT": "3", "Date": "2/1/2021", "AMOUNT": "100", "Bill For": "Dev", "From": "Coca Cola", "Terms" : "Net for 30 days"}}
|
46 |
+
"""
|
47 |
+
|
48 |
+
prompt_template = PromptTemplate(input_variables = ["pages"], template = template)
|
49 |
+
return prompt_template
|
50 |
+
|
51 |
+
|
52 |
+
# We will try to extract the text from the PDF to a normal variable.
|
53 |
+
def _get_raw_text_from_pdf(self, path:str) -> str:
|
54 |
+
text = ""
|
55 |
+
pdf_reader = PdfReader(path)
|
56 |
+
for page in pdf_reader:
|
57 |
+
text += page.extract_text()
|
58 |
+
return text
|
59 |
+
|
60 |
+
def _extract_data_from_llm(self, raw_data:str) -> str:
|
61 |
+
resp = self._llm(self._prompt_template.format(pages = raw_data))
|
62 |
+
return resp
|
63 |
+
|
64 |
+
|
65 |
+
|
66 |
+
|
67 |
+
|