-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.py
executable file
·179 lines (142 loc) · 5.44 KB
/
backend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from api import (
Summarizer,
LLMUtils,
DuplicateChecker,
DocumentsProcessor,
TabularDataExtractor,
)
from llama_index import set_global_tokenizer, set_global_service_context
from transformers import AutoTokenizer
from flask import Flask, request, jsonify
import shutil
import os
import pandas as pd
app = Flask(__name__)
llm_module = LLMUtils()
set_global_tokenizer(
AutoTokenizer.from_pretrained("macadeliccc/laser-dolphin-mixtral-2x7b-dpo").encode
)
set_global_service_context(service_context=llm_module.service_context)
temp_filepath = "./temp/temp.pdf"
support_directory = "./temp/support/"
temp_zip_filepath = "./temp/uploaded_files/files.zip"
extracted_data_dir = "./temp/extracted_data"
extracted_data_filepath = "./temp/extracted_data"
invoice_csv_filepath = "./temp/invoice.csv"
files_dir = "./v2"
summarized_pdf_csv_filepath = "./v2/file.csv"
extracted_data_dir_v2 = "./v2/extracted_data"
uploaded_filepath = "./v2/file.pdf"
db_dir = "./v2/db_dir"
for dir in [files_dir, extracted_data_dir_v2, db_dir]:
if not os.path.exists(dir):
os.mkdir(dir)
@app.route("/", methods=["GET"])
def index():
return """<!DOCTYPE html>
<html>
<body>
<a href='http://127.0.0.1:5000/streamlit'>Go to prediction app</a>
</body>
</html>"""
@app.route("/summarize_invoice", methods=["GET"])
def summarize_invoice():
uploaded_files_directory = os.path.dirname("./temp/uploaded_files/files.zip")
filenames = os.listdir(uploaded_files_directory)
if len(filenames) == 0:
return jsonify({"error": "No documents uploaded!"})
response = ""
for filename in filenames:
if filename.endswith("pdf"):
summarizer = Summarizer(
filepath=os.path.join(uploaded_files_directory, filename)
)
response += (
f"\n### Here is the invoice summary for {filename}:\n"
+ summarizer.summarize()
)
if response == "":
return jsonify({"error": "Uploaded files contain no Invoice pdfs"})
return jsonify({"response": response})
@app.route("/process_documents", methods=["POST"])
def process_docs():
shutil.rmtree(os.path.dirname(temp_zip_filepath))
os.mkdir(os.path.dirname(temp_zip_filepath))
shutil.rmtree(extracted_data_dir)
os.mkdir(extracted_data_dir)
if os.path.exists(extracted_data_dir + ".zip"):
os.remove(extracted_data_dir + ".zip")
if "files" not in request.files:
return jsonify({"error": "No zip file uploaded!"})
zip_fs = request.files["files"]
zip_fs.save(temp_zip_filepath)
doc_processor = DocumentsProcessor(
zip_filepath=temp_zip_filepath, data_dir=extracted_data_dir
)
# save extracted data from the document processor
ret = doc_processor.save_extracted_data(extracted_data_filepath)
if ret:
return jsonify(
{
"response": "Data extracted successfully",
"output_filepath": extracted_data_filepath + ".zip",
}
)
@app.route("/invoice/summarize_invoice", methods=["POST"])
def summarize_invoice_pdf():
if "files" not in request.files:
return jsonify({"error": "No pdf uploaded!"})
invoice_fs = request.files["invoice"]
invoice_fs.save(temp_filepath)
summarizer = Summarizer(filepath=temp_filepath)
csv_text = summarizer.summarize()
with open(invoice_csv_filepath, "w+") as f:
f.write(csv_text)
return jsonify({"output_filepath": invoice_csv_filepath})
# Health Check API
@app.route("/v2/health", methods=["GET"])
def health_check():
return jsonify({"status": "healthy"})
@app.route("/v2/summarize_data", methods=["POST"])
def summarize_document():
request_data = request.get_json()
filetype = request_data["filetype"]
summarizer = Summarizer(filepath=uploaded_filepath, filetype=filetype)
csv_text = summarizer.summarize()
with open(summarized_pdf_csv_filepath, "w+") as f:
f.write(csv_text)
return jsonify({"output_filepath": summarized_pdf_csv_filepath})
@app.route("/v2/extract_data", methods=["POST"])
def extract_data():
if "files" not in request.files:
return jsonify({"error": "No pdf uploaded!"})
document_fs = request.files["files"]
document_fs.save(uploaded_filepath)
extractor = TabularDataExtractor(filepath=uploaded_filepath)
output_filepath = extractor.extract_and_save_data()
return jsonify({"output_filepath": output_filepath})
@app.route("/check_duplicates", methods=["GET"])
def check_duplicates():
uploaded_files_directory = os.path.dirname("./temp/uploaded_files/files.zip")
filepaths = os.listdir(uploaded_files_directory)
filepaths = [
os.path.join(uploaded_files_directory, filepath) for filepath in filepaths
]
if len(filepaths) == 0:
return jsonify({"error": "No documents uploaded!"})
duplicate_checker = DuplicateChecker(
files=filepaths,
unique_col="SAP ID",
)
ret, sap_ids = duplicate_checker.check_duplicates()
sap_ids_df = pd.DataFrame({"SAP ID": sap_ids})
duplicates_filepath = "./temp/duplicates_dir/duplicates.csv"
sap_ids_df.to_csv(duplicates_filepath, index=False)
if ret:
response = "### Annexure contains duplicate records!"
return jsonify({"response": response, "output_filepath": duplicates_filepath})
else:
response = "### Annexure Contains No Duplicates!"
return jsonify({"response": response})
if __name__ == "__main__":
app.run(debug=True)