Th3BossC commited on
Commit
1b104d8
1 Parent(s): d2071cc

rewired login functionality

Browse files
ChitChat/__init__.py CHANGED
@@ -3,17 +3,18 @@ from flask_sqlalchemy import SQLAlchemy
3
  from flask_bcrypt import Bcrypt
4
  from flask_cors import CORS
5
  from ChitChat.config import Config
6
-
7
 
8
  db = SQLAlchemy()
9
  bcrypt = Bcrypt()
10
-
11
  def create_app(config_class = Config):
12
  app = Flask(__name__)
13
  CORS(app)
14
  app.config.from_object(Config)
15
  db.init_app(app)
16
  bcrypt.init_app(app)
 
17
 
18
  from ChitChat.resources.routes import resources
19
  app.register_blueprint(resources)
 
3
  from flask_bcrypt import Bcrypt
4
  from flask_cors import CORS
5
  from ChitChat.config import Config
6
+ from flask_login import LoginManager
7
 
8
  db = SQLAlchemy()
9
  bcrypt = Bcrypt()
10
+ login_manager = LoginManager()
11
  def create_app(config_class = Config):
12
  app = Flask(__name__)
13
  CORS(app)
14
  app.config.from_object(Config)
15
  db.init_app(app)
16
  bcrypt.init_app(app)
17
+ login_manager.init_app(app)
18
 
19
  from ChitChat.resources.routes import resources
20
  app.register_blueprint(resources)
