-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
393 lines (313 loc) · 17.9 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
"""
@Author: Mamunur Rahman
"""
from flask import Flask, render_template, request
import pickle
import numpy as np
import pandas as pd
import difflib
from sklearn.metrics.pairwise import cosine_similarity
import random
from nltk.stem import WordNetLemmatizer
import base64
import datetime
from datetime import timedelta
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/input_wart')
def input_wart():
return render_template('input_wart.html')
@app.route('/input_breast_cancer')
def input_breast_cancer():
return render_template('input_breast_cancer.html')
@app.route('/input_movie_recommender')
def input_movie_recommender():
return render_template('input_movie_recommender.html')
@app.route('/more_about_apps')
def more_about_apps():
return render_template('more_about_apps.html')
@app.route('/about_me')
def about_me():
return render_template('about_me.html')
#-------------------------------------------------------------------------------------------------------
@app.route('/result_wart', methods = ['POST'])
def result_wart():
try:
user_input = request.form
time = float(user_input['time'])
age = float(user_input['age'])
type_ = int(user_input['type'])
area = float(user_input['area'])
## check the input values whether is it within the recommended range
if age<15 or age>100 or time<0 or time>24 or area<1 or area>1000:
output_text = "Please insert values within the recommended range, \
otherwise the prediction wouldn't be reliable"
return render_template("input_error_wart.html", user_input = user_input, result = output_text)
else:
## Cryotherapy
new_data_cryo = np.array([time, age, type_, area]).reshape(1, -1)
# load the saved scaler and classifier models
scaler_cryo = pickle.load(open('scaler_crytotherapy.pkl','rb'))
model_cryo = pickle.load(open('model_crytotherapy.pkl','rb'))
# scale the new data
new_data_cryo = scaler_cryo.transform(new_data_cryo)
# estimate the probability of treatment success
probability_cryo = model_cryo.predict_proba(new_data_cryo)[:,1] # take the second column of the probability matrix
# get the probability in % and round it up to one decimal points
probability_cryo = round(probability_cryo[0]*100, 1)
## Immunotherapy
new_data_immuno = np.array([time, age, type_]).reshape(1, -1)
# load the saved scaler and classifier models
scaler_immuno = pickle.load(open('scaler_immunotherapy.pkl','rb'))
model_immuno = pickle.load(open('model_immunotherapy.pkl','rb'))
# scale the new data
new_data_immuno = scaler_immuno.transform(new_data_immuno)
# estimate the probability of treatment success
probability_immuno = model_immuno.predict_proba(new_data_immuno)[:,1] # take the second column of the probability matrix
# get the probability in % and round it up to one decimal points
probability_immuno = round(probability_immuno[0]*100, 1)
## output text
if probability_cryo <= 50 and probability_immuno <= 50:
output_text = f"Neither Cryotherapy nor Immunotherapy treatment method will work \
for this patient. The probabilities of treatment success are only \
{probability_cryo}% for Cryotherapy and only {probability_immuno}% \
for Immunotherapy. Other treatment methods are recommended."
elif probability_cryo > probability_immuno:
output_text = f"Cryotherapy is preferred to Immunotherapy for this patient. \
The probabilities of treatment success are {probability_cryo}% for \
Cryotherapy and {probability_immuno}% for Immunotherapy."
elif probability_cryo < probability_immuno:
output_text = f"Immunotherapy is preferred to Cryotherapy for this patient. \
The probabilities of treatment success are {probability_immuno}% for \
Immunotherapy and {probability_cryo}% for Cryotherapy."
else:
output_text = f"Both treatment methods are equally likely to work on this patient. \
The probability of treatment success is approximately {probability_cryo}%"
return render_template("result_wart.html", user_input = user_input, result = output_text)
## the user input is not numeric
except:
user_input = request.form
output_text = "Please insert only numeric values"
return render_template("input_error_wart.html", user_input = user_input, result = output_text)
#---------------------------------------------------------------------------------------------------
@app.route('/result_breast_cancer', methods = ['POST'])
def result_breast_cancer():
try:
user_input = request.form
age = float(user_input['age'])
bmi = float(user_input['bmi'])
glucose = float(user_input['glucose'])
insulin = float(user_input['insulin'])
resistin = float(user_input['resistin'])
mcp_1 = float(user_input['mcp_1'])
## check the input values whether it is within the recommended range
if age<15 or age>100 or bmi<15 or bmi>40 or glucose<60 or glucose>250 or insulin<2 or insulin>60 or resistin<3 or resistin>80 or mcp_1<40 or mcp_1>1700:
output_text = "Please insert values within the recommended range, \
otherwise the prediction wouldn't be reliable"
return render_template("input_error_breast_cancer.html", user_input = user_input, result = output_text)
else:
new_data = np.array([age, bmi, glucose, insulin, resistin, mcp_1]).reshape(1, -1)
# load the saved scaler and classifier models
scaler = pickle.load(open('scaler_breast_cancer.pkl','rb'))
model = pickle.load(open('model_breast_cancer.pkl','rb'))
# scale the new data
new_data = scaler.transform(new_data)
# estimate the probability of treatment success
probability = model.predict_proba(new_data)[:,1] # take the second column of the probability matrix
# get the probability in % and round it up to one decimal points
probability = round(probability[0]*100, 1)
## output text
if probability < 10:
output_text = "Great!!! The probability of having breast cancer for this \
person is very low, less than 10%."
elif probability < 30:
output_text = f"The probability of having breast cancer for this person \
is low, approximately {probability}%."
elif probability < 40:
output_text = f"The probability of having breast cancer for this person \
is approximately {probability}%."
elif probability < 60:
output_text = f"The probability of having breast cancer for this person \
is approximately {probability}%. You might want to \
discuss with your doctor for further investigation."
elif probability <90:
output_text = f"The probability of having breast cancer for this person \
is approximately {probability}%. Please discuss with \
your doctor for further investigation."
else:
output_text = "The probability of having breast cancer for this person is \
extremely high, more than 90%. Please discuss with your \
doctor immediately for further investigation."
return render_template("result_breast_cancer.html", user_input = user_input, result = output_text)
## the user input is not numeric
except:
user_input = request.form
output_text = "Please insert only numeric values"
return render_template("input_error_breast_cancer.html", user_input = user_input, result = output_text)
#----------------------------------------------------------------------------------------------------
@app.route('/result_movie_recommender', methods = ['POST'])
def result_movie_recommender():
original_user_input = request.form['movie_name']
# define a function which will recommend best 'n' movies for a specific user
def recommend_movie(original_user_input, n=7):
user_input = original_user_input.strip()
user_input = user_input.lower().replace(' ', '').replace(':', '').replace('\'', '').replace('-', '')
# get index of the movie from the movie_list
movie_list = pickle.load(open('movie_list.pkl','rb'))
idx = movie_list.index(user_input)
# unpickle scaled data
scaled_data = pickle.load(open('scaled_data_movie_recommender.pkl','rb'))
# calculate cosine similarity for the user input movie
similarity_matrix = cosine_similarity(scaled_data, scaled_data[idx].reshape(1, -1))
# get the index of the top 10 movies similar to user_input
# the index 0 contains the user input movie. So, we start the index from 1
idx = list(np.argsort(-similarity_matrix.flatten())[1:15])
# reload df_name_and_weighted_rank
df_name_and_weighted_rank = pickle.load(open('df_name_and_weighted_rank_movie_recommender.pkl','rb'))
df_top_10_by_type = df_name_and_weighted_rank.loc[idx]
# sort the list by weighted rank and show the first 5 movies
result = df_top_10_by_type.sort_values(by=['weighted_rank'], ascending=False)['movie_title'][:n].tolist()
return result
try:
try:
recommendations = recommend_movie(original_user_input)
output_text = f"Since {original_user_input} is your favorite, you might also like: \n "
except:
movie_list = pickle.load(open('movie_list.pkl','rb'))
close_match = difflib.get_close_matches(original_user_input, movie_list, n=1)[0]
user_input = close_match
idx = movie_list.index(user_input)
df_name_and_weighted_rank = pickle.load(open('df_name_and_weighted_rank_movie_recommender.pkl','rb'))
movie_title = df_name_and_weighted_rank.loc[idx, 'movie_title']
output_text = f"Did you mean- {movie_title.strip()}? \nIf yes, our recommendations are:\n"
recommendations = recommend_movie(close_match)
except:
output_text = "Sorry!!! The movie is not in our database. Please try another movie or keyword."
recommendations = []
return render_template("result_movie_recommender.html", result = output_text, recommended_movies = recommendations)
#------------------------------------------------------------------------------------------------------
## ChatBot
# unpickle the necessary data and variables
with open("data_chatbot.pickle", "rb") as f:
remove_punct_dict, remove_word_dict, scaled_data_chatbot, df_user_input, df_response, vectorizer = pickle.load(f)
# clean and preprocess the target column of the dataframe
def process_sentence(sentence, dictionary):
#remove punctuations
sentence = sentence.translate(remove_punct_dict)
new_sentence = ''
for word in sentence.split():
# make the letters lowercase
word = word.lower()
try:
# replace word if it is in remove_word_dict, do it twice
word = dictionary[word]
word = dictionary[word]
except:
pass
# lemmatize the word
try: #In Heroku, if nltk package is not not properly installed
lemmatizer = WordNetLemmatizer()
word = lemmatizer.lemmatize(word)
except:
pass
new_sentence = new_sentence + word + ' '
return new_sentence.strip() # remove leading and trailing space of the sentence
# define response where chat history data will be stored for all the users
response= {}
# email the chat history in every 24 hours
from send_email import send_email
# define a function which will process a dictionary to line by line sentence
def format_chat_history(history_dict):
message = ""
for key in list(history_dict.keys()):
message = message + key + '\n'
for sentence in history_dict[key]:
message = message + sentence + '\n'
return message
last_email_time = datetime.datetime(2019,12,9,22,0)
time_difference = datetime.datetime.now() - last_email_time
time_difference_in_minutes = time_difference / timedelta(minutes=1)
if time_difference_in_minutes >= 24*60:
# send email
subject = "ChatBot communications"
email_body = format_chat_history(response)
try:
send_email(subject, email_body, base64.b64decode('xxxxxxxxx').decode("utf-8"))
last_email_time = datetime.datetime.now()
except:
pass
@app.route('/input_chatbot')
def input_chatbot():
user_key = request.remote_addr
global response
response[user_key]= ['Komola: Welcome!! What name should I call you??']
return render_template('input_chatbot.html', result = response[user_key])
# define a function which will keep the last 'n' elements of a dictionary, remove rest of the keys
def keep_n_key_in_dict(x, n):
while len(x) > n:
x.pop(list(x.keys())[0])
# keep 20 user chat history in response dictionary
keep_n_key_in_dict(response, 20)
@app.route('/result_chatbot', methods = ['POST'])
def result_chatbot():
# get the user IP address
user_key = request.remote_addr
if request.form['user_input'] not in ['quit', 'stop', 'exit', 'end']:
def generate_response(original_user_input):
processed_user_input= []
processed_user_input.append(process_sentence(original_user_input, remove_word_dict))
user_input_count_vectorizer = vectorizer.transform(processed_user_input).toarray()
# calculate cosine similarity for the user input movie
similarity_matrix = cosine_similarity(scaled_data_chatbot, user_input_count_vectorizer)
# find the maximum value of the similarity matrix
max_value = max(similarity_matrix.flatten())
if max_value < 0.2:
return ["I apolozize!! I don't understand.", "dont_understand"]
else:
# get the index of the maximum cosine_similarity
idx = np.argsort(similarity_matrix.flatten())[-1]
# get the 'intent' from the df_user_input dataframe
intent = df_user_input.loc[idx, 'intent']
# get a random response
chatbot_response = df_response['chatbot_response'][df_response['intent']==intent].tolist()
random_response = random.choice(chatbot_response)
return random_response, intent
if request.form['user_input'] not in ['small_pizza', 'medium_pizza', 'large_pizza']:
user_input = request.form['user_input']
response[user_key].append(f"You: {user_input}")
# by default, place_order is set as False
place_order = False
# if customer want to place order, take him to order page
if generate_response(user_input)[1] == "place_order":
place_order = True
# At the beginning, in reply to customers name, bot will generate the below response
if response[user_key][-2] == 'Komola: Welcome!! What name should I call you??':
response[user_key].append("Komola: Okay, Thanks!!!")
else:
response[user_key].append(f"Komola: {generate_response(user_input)[0]}")
return render_template("result_chatbot.html", result = response[user_key], show_order_page = place_order)
else: # when the user submit the place order button, this section is executed
response[user_key].append("Our system is processing your order..................")
response[user_key].append(f"Komola: Great!!! Your order has been placed. Your order ID is {random.randint(100, 999)}. Is there anything else I can help you with? ")
return render_template("result_chatbot.html", result = response[user_key], show_order_page = False)
else: # if the user quit the chat
# email the chat history and render the homepage
# define a function which will process a dictionary to line by line sentence
def format_chat_history(history_dict):
message = ""
for key in list(history_dict.keys()):
message = message + key + '\n'
for sentence in history_dict[key]:
message = message + sentence + '\n'
return message
subject = "ChatBot communications"
email_body = format_chat_history(response)
try:
send_email(subject, email_body, base64.b64decode('xxxxxxxxxxxxx').decode("utf-8"))
except:
pass
return render_template("home.html")
if __name__ == '__main__':
app.run()