-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
244 lines (201 loc) · 7.59 KB
/
main.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
import sys
# uncomment below line in mac
#sys.path.append('/Users/dheeraj/Documents/Project/venv/lib/python3.9/site-packages')
import os
import random
import time
import re
from flask import (
Flask,
render_template,
session,
redirect,
url_for,
flash,
request,
jsonify
)
from flask_session import Session
from werkzeug.security import check_password_hash, generate_password_hash
import requests
import json
from db import db
from utils import sendmail, openaichat, getintent, chatwithollama
otpstore = {}
conversation_history = []
app = Flask(
__name__,
static_folder = os.path.abspath('static'),
template_folder = os.path.abspath('templates')
)
app.secret_key = 'mrvishal2k2'
app.config['SESSION_TYPE'] = 'filesystem'
Session(app)
#index page
@app.route('/', methods=['GET',"POST"])
def index():
if request.method == "GET":
return render_template('index.html')
else:
data = request.form.to_dict()
print(data)
return "SUccess"
#dashboard page
@app.route('/dashboard')
def dashboard():
user = session.get('email', None)
if not user:
flash("Please login first!", "success")
return redirect(url_for('login'))
return render_template('dashboard.html', user=session["fname"])
@app.route("/chat", methods=["POST", "GET"])
def chat():
if request.method == "GET":
return 404
else:
# Ref: https://gist.github.com/mberman84/a1291cfb08d0a37c3d439028f3bc5f26
user_text = request.json["userText"]
conversation_history.append(f"User: {user_text}")
full_prompt = "\n".join(conversation_history)
intent = getintent(user_text)
print(intent)
# Add intent details to final msg if found, else do nothing
opintent = f"Detected Intent is:{intent}\n\n" if intent != "unknown" else ""
# search order id
pattern = r"\b[1-3][0-9]{1,2}\b" # 1-300 order id
match = re.search(pattern, user_text)
if match:
order_id = match.group(0)
print("order id detected", order_id)
# get order details from db and append it to user msg within bracket
# can pass order details here from db
ordermsg = f"(Order id detected, order details: {order_id}, status: delayed by 2 days)"
user_text += ordermsg
full_prompt += ordermsg
full_prompt += f"\nBot: "
print(full_prompt)
#content, rtype = openaichat(full_prompt)
content, rtype = chatwithollama(user_text)
conversation_history.append(f"Bot: {content}")
if rtype==404:
return jsonify({"error": "Not Found"}), 404
else:
return jsonify({"message": f"{opintent}{content}"}), 200
@app.route("/chathistory", methods=["POST", "GET"])
def chathistory():
if request.method == "GET":
return render_template("chathistory.html")
#help page
@app.route("/help", methods=["POST", "GET"])
def help():
if request.method == "GET":
return render_template("help.html")
#logout page
@app.route("/logout")
def logout():
session["email"] = None
return render_template(url_for("index"))
#login page
@app.route("/login", methods=["GET","POST"])
def login():
if request.method == "GET":
return render_template("login.html")
else:
data = request.form.to_dict()
x = db.users.find_one({"email":data.get('email')})
if x and check_password_hash(x.get('pass'), data.get('pass')):
session["fname"] = x.get('fname')
session['email'] = data.get('email')
return redirect(url_for('dashboard'))
flash("Wrong credentials!", "error")
return redirect(url_for('login'))
#reset password page
@app.route("/reset", methods=["GET", "POST"])
def reset():
if request.method == 'GET':
return render_template("reset.html")
else:
data = request.form.to_dict()
email_exists = bool(db.users.find_one({"email": data["email"]}))
if not email_exists:
flash("Account with the mail doesn't exist", "error")
return redirect(url_for('register'))
otp = "".join([random.choice("0123456789") for _ in range(6)])
print(otp)
sendmail(data["email"], otp)
otpstore[data["email"]] = {"otp":str(otp), "time": time.time(), "isDone": False}
session["otp_page"] =True
return redirect(url_for("verify", email=data["email"]))
#verifying otp page
@app.route("/verify", methods=["GET","POST"])
def verify():
if request.method == 'GET':
if not session.get("otp_page") or not session:
return redirect(url_for("reset"))
else:
return render_template("otp.html", email=request.args.get("email"))
else:
codes = ''.join(request.form.getlist("code"))
email = request.form['email']
if email in otpstore:
daata = otpstore[email]
if int(time.time())-int(daata["time"]) <= 600: # 10 mins timeout
if codes==daata["otp"]: # correct otp
otpstore[email]["isDone"] = True
return redirect(url_for("setpassword", email=email))
else:
flash("Invalid OTP, Try again", "error")
return redirect(url_for("verify", email=email))
else:
flash("Timeout, Try sending otp again later","error")
return redirect(url_for("verify", email=email))
else:
return redirect(url_for("reset"))
@app.route("/setpassword", methods=["GET", "POST"])
def setpassword():
if request.method == 'GET':
if not session.get("otp_page") or not session:
return redirect(url_for("reset"))
else:
email = request.args.get("email", None)
if email is None:
return redirect(url_for("reset"))
if otpstore[email]["isDone"]:
return render_template("setpassword.html", email=request.args.get("email"))
else:
return "Incomplete, u not verified otp"
else:
data = request.form.to_dict()
email = data["email"]
hashed_password = generate_password_hash(data["pass1"], method='pbkdf2:sha256')
result = db.users.update_one(
{"email": email.lower()},
{"$set": {"pass": hashed_password}}
)
if result.matched_count == 1:
return render_template("exit.html")
else:
return f"No user found with email: {email}"
#register page
@app.route("/register", methods=["GET","POST"])
def register():
if request.method == "GET":
return render_template("register.html")
else:
data = request.form.to_dict()
print(data)
x = db.users.find_one({"email":data.get('email')})
if x:
flash("User with this email already exist", "error")
return redirect(url_for('register'))
flash("Account created!", "success")
db.users.insert_one({"fname":data.get('fname'), "email":data.get('email').lower(), "pnum":data.get('pnum'), "pass":generate_password_hash(data.get('pass'), method='pbkdf2:sha256')})
return redirect(url_for('login'))
@app.errorhandler(404)
def handle_404(e):
return render_template('notfound.html')
@app.errorhandler(505)
def handle_505(e):
return render_template('notfound.html')
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8000, debug=True)