-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathflaskMain.py
336 lines (247 loc) · 12.4 KB
/
flaskMain.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,request
import json
import spacy
# initialization
nlp = spacy.load("en_core_web_md") # make sure to use larger model!
# Conversation Samples
CONV_SAMPLES = [['greetings'], ['destitute'], ['marriage', 'husband', 'wife', 'spouse', 'divorce'], ['depressed', 'sad', 'lonely', 'melancholic', 'need care', 'anxious', 'insomnia', 'bad sleep', 'guilt']]
initialTest = 2
depressedTest = 0
strength = 0
# Stores the analysis
analysis = [[], [], [], [], [], [], [], [], [], [], [], [], []]
# Output control for the web
outputToWeb = {"code": 0, "value": "initial", "response": "Hello I am Invictus.\nLet us start off by asking, is the consulting person from a poor background?"}
# This is to analyse the sentences given
def get_response(u_query):
mostSimilarity = 0
mostSimilarityValue = 0
global outputToWeb
global initialTest
global depressedTest
global strength
print("Strength", strength)
# Analysing the input
for i in range (len (CONV_SAMPLES)):
for j in range( len(CONV_SAMPLES[i])):
if (nlp(u_query).similarity(nlp(CONV_SAMPLES[i][j])) > mostSimilarityValue):
mostSimilarity = i
mostSimilarityValue = nlp(u_query).similarity(nlp(CONV_SAMPLES[i][j]))
# Print current analysis
print(analysis)
# Check the initial condition for bystander
if (initialTest == 0):
initialTest = 1
# Is a bystander
if (nlp(u_query).similarity(nlp("Yes")) > nlp(u_query).similarity(nlp("No"))):
# The output value
outputToWeb = {"code": 0, "value": "initial", "response": "Is the person being consulted a destitute?"}
print(outputToWeb)
print("RESPONSE:\nIs the person being consulted a destitute?")
# Return the output
return outputToWeb
# Not a bystander
else:
# Initialization
initialTest = 2
outputToWeb = {"code": 0, "value": "message", "response": "How are you feeling?"}
# Returned value
return outputToWeb
# To check for the codition to send an NGO to the location
elif (initialTest == 1):
initialTest = 2
# Send NGO to the location
if (nlp(u_query).similarity(nlp("Yes")) > nlp(u_query).similarity(nlp("No"))):
# The value is formatted
outputToWeb = {"code": 1, "value": "NGO-Yes", "response": "A volunteer will be sent to your location."}
analysis[1].append("NGO")
analysis[1].append("Yes")
# NGO has not been assigned
else:
# The person is not a destitute
outputToWeb = {"code": 2, "value": "NGO-No", "response": "How are you feeling now?"}
analysis[1].append("NGO")
analysis[1].append("No")
# Return the output format value
return outputToWeb
# Destitute checking
elif CONV_SAMPLES[mostSimilarity][0] == 'destitute':
# We rerun the program and consider the situation of a less privilaged person
outputToWeb = {"code": 0, "value": "initial", "response": "Is the consulting person from a poor background?"}
print("RESULT:\nis the consulting person from a poor background?")
initialTest = 1
# Return the value
return outputToWeb
if CONV_SAMPLES[mostSimilarity][0] == 'depressed' or depressedTest > 0:
if (depressedTest == 0):
depressedTest = 1
# Check the sleep schedule
outputToWeb = {"code": 0, "value": "Sleep", "response": "How is your sleep schedule?"}
print(outputToWeb)
print("RESPONSE:\nHow is your sleep schedule?")
# Return the required value
return outputToWeb
elif (depressedTest == 1):
depressedTest = 2
if (nlp(u_query).similarity(nlp("good")) + nlp(u_query).similarity(nlp("long")) + nlp(u_query).similarity(nlp("yes"))> nlp(u_query).similarity(nlp("bad")) + nlp(u_query).similarity(nlp("short")) + nlp(u_query).similarity(nlp("no"))):
outputToWeb = {"code": 0, "value": "Sleep-Yes", "response": "How is your appetite?"}
analysis[2].append("Sleep")
analysis[2].append("Good")
# Person might suffer from diseases pertaining to lack of sleep
else:
outputToWeb = {"code": 0, "value": "Sleep-No", "response": "How is your appetite?"}
analysis[2].append("Sleep")
analysis[2].append("Bad")
# Formatting the output
print(analysis)
print(outputToWeb)
print("RESPONSE:\nHow is your appetite?")
# Returning the value that is required
return outputToWeb
elif (depressedTest == 2):
depressedTest = 3
# Person has a good diet
if (nlp(u_query).similarity(nlp("good")) + nlp(u_query).similarity(nlp("yes")) > nlp(u_query).similarity(nlp("bad")) + nlp(u_query).similarity(nlp("no"))):
# Analyzing the output
outputToWeb = {"code": 0, "value": "Diet-Yes", "response": "Do you know today's date or remember your neighbour's name?"}
analysis[3].append("Diet")
analysis[3].append("Good")
# Person may suffer from diseases that cause lose of appetite
else:
# Analyzing the output
outputToWeb = {"code": 0, "value": "Diet-No", "response": "Do you know today's date or remember your neighbour's name?"}
analysis[3].append("Diet")
analysis[3].append("Bad")
# Analyzing th strength of situation
if ("Bad" in analysis[2]):
strength = strength + 1
if ("Bad" in analysis[3]):
strength = strength + 1
print(analysis)
# Checking for the possibility of depression
print(outputToWeb)
# return value to get the output
return outputToWeb
# For a sudden weight change
elif (depressedTest == 3):
depressedTest = 4
# Sudden weight change
if (nlp(u_query).similarity(nlp("good")) + nlp(u_query).similarity(nlp("yes")) > nlp(u_query).similarity(nlp("bad")) + nlp(u_query).similarity(nlp("no"))):
# Analyzing the output
outputToWeb = {"code": 0, "value": "Weight-Change-Yes", "response": "Do you ever think of ending your life?"}
analysis[5].append("Weight-change")
analysis[5].append("Good")
# No sudden wight change
else:
# Analyzing the output
outputToWeb = {"code": 0, "value": "Weight-Change-No", "response": "Do you ever think of ending your life?"}
analysis[5].append("Weight-change")
analysis[5].append("Bad")
# Return the output needed
return outputToWeb
elif (depressedTest == 4):
depressedTest = 5
# Analyzing the suicide case
if (nlp(u_query).similarity(nlp("good")) > nlp(u_query).similarity(nlp("bad")) or nlp(u_query).similarity(nlp("yes")) > nlp(u_query).similarity(nlp("no"))):
strength = strength + 1
analysis[6].append("suicidal")
analysis[6].append("yes")
outputToWeb = {"code": 5, "value": "suicide", "response": "We will now redirect you to the human councellor"}
print("RESULT:\nWe will now redirect you to the human councellor")
return outputToWeb
# Case with no suicide case
else:
strength = strength + 1
analysis[6].append("suicidal")
analysis[6].append("no")
# Analyse the attention span of the person
outputToWeb = {"code": 0, "value": "initial", "response": "Do you ever see, hear, smell, feel, or taste things that are not really there?"}
print(outputToWeb)
# Return the value
return outputToWeb
elif (depressedTest == 5):
print("HAHAHA")
depressedTest = 6
# Case with low attention span
print(strength)
if (nlp(u_query).similarity(nlp("no")) > 0.6):
strength = strength + 1
# Finding the possibility of depression
if (strength >= 3):
# Analyze the output
analysis[7].append("depression")
analysis[7].append("yes")
# Get the return value
outputToWeb = {"code": 6, "value": "depression", "response": "We suggest that you to attend group therapy"}
print("RESULT:\nWe suggest that you to attend group therapy")
# Return the value
return outputToWeb
elif CONV_SAMPLES[mostSimilarity][0] == 'greetings':
# This is used to check greetings.
outputToWeb = {"code": 0, "value": "greeting", "response": "Hello, nice to meet you.\nI am Invictus.\nI am here to help"}
print("RESULT:\n\nHello, nice to meet you.\nI am Invictus.\nI am here to help\n")
return outputToWeb
# Check for marriage related problems
elif CONV_SAMPLES[mostSimilarity][0] == 'marriage':
analysis[4].append("Marriage")
# Gives the various connection to marriage related keywords
print("Mariage report:")
print("abuse", nlp(u_query).similarity(nlp("abuse")))
print("beat", nlp(u_query).similarity(nlp("beat")))
print("cheat", nlp(u_query).similarity(nlp("cheat")))
print("rape", nlp(u_query).similarity(nlp("rape")))
# Categorizing the marriage problems
if (nlp(u_query).similarity(nlp("abuse")) > 0.5 or nlp(u_query).similarity(nlp("beat")) > 0.5):
analysis[4].append("abuse")
if (nlp(u_query).similarity(nlp("rape")) > 0.5):
analysis[4].append("rape")
if (nlp(u_query).similarity(nlp("cheat")) > 0.5 or nlp(u_query).similarity(nlp("affair")) > 0.5 or nlp(u_query).similarity(nlp("extramarital")) > 0.5):
analysis[4].append("affair")
if (nlp(u_query).similarity(nlp("physical")) > 0.5 or nlp(u_query).similarity(nlp("romance")) > 0.5):
analysis[4].append("romance")
# Finding the intensity of the problem
strength = 0
if ("romance" in analysis[4] or "affair" in analysis[4]):
strength = strength + 1
if ("abuse" in analysis[4] or "rape" in analysis[4]):
strength = strength + 3
# A severe case that needs attention
if (strength > 2):
outputToWeb = {"code": 4, "value": "marriage", "response": "I see that you are facing extreme issues at your home.\nYou will be given a priority in our cuncellor queue."}
print("RESULT:\n\nI see that you are facing extreme issues at your home.\nYou will be given a priority in our cuncellor queue.")
# Return data
return outputToWeb
# A mild case that needs to checked
elif (strength > 0):
outputToWeb = {"code": 3, "value": "marriage", "response": "I hear your problems.\nI would suggest that you book a session with our councellor"}
print("RESULT:\n\nI hear your problems.\nI would suggest that you book a session with our councellor")
# Return data
return outputToWeb
return {"code": 999, "value": "Suggestion", "response": "We suggest that you listen to our prepared music.\nWe also suggest some exercises for you.\nIt will calm your mind."}
app = Flask(__name__)
@app.route('/')
def index():
return "Flask server"
@app.route('/postdata', methods = ['POST'])
def postdata():
data = request.get_data()
data = data.decode("utf-8")
print(type(data))
print(data)
print(data[5:])
data = data[5:]
newData = ""
word = ""
for i in range(len(data)):
if (data[i].isalpha()):
word = word + data[i]
else:
newData = newData + " " +word
word = ""
newData = newData + " " +word
print(newData)
OutputWeb = get_response(newData)
print(OutputWeb)
return OutputWeb
if __name__ == "__main__":
app.run(port=5000)