-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_1.py
287 lines (236 loc) · 9.15 KB
/
main_1.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
#import pymysql
import sqlite3
import os
conn = sqlite3.connect('chatbot_database')
cur = conn.cursor()
try:
cur.execute('''CREATE TABLE doctor (
id integer Primary key AUTOINCREMENT,
name varchar(20),
email varchar(50),
password varchar(20),
gender varchar(10),
specialist varchar(50),
avaliability tinyint(1),
address varchar(100))''')
cur.execute('''CREATE TABLE user (
name varchar(20) DEFAULT NULL,
email varchar(50) DEFAULT NULL,
password varchar(20) DEFAULT NULL,
gender varchar(10) DEFAULT NULL,
age int(11) DEFAULT NULL
)''')
cur.execute('''CREATE TABLE appointment (
name varchar(20) DEFAULT NULL,
doctor_name varchar(50) DEFAULT NULL,
date_time varchar(20) DEFAULT NULL,
hospital varchar(10) DEFAULT NULL)''')
except:
pass
import os
from flask import Flask, render_template, url_for,request, flash, redirect, session
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
filenumber = int(os.listdir('saved_conversations')[-1])
filenumber = filenumber+1
file= open('saved_conversations/'+str(filenumber),"w+")
file.write('bot : Hi There! I am a medical chatbot. You can begin conversation by typing in a message and pressing enter.\n')
file.close()
app = Flask(__name__)
english_bot = ChatBot('Bot',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
logic_adapters=[
{
'import_path': 'chatterbot.logic.BestMatch'
},
],
trainer='chatterbot.trainers.ListTrainer')
english_bot.set_trainer(ListTrainer)
#conn = pymysql.connect(host='127.0.0.1', user='root', password='root', database='xtipl')
#cur = conn.cursor()
@app.route('/user_login',methods = ['POST', 'GET'])
def user_login():
conn = sqlite3.connect('chatbot_database')
cur = conn.cursor()
if request.method == 'POST':
email = request.form['email']
password = request.form['psw']
count = cur.execute('SELECT * FROM user WHERE email = "%s" AND password = "%s"' % (email, password))
#conn.commit()
#cur.close()
#print(count)
if len(cur.fetchall()) == 1:
session['logged_in'] = True
cur.execute('select * from doctor where avaliability="true"')
s = cur.fetchall()
print(s)
cur.execute('select * from doctor where avaliability="false"')
#conn.commit()
s1 = cur.fetchall()
s2 = s + s1
return render_template('user_account.html', data=s2)
else:
flash('invalid email and password!')
return render_template('user_login.html')
@app.route('/user_register',methods = ['POST', 'GET'])
def user_register():
conn = sqlite3.connect('chatbot_database')
cur = conn.cursor()
if request.method == 'POST':
name = request.form['uname']
email = request.form['email']
password = request.form['psw']
gender = request.form['gender']
age = request.form['age']
cur.execute("insert into user(name,email,password,gender,age)values('%s','%s','%s','%s','%s')" % (name, email, password, gender, age))
conn.commit()
# cur.close()
return redirect(url_for('user_login'))
return render_template('user_register.html')
@app.route('/doctor_login',methods = ['POST', 'GET'])
def doctor_login():
conn = sqlite3.connect('chatbot_database')
cur = conn.cursor()
if request.method == 'POST':
email = request.form['email']
password = request.form['psw']
count = cur.execute('SELECT * FROM doctor WHERE email = "%s" AND password = "%s"' % (email, password))
conn.commit()
#cur.close()
print(count)
if len(cur.fetchall()) == 1:
print(count)
flash('{} You have been logged in!'.format(email), 'success')
session['logged_in_d'] = True
cur.execute("update doctor set avaliability='true' where email='%s'" %email)
print("""update doctor set avaliability='true' where email='%s'" %email""")
conn.commit()
cur.execute('select * from doctor where email="%s"' % email)
s = cur.fetchall()
print(s)
return render_template('doctor_account.html', a=email,b=s)
else:
flash('invalid email and password')
return redirect(url_for('doctor_login'))
return render_template('doctor_login.html')
@app.route('/medicine_predictor')
def medicine_predictor():
import college_doctor
return render_template('user_account.html')
@app.route('/server')
def server():
import pythonchat.pyserve
@app.route('/livechat')
def livechat():
#os.system('dir')
os.system('python pythonchat/pyclient.py')
return render_template('user_account.html')
@app.route('/doctor_livechat')
def doctor_livechat():
#os.system('dir')
os.system('python pythonchat/pyclient.py')
return render_template('doctor_login.html')
@app.route('/doctor_register',methods = ['POST', 'GET'])
def doctor_register():
conn = sqlite3.connect('chatbot_database')
cur = conn.cursor()
if request.method == 'POST':
name = request.form['uname']
email = request.form['email']
password = request.form['psw']
gender = request.form['gender']
specialist = request.form['specialist']
address = request.form['address']
cur.execute("insert into doctor(name,email,password,gender,specialist,avaliability,address)values('%s','%s','%s','%s','%s','false','%s')" % (name, email, password, gender, specialist, address))
conn.commit()
# cur.close()
return redirect(url_for('doctor_login'))
return render_template('doctor_register.html')
@app.route('/user_account',methods = ['POST', 'GET'])
def user_account():
return render_template('user_account.html')
@app.route('/doctor_account',methods = ['POST', 'GET'])
def doctor_account():
if request.method == 'POST':
email = request.form['email']
return render_template('doctor_account_edit.html', em = email)
@app.route('/doctor_edit',methods = ['POST', 'GET'])
def doctor_edit():
conn = sqlite3.connect('chatbot_database')
cur = conn.cursor()
if request.method == 'POST':
name = request.form['uname']
email = request.form['email']
password = request.form['psw']
gender = request.form['gender']
specialist = request.form['specialist']
address = request.form['address']
em = request.form['em']
cur.execute("update doctor set name='%s', email='%s',password='%s',gender='%s',specialist='%s',address='%s' where email='%s'" %
(name, email,password, gender, specialist, address,em))
conn.commit()
return redirect('doctor_login')
@app.route('/')
@app.route('/home')
def home():
if not session.get('logged_in'):
return render_template('home.html')
else:
return redirect(url_for('user_account'))
@app.route('/chatbot')
def chatbot():
return render_template('index.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route("/logout")
def logout():
session['logged_in'] = False
return home()
@app.route("/logoutd",methods = ['POST','GET'])
def logoutd():
conn = sqlite3.connect('chatbot_database')
cur = conn.cursor()
print('doctor logout')
if request.method == 'POST':
email = request.form['email']
cur.execute('update doctor set avaliability="false" where email="%s"' %email)
conn.commit()
session['logged_in_d'] = False
return home()
@app.route("/get")
def get_bot_response():
userText = request.args.get('msg')
response = str(english_bot.get_response(userText))
appendfile=os.listdir('saved_conversations')[-1]
appendfile= open('saved_conversations/'+str(filenumber),"a")
appendfile.write('user : '+userText+'\n')
appendfile.write('bot : '+response+'\n')
appendfile.close()
return response
@app.route('/appoinment',methods = ['POST', 'GET'])
def appoinment():
conn = sqlite3.connect('chatbot_database')
cur = conn.cursor()
if request.method == 'POST':
name = request.form['name']
doctor = request.form['doctor']
date_time = request.form['date_time']
hospital = request.form['hospital']
cur.execute("insert into appointment(name,doctor_name,date_time,hospital)values('%s','%s','%s','%s')" % (name, doctor, date_time, hospital))
conn.commit()
return redirect(url_for('user_login'))
return render_template('appoinment.html')
@app.route('/appoinment_details',methods = ['POST', 'GET'])
def appoinment_details():
conn = sqlite3.connect('chatbot_database')
cur = conn.cursor()
#if request.method == 'POST':
session['logged_in'] = True
cur.execute('select * from appointment')
s = cur.fetchall()
return render_template('appoinment_details.html', data=s)
#return render_template('doctor_account.html')
if __name__ == '__main__':
app.secret_key = os.urandom(12)
app.run(debug=True)