forked from cyclic-software/starter-flask-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
336 lines (255 loc) · 12 KB
/
app.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
from flask import Flask
import os
import tensorflow as tf
import tensorflow_hub as hub
from sklearn.ensemble import RandomForestClassifier
import matplotlib.pyplot as plt
import os
import re
import numpy as np
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.neighbors import NearestNeighbors
model_url = "https://tfhub.dev/google/universal-sentence-encoder/4"
model = hub.load(model_url)
print('Model Loaded')
import pymongo
import pandas as pd
# Connect to MongoDB
client = pymongo.MongoClient("mongodb+srv://ao-uat-ai:[email protected]/ActingOfficeUATAI?retryWrites=true&w=majority")
db = client["ActingOfficeUATAI"]
collection = db["TrainData"]
# Query and retrieve data with specific columns
projection = {
"Description": 1,
"Debit": 1,
"Credit": 1,
"SIC Code": 1,
"A'Heads": 1,
"_id": 0 # Exclude the default "_id" field
}
data = collection.find({}, projection)
# Convert data to a list of dictionaries
data_list = list(data)
# Create a Pandas DataFrame with specific columns
final_df = pd.DataFrame(data_list)
# final_df = pd.read_excel('Corrected EXL.xlsx')
final_df= final_df.fillna(0)
collection = db["SICs"]
projection1 = {
"SIC Code (Defined)": 1,
"_id": 0 # Exclude the default "_id" field
}
data1 = collection.find({}, projection1)
# Convert data to a list of dictionaries
data_list1 = list(data1)
# Create a Pandas DataFrame with specific columns
dfs = pd.DataFrame(data_list1)
# dfs = pd.read_excel("all_sic_codes (1).xlsx")
sample_suggestions = dfs["SIC Code (Defined)"].tolist()
def embed(texts):
return model(texts)
def preprocess_text(text, lowercase=True, remove_special_chars=True, apply_stemming=False, remove_stopwords=False,remove_extra_spaces=True):
if not isinstance(text, str):
return ""
if remove_special_chars:
text = re.sub(r'[^a-zA-Z\s]', '', text)
if lowercase:
text = text.lower()
if remove_extra_spaces:
text = re.sub(r'\s+', ' ', text)
return text
from flask import Flask, redirect, render_template, request, send_file, send_from_directory, jsonify, url_for
from flask_mail import Message, Mail
import pandas as pd
import io
import joblib
from flask import Flask
from flask_mail import Mail
app = Flask(__name__)
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USERNAME'] = '[email protected]' # Replace with your Gmail address
app.config['MAIL_PASSWORD'] = 'oyil vhjs ftvu fugj' # Replace with your Gmail password or an app password
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USE_SSL'] = False
mail = Mail(app)
# def email_sent_callback(message, app):
# def callback(sender, message, **extra):
# app.logger.info(f"Email sent to {', '.join(message.recipients)}")
# return callback
def determine_dc(row):
if row["Debit"] != 0 and row["Credit"] ==0 :
return 1
elif row["Credit"] != 0 and row["Debit"] ==0 :
return -1
else:
return 0
final_df['Description'] = final_df['Description'].apply(preprocess_text)
final_df['SIC Code'] = final_df['SIC Code'].apply(preprocess_text)
final_df["DC"] = final_df.apply(determine_dc, axis=1)
#Model 1
Desc = np.array(final_df['Description'])
Desc_embeddings = embed(Desc)
DC_emb = np.array(final_df['DC']).reshape(-1, 1)
combined_emb = np.concatenate((Desc_embeddings, DC_emb), axis=1)
nn = NearestNeighbors(n_neighbors=50, metric = 'manhattan')
nn.fit(combined_emb)
#Model 2
Desc = np.array(final_df['Description'])
Desc_embeddings = embed(Desc)
Sic = np.array(final_df['SIC Code'])
Sic_embeddings = embed(Sic)
DC_emb = np.array(final_df['DC']).reshape(-1, 1)
combined_emb1 = np.concatenate((Desc_embeddings,Sic_embeddings, DC_emb), axis=1)
nn1 = NearestNeighbors(n_neighbors=50, metric = 'manhattan')
nn1.fit(combined_emb1)
df = None
output_file_name = None
file = None
@app.route('/')
def index():
sic_codes = sorted(sample_suggestions)
return render_template('upload.html',sic_codes=sic_codes)
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
global df
global output_file_name
global file
try:
if 'file' not in request.files:
return "No file part"
file = request.files['file']
if file.filename == '':
return "No selected file"
if file and nn and nn1:
df = pd.read_excel(file)
sic_input = request.form['SIC']
df["sic code"] = sic_input
def determine_dcinput(row):
if row["debit_amount"] != 0 and row["credit_amount"] ==0 :
return 1
elif row["credit_amount"] != 0 and row["debit_amount"] ==0 :
return -1
else:
return 0
df['description'] = df['description'].apply(preprocess_text)
df['debit_amount'] = df['debit_amount'].astype(float)
df['credit_amount'] = df['credit_amount'].astype(float)
df['description'] = df['description'].astype(str)
df['sic code'] = df['sic code'].astype(str)
df["dc"] = df.apply(determine_dcinput, axis=1)
def recommend(desc, dc):
processed_desc = preprocess_text(desc)
desc_emb = embed([processed_desc])
dc_emb = np.array([[dc]])
combined_embinp = np.concatenate((desc_emb, dc_emb), axis=1)
neighbors = nn.kneighbors(combined_embinp, return_distance=False)[0]
df_neighbors = final_df.iloc[neighbors]
neighbor_names = df_neighbors["A'Heads"].tolist()
if "Square Off" in neighbor_names and "refund" not in processed_desc:
neighbor_names.remove("Square Off")
start_index = next((i for i, name in enumerate(neighbor_names) if name != "Sundry Expenses"), None)
if start_index is not None:
neighbor_names = neighbor_names[start_index:]
unique_neighbor_names = set()
updated_neighbor_names = []
for name in neighbor_names:
if name not in unique_neighbor_names:
updated_neighbor_names.append(name)
unique_neighbor_names.add(name)
return updated_neighbor_names
def recommend1(desc,sic, dc):
processed_desc = preprocess_text(desc)
desc_emb = embed([processed_desc])
processed_sic = preprocess_text(sic)
sic_emb = embed([processed_sic])
dc_emb = np.array([[dc]])
combined_embinp = np.concatenate((desc_emb, sic_emb, dc_emb), axis=1)
neighbors = nn1.kneighbors(combined_embinp, return_distance=False)[0]
df_neighbors = final_df.iloc[neighbors]
neighbor_names = df_neighbors["A'Heads"].tolist()
if "Square Off" in neighbor_names and "refund" not in processed_desc:
neighbor_names.remove("Square Off")
start_index = next((i for i, name in enumerate(neighbor_names) if name != "Sundry"), None)
if start_index is not None:
neighbor_names = neighbor_names[start_index:]
unique_neighbor_names1 = set()
updated_neighbor_names1 = []
for name in neighbor_names:
if name not in unique_neighbor_names1:
updated_neighbor_names1.append(name)
unique_neighbor_names1.add(name)
return updated_neighbor_names1
def combine_recommendations(desc, sic, dc):
recommendations1 = recommend(desc, dc)
recommendations2 = recommend1(desc, sic, dc)
pointer1, pointer2 = 0, 0
unique_combined_recommendations = []
while pointer1 < len(recommendations1) or pointer2 < len(recommendations2):
if pointer1 < len(recommendations1):
recommendation1 = recommendations1[pointer1]
if recommendation1 not in unique_combined_recommendations:
unique_combined_recommendations.append(recommendation1)
pointer1 += 1
if pointer2 < len(recommendations2):
recommendation2 = recommendations2[pointer2]
if recommendation2 not in unique_combined_recommendations:
unique_combined_recommendations.append(recommendation2)
pointer2 += 1
return unique_combined_recommendations
df["Predicted Heads"] = df.apply(lambda row: combine_recommendations(row["description"], row["sic code"], row["dc"]), axis=1)
extracted_data = df[['description', 'debit_amount', 'credit_amount', 'sic code','Predicted Heads']]
remaining_ones = final_df["A'Heads"].unique().tolist()
return render_template('correction_form.html', excel_data=df.to_dict('records'),remaining_ones=remaining_ones)
else:
return "Model not found or uploaded file is invalid."
except Exception as e:
return f"An error occurred: {str(e)}"
@app.route('/process_corrections', methods=['POST'])
def process_corrections():
global df
global output_file_name
global file
try:
if 'predicted_heads' not in request.form:
return "No predicted heads data received."
predicted_heads = request.form.getlist('predicted_heads')
others_text = request.form.getlist('others_text')
updated_predicted_heads = []
for i, selection in enumerate(predicted_heads):
if selection == "Others":
# If "Others" is selected, use the manually entered text
updated_predicted_heads.append(others_text[i])
else:
updated_predicted_heads.append(selection)
# Update the 'Predicted Heads' column directly
df['Predicted Heads'] = updated_predicted_heads
input_filename = os.path.splitext(file.filename)[0]
output_file_name = f'Corrected_{input_filename}.xlsx' # New file name
output_file_path = os.path.join('output', output_file_name)
df.to_excel(output_file_path, index=False)
# Send an email with the corrected Excel file attached
msg = Message('Correction Report', sender='[email protected]', recipients=['[email protected]'])
msg.body = 'Here is the copy of the corrected Excel file.'
with app.open_resource(output_file_path) as attachment:
msg.attach(output_file_name, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', attachment.read())
mail.send(msg)
# Check if the data has already been inserted
if not hasattr(process_corrections, 'data_inserted'):
# Read the data from the saved Excel file
collection = db[input_filename]
tested_data = pd.read_excel(output_file_path, usecols=["description", "debit_amount", "credit_amount", "sic code", "Predicted Heads"])
# Insert the data into the 'Tested' collection in MongoDB
collection.insert_many(tested_data.to_dict(orient='records'))
# Set the flag to indicate that data has been inserted
process_corrections.data_inserted = True
return redirect(url_for('download_file', filename=output_file_name))
except Exception as e:
# app.logger.error(f"An error occurred while sending the email: {str(e)}")
return f"An error occurred: {str(e)}"
@app.route('/download/<filename>')
def download_file(filename):
return send_file(os.path.join('output', filename), as_attachment=True)
if __name__ == '__main__':
app.run(debug=True)