-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
325 lines (243 loc) · 10.5 KB
/
server.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
from gevent import monkey
monkey.patch_all()
from flask import Flask, render_template, session, request
from flask_socketio import SocketIO, emit, join_room
from flask_session import Session
from difflib import SequenceMatcher
from pathlib import Path
from datetime import date,datetime
import json
'''
@author Salman Rakin
'''
count=0
stack=set()
fall_query=""
fall_response=""
async_mode = None
app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'nuttertools'
Session(app)
socketio = SocketIO(app,manage_session=False, async_mode=async_mode)
users_connected=0
#users_active=0
#CLIENT_ACCESS_TOKEN = '9624c80a372f44aeaf364eab4cc182b0'
def similar(a, b):
return SequenceMatcher(None, a, b).ratio()
def stat():
global users_connected
print("Number of User Connected to our Server:",users_connected)
print("Number of User Actively Chatting:", len(stack))
def detect_intent_texts(project_id, session_id, texts, language_code):
"""Returns the result of detect intent with texts as inputs.
Using the same `session_id` between requests allows continuation
of the conversation."""
from google.cloud import dialogflow
session_client = dialogflow.SessionsClient()
session = session_client.session_path(project_id, session_id)
# print("Session path: {}\n".format(session))
for text in texts:
text_input = dialogflow.TextInput(text=text, language_code=language_code)
query_input = dialogflow.QueryInput(text=text_input)
response = session_client.detect_intent(
request={"session": session, "query_input": query_input}
)
# print(response.query_result.fulfillment_text)
# print( type(response.query_result.fulfillment_text))
return response.query_result.fulfillment_text
@app.route('/')
def chat():
return render_template('login.html')
@app.route('/chatting')
def login():
return render_template('chat.html')
@socketio.on('stats',namespace='/chat')
def show_stats():
emit('server_stats',{'User_Connected': users_connected, 'User_Active': len(stack)},broadcast=False)
@socketio.on('message', namespace='/chat')
def chat_message(message):
print("message =!! ", message)
test_message=message
emit('message', {'data': test_message['data']}, broadcast=False)
@socketio.on('my_conn', namespace='/chat')
def chat_message(data):
username= data['data']['username']
mobile_number=data['data']['mobile_number']
session= data['data']['session']
date= data['data']['full']
time= data['data']['time']
print(" A user named " +username+" with mobile number "+str(mobile_number)+" and session id "+str(session)+" has connceted to the server at "+date+" "+time )
@socketio.on('discon', namespace='/chat')
def chat_message():
global users_connected
print('A user disconnected for reloading...')
users_connected = users_connected - 1
# users_active=users_active-1
if stack:
stack.pop()
stat()
@socketio.on('auth', namespace='/chat')
def chat_message(data):
username=data['data']
#print("username ---",username)
print("Authentication blcok in the server")
username='admin'
password='admin'
emit('auth',{'username':username,'password':password},broadcast=False)
@socketio.on('transcript', namespace='/chat')
def chat_message(data):
username=data['data']['username']
username=str(username).strip()
mobile_no=data['data']['mobile_number']
mobile_no=str(mobile_no).strip()
#print("mobile_no ---",mobile_no.encode('utf-8'))
session= data['data']['session']
session=str(session).strip()
dir_name=data['data']['full']
dir_name=str(dir_name).strip()
print("username ---",username.encode('utf-8'))
print("mobile_no ---",mobile_no.encode('utf-8'))
print("session ----",session)
print("Dir_name ---", dir_name)
file_name=str(dir_name)+".txt"
file_or_directory=Path("transcripts/"+mobile_no+"/"+username)
if file_or_directory.exists():
print(str(mobile_no.encode('utf-8'))+"/"+str(username.encode('utf-8')) + " directory exits---")
else:
print(str(mobile_no.encode('utf-8'))+"/"+str(username.encode('utf-8')) + " --directory does not exit. --So Create it")
file_or_directory.mkdir(parents=True, exist_ok=True)
fo = open("transcripts/"+mobile_no+"/"+username+"/"+file_name, "ab+")
my_file = Path("transcripts/"+mobile_no+"/"+username+"/"+ file_name)
print("File exists:---", my_file.is_file())
if my_file.is_file():
fo.write(str.encode("\n\n || Session_ID:- "+str(session)+" ||\n\n"))
stat()
fo.close()
@socketio.on('transcript_user', namespace='/chat')
def chat_message(data):
username = data['data']['author']
username=str(username).strip()
mobile_no = data['data']['mobile_no']
mobile_no=str(mobile_no).strip()
session = data['data']['Session']
session=str(session).strip()
stack.add(session)
dir_name=data['data']['full']
dir_name=str(dir_name).strip()
time=data['data']['time']
time=str(time).strip()
query=data['data']['message']
query=str(query).strip()
print("username ---", username.encode('utf-8'))
print("mobile_no --", mobile_no.encode('utf-8'))
print("session --transcript_user", session)
print("Dir Name-", dir_name)
print("Time", time)
print("query----", query.encode('utf-8'))
file_name = str(dir_name) + ".txt"
file_or_directory = Path("transcripts/" + mobile_no + "/" + username)
if file_or_directory.exists():
print(str(mobile_no.encode('utf-8')) + "/" + str(username.encode('utf-8')) + " directory exits--")
else:
print(str(mobile_no.encode('utf-8')) + "/" + str(username.encode('utf-8')) + " --directory does not exit. --So Create it")
file_or_directory.mkdir(parents=True, exist_ok=True)
fo = open("transcripts/" + mobile_no + "/" + username + "/" + file_name, "ab+")
my_file = Path("transcripts/" + mobile_no + "/" + username + "/" + file_name)
print("File exists:---", my_file.is_file())
if my_file.is_file():
fo.write(str.encode("\n\n ")+username.encode('utf-8')+str.encode(" ( ")+time.encode('utf-8')+str.encode(" ) :-- ") + query.encode('utf-8') + str.encode(" \n\n"))
fo.close()
stat()
@socketio.on('transcript_bot', namespace='/chat')
def chat_message(data):
username = data['data']['author']
username=str(username).strip()
mobile_no = data['data']['mobile_no']
mobile_no=str(mobile_no).strip()
session = data['data']['Session']
session=str(session).strip()
dir_name=data['data']['full']
dir_name=str(dir_name).strip()
time=data['data']['time']
time=str(time).strip()
query=data['data']['message']
query=str(query).strip()
print("username ---", username.encode('utf-8'))
print("mobile_no --", mobile_no.encode('utf-8'))
print("session ----transctip_bot ", session)
print("Dir Name-", dir_name)
print("Time", time)
print("query", query.encode('utf-8'))
file_name = str(dir_name) + ".txt"
file_or_directory = Path("transcripts/" + mobile_no + "/" + username)
if file_or_directory.exists():
print(str(mobile_no.encode('utf-8')) + "/" + str(username.encode('utf-8')) + " directory exits--")
else:
print(str(mobile_no.encode('utf-8')) + "/" + str(username.encode('utf-8')) + " --directory does not exit. --So Create it")
file_or_directory.mkdir(parents=True, exist_ok=True)
fo = open("transcripts/" + mobile_no + "/" + username + "/" + file_name, "ab+")
my_file = Path("transcripts/" + mobile_no + "/" + username + "/" + file_name)
print("File exists:----", my_file.is_file())
username="Weather-Agent"
if my_file.is_file():
fo.write(str.encode("\n\n ")+username.encode('utf-8')+str.encode(" ( ")+time.encode('utf-8')+str.encode(" ) :-- ") + query.encode('utf-8') + str.encode(" \n\n"))
fo.close()
stat()
@socketio.on('response', namespace='/chat')
def chat_message(message):
global path_fail
test_message=message
query=message['data']['message']
session_id=message['data']['Session']
print("\n*************** Query-", query.encode('utf-8')," ---- Username:- ", message['data']['author'].encode('utf-8'), " ----- Session ID:- ", message['data']['Session']," ********")
res=detect_intent_texts(
"florist-shop-5a5b0", session_id, [query], "en-US"
)
response={'data':{'message': res,'author':'Rakin'}}
response=json.dumps(response)
response=json.loads(response)
print(response['data'])
print("query:-",query," response: = ",res,"session: ",message['data']['Session'])
emit('response', {'data': response['data']}, broadcast=False)
if "I am sorry" in res:
print("Yes....................","session: ",message['data']['Session'])
today=str(date.today())
print(str(date.today()))
path = Path('Fail')
if path.exists() == False:
path.mkdir(parents=True, exist_ok=True)
path_fail = "Fail/"+today+".txt"
username=message['data']['author']
time=message['data']['time']
mobile_no=message['data']['mobile_no']
fo = open(path_fail, "ab+")
myfile=Path(path_fail)
if myfile.is_file():
fo.write(str.encode("\n\n ") + username.encode('utf-8')+str.encode(" -- ")+mobile_no.encode('utf-8') + str.encode(" ( ") + time.encode('utf-8') + str.encode(" ) :-- ") + query.encode('utf-8') + str.encode(" \n\n"))
username="Weather-Agent"
fo.write(str.encode("\n\n ") + username.encode('utf-8') + str.encode(" ( ") + time.encode('utf-8') + str.encode(" ) :-- ") + res.encode('utf-8') + str.encode(" \n\n"))
fo.close()
else:
print("No....................","session: ",message['data']['Session'])
stat()
@socketio.on('connect', namespace='/chat')
def test_connect():
global users_connected
users_connected = users_connected + 1
#emit('my response', {'data': 'Connected', 'count': 0})
print("A user connected to the server.....")
stat()
@socketio.on('disconnect', namespace='/chat')
def test_disconnect():
global users_connected
print('A user disconnected from our server...')
#emit('disconnect',broadcast=False)
users_connected= users_connected -1
#users_active=users_active-1
if stack:
stack.pop()
stat()
if __name__ == '__main__':
socketio.run(app, host='0.0.0.0', port=5003, debug=True)
#socketio.run(app,debug=True)