-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
172 lines (136 loc) · 6.08 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
from flask import Flask, request, jsonify, send_from_directory, render_template,Response
import pickle
import nltk
import os
import json
import re
import nltk
import string
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
# Download required NLTK data
nltk.download('punkt')
nltk.download('stopwords')
app = Flask(__name__, template_folder=os.path.join(os.path.dirname(__file__), 'templates'))
# Load the trained model
with open('logistic_regression_model.pkl', 'rb') as f:
model = pickle.load(f)
with open('count_vectorizer.pickle', 'rb') as f:
count_vect = pickle.load(f)
# Create the count vectorizer and TF-IDF transformer
with open('tfidf_transformer.pickle', 'rb') as f:
tfidf_transformer = pickle.load(f)
# def preprocess_complaint(complaint):
# if isinstance(complaint, str):
# complaint = complaint.lower() # to lowercase
# complaint = re.sub('\[.*\]', '', complaint).strip() # Remove text in square brackets
# complaint = complaint.translate(str.maketrans('', '', string.punctuation)) # Remove punctuation
# complaint = re.sub('\S*clean_text\d\S*\s*', '', complaint).strip() # Remove words containing numbers
# stop_words = set(stopwords.words('english'))
# tokens = word_tokenize(complaint)
# filtered_tokens = [word for word in tokens if word not in stop_words]
# return ' '.join(filtered_tokens)
# else:
# return ''
# stop_words = set(stopwords.words('english'))
def preprocess_complaint(complaint):
# Convert the complaint to lowercase and tokenize it
complaint = complaint.lower()
complaint = re.sub('\[.*\]', '', complaint).strip()
complaint = complaint.translate(str.maketrans('', '', string.punctuation))
complaint = re.sub('\S*clean_text\d\S*\s*', '', complaint).strip()
tokens = word_tokenize(complaint)
stop_words = set(stopwords.words('english'))
filtered_tokens = [word for word in tokens if word not in stop_words]
return ' '.join(filtered_tokens)
topic_mapping = {
0: 'bank',
1: 'credit',
2: 'other',
3: 'theft',
4: 'loan'
}
classified_complaints = []
last_sent_complaint_id = None
@app.route('/stream')
def stream():
def event_stream():
for complaint in classified_complaints:
yield 'data: {}\n\n'.format(json.dumps(complaint))
return Response(event_stream(), mimetype="text/event-stream")
@app.route('/submit-complaint', methods=['POST'])
def predict():
if request.method == 'POST':
# Get the complaint text from the form
# complaint = request.form.get('complaint')
complaint = request.json['complaint']
# Preprocess the complaint text
preprocessed_complaint = preprocess_complaint(complaint)
# Vectorize and TF-IDF transform
test = count_vect.transform([preprocessed_complaint])
test_tfidf = tfidf_transformer.transform(test)
# Make a prediction
prediction = model.predict(test_tfidf)
print(prediction)
department = topic_mapping[prediction[0]]
#print(predicted_topic)
# if(predicted_topic=="Theft/Dispute Reporting")
# {
# predticted_topic=""
# }
# Render the prediction result templat
new_complaint_id = len(classified_complaints) # Use list length for simplicity
classified_complaint = {'id': new_complaint_id, 'text': complaint, 'department': department}
classified_complaints.append(classified_complaint)
if(department=='loan'):
refresh_loan_page=True
elif(department=='credit'):
refresh_credit_page=True
elif(department=='bank'):
refresh_bank_page=True
elif(department=='other'):
refresh_other_page=True
elif(department=='theft'):
refresh_theft_page=True
if department == 'loan':
return jsonify({'success': True, 'refresh_loan_page': refresh_loan_page})
elif department == 'credit':
return jsonify({'success': True,'refresh_credit_page': refresh_credit_page})
elif department == 'theft':
return jsonify({'success': True,'refresh_theft_page': refresh_theft_page})
elif department == 'bank':
return jsonify({'success': True,'refresh_bank_page': refresh_bank_page})
elif department == 'other':
return jsonify({'success': True,'refresh_other_page': refresh_other_page})
else:
return jsonify({'success': True})
@app.route('/')
def index():
return render_template('predict.html')
@app.route('/department/loan')
def loan_department():
loan_complaints = [complaint for complaint in classified_complaints if complaint['department'] == 'loan']
return render_template('Loan.html', complaints=loan_complaints)
@app.route('/department/credit_card')
def credit_card_department():
credit_card_complaints = [complaint for complaint in classified_complaints if complaint['department'] == 'credit']
return render_template('credit_card.html', complaints=credit_card_complaints)
@app.route('/department/other')
def other_department():
other_complaints = [complaint for complaint in classified_complaints if complaint['department'] == 'other']
return render_template('other.html', complaints=other_complaints)
@app.route('/department/bank')
def bank_account_department():
bank_account_complaints = [complaint for complaint in classified_complaints if complaint['department'] == 'bank']
return render_template('Bank.html', complaints=bank_account_complaints)
@app.route('/department/theft')
def theft_department():
theft_complaints = [complaint for complaint in classified_complaints if complaint['department'] == 'theft']
return render_template('theft.html', complaints=theft_complaints)
@app.route('/static/<path:path>')
def serve_static(path):
return send_from_directory('static', path)
if __name__ == '__main__':
app.run(debug=True, use_reloader=False)