ChitChat/common/files/.gitignore CHANGED
@@ -0,0 +1 @@
 
 
1
+ **/**
ChitChat/common/utils.py CHANGED
@@ -3,7 +3,9 @@ from transformers import T5Tokenizer, T5ForConditionalGeneration
3
  import torch
4
  from flask import current_app
5
  from ChitChat import db
6
- from ChitChat.models import ChatHistory
 
 
7
 
8
  small_model_name = 'Th3BossC/DialoGPT-medium-AICLUB_NITC'
9
  default_model = 'microsoft/DialoGPT-medium'
@@ -24,7 +26,7 @@ def getChatHistory(user):
24
 
25
  def saveChatHistory(user, chat_history_ids):
26
  location = current_app.config.get('SAVE_FOLDER')
27
- file_name = location + user.user_id + '.pt'
28
  if chat_history_ids.shape[-1] > 100:
29
  user.history = None
30
  db.session.commit()
 
3
  import torch
4
  from flask import current_app
5
  from ChitChat import db
6
+ from ChitChat.models import User
7
+ # from flask_login import current_user
8
+
9
 
10
  small_model_name = 'Th3BossC/DialoGPT-medium-AICLUB_NITC'
11
  default_model = 'microsoft/DialoGPT-medium'
 
26
 
27
  def saveChatHistory(user, chat_history_ids):
28
  location = current_app.config.get('SAVE_FOLDER')
29
+ file_name = location + str(user.id) + '.pt'
30
  if chat_history_ids.shape[-1] > 100:
31
  user.history = None
32
  db.session.commit()
ChitChat/models.py CHANGED
@@ -1,15 +1,19 @@
1
  from ChitChat import db
2
  from datetime import datetime
3
  import uuid
 
 
4
 
5
- class ChatHistory(db.Model):
 
 
 
 
6
  id = db.Column(db.Integer, primary_key = True)
7
- user_id = db.Column(db.String(), unique = True, nullable = False, default = str(uuid.uuid4()))
8
  username = db.Column(db.String(10), unique = True, nullable = False)
9
  email = db.Column(db.String(100), nullable = False)
10
  password = db.Column(db.String(60), nullable = False)
11
  date = db.Column(db.DateTime, nullable = False, default = datetime.utcnow)
12
  history = db.Column(db.String(100), nullable = True)
13
-
14
  def __repr__(self):
15
- return f"ChatHistory({self.username}, {self.email})"
 
1
  from ChitChat import db
2
  from datetime import datetime
3
  import uuid
4
+ from flask_login import UserMixin
5
+ from ChitChat import login_manager
6
 
7
+ @login_manager.user_loader
8
+ def load_user(user_id):
9
+ return User.query.get(int(user_id))
10
+
11
+ class User(db.Model, UserMixin):
12
  id = db.Column(db.Integer, primary_key = True)
 
13
  username = db.Column(db.String(10), unique = True, nullable = False)
14
  email = db.Column(db.String(100), nullable = False)
15
  password = db.Column(db.String(60), nullable = False)
16
  date = db.Column(db.DateTime, nullable = False, default = datetime.utcnow)
17
  history = db.Column(db.String(100), nullable = True)
 
18
  def __repr__(self):
19
+ return f"User({self.username}, {self.email})"
ChitChat/resources/routes.py CHANGED
@@ -1,9 +1,9 @@
1
  from flask import Blueprint, request, current_app
2
  from flask_restful import Api, Resource
3
- from ChitChat.models import ChatHistory
4
  from ChitChat import bcrypt, db
5
  from ChitChat.common.utils import conversation, complexChat
6
-
7
 
8
  resources = Blueprint('resources', __name__)
9
  api = Api(resources)
@@ -12,13 +12,12 @@ api = Api(resources)
12
  class UserLogin(Resource):
13
  def post(self):
14
  userInfo = request.json
15
-
16
- user = ChatHistory.query.filter_by(email = userInfo['email']).first()
17
 
18
  if user is None:
19
  return {'status' : "Account doesn't exist", 'user_id' : None}
20
  elif bcrypt.check_password_hash(pw_hash = user.password, password = userInfo['password']):
21
- return {'status' : "login successful", 'user_id' : user.user_id}
22
  else:
23
  return {'status' : "Invalid password", 'user_id' : None}
24
  api.add_resource(UserLogin, '/login/')
@@ -27,23 +26,24 @@ class RegisterUser(Resource):
27
  def post(self):
28
  userInfo = request.json
29
 
30
- user = ChatHistory.query.filter_by(email = userInfo['email']).first()
31
  if user is not None:
32
  return {'status' : 'email already registered'}
33
  else:
34
- user = ChatHistory.query.filter_by(username = userInfo['username']).first()
35
  if user is not None:
36
  return {'status' : 'Username already exists'}
37
 
38
- newUser = ChatHistory(username = userInfo['username'], email = userInfo['email'], password = bcrypt.generate_password_hash(password = userInfo['password']))
39
  db.session.add(newUser)
40
  db.session.commit()
41
  return {'status' : 'User created successfully'}
42
  api.add_resource(RegisterUser, '/register/')
43
 
 
44
  class ChatBot(Resource):
45
  def post(self, user_id):
46
- user = ChatHistory.query.filter_by(user_id = user_id).first()
47
  userInput = request.json['user']
48
  if user is None:
49
  return {'error' : "User doesn't exist"}, 400
 
1
  from flask import Blueprint, request, current_app
2
  from flask_restful import Api, Resource
3
+ from ChitChat.models import User
4
  from ChitChat import bcrypt, db
5
  from ChitChat.common.utils import conversation, complexChat
6
+ # from flask_login import login_user, logout_user, current_user
7
 
8
  resources = Blueprint('resources', __name__)
9
  api = Api(resources)
 
12
  class UserLogin(Resource):
13
  def post(self):
14
  userInfo = request.json
15
+ user = User.query.filter_by(username = userInfo['username']).first()
 
16
 
17
  if user is None:
18
  return {'status' : "Account doesn't exist", 'user_id' : None}
19
  elif bcrypt.check_password_hash(pw_hash = user.password, password = userInfo['password']):
20
+ return {'status' : "login successful", 'user_id' : str(user.id)}
21
  else:
22
  return {'status' : "Invalid password", 'user_id' : None}
23
  api.add_resource(UserLogin, '/login/')
 
26
  def post(self):
27
  userInfo = request.json
28
 
29
+ user = User.query.filter_by(email = userInfo['email']).first()
30
  if user is not None:
31
  return {'status' : 'email already registered'}
32
  else:
33
+ user = User.query.filter_by(username = userInfo['username']).first()
34
  if user is not None:
35
  return {'status' : 'Username already exists'}
36
 
37
+ newUser = User(username = userInfo['username'], email = userInfo['email'], password = bcrypt.generate_password_hash(password = userInfo['password']))
38
  db.session.add(newUser)
39
  db.session.commit()
40
  return {'status' : 'User created successfully'}
41
  api.add_resource(RegisterUser, '/register/')
42
 
43
+
44
  class ChatBot(Resource):
45
  def post(self, user_id):
46
+ user = User.query.filter_by(id = user_id).first()
47
  userInput = request.json['user']
48
  if user is None:
49
  return {'error' : "User doesn't exist"}, 400
app.py CHANGED
@@ -3,8 +3,8 @@ from ChitChat import create_app
3
 
4
  app = create_app()
5
 
6
- # if __name__ == '__main__':
7
- # app.run(debug = True, port = 5000)
8
-
9
  if __name__ == '__main__':
10
- app.run(debug = False, host = "0.0.0.0", port = 7860)
 
 
 
 
3
 
4
  app = create_app()
5
 
 
 
 
6
  if __name__ == '__main__':
7
+ app.run(debug = True, port = 5000)
8
+
9
+ # if __name__ == '__main__':
10
+ # app.run(debug = False, host = "0.0.0.0", port = 7860)
instance/site.db CHANGED
Binary files a/instance/site.db and b/instance/site.db differ