PascalZhan
commited on
Commit
•
d72e3a5
0
Parent(s):
Synchronisation Frontend DracolIA QA
Browse files- Controllers/Controller_Database.py +102 -0
- Controllers/Controller_IA.py +23 -0
- Controllers/__init__.py +0 -0
- Dockerfile +15 -0
- Exceptions/ModelNotSelectedException.py +2 -0
- Exceptions/UserExistsException.py +2 -0
- Exceptions/UserNotConnectedException.py +2 -0
- Exceptions/UserOrPasswordIncorrectException.py +2 -0
- Exceptions/__init__.py +0 -0
- Model/Database.py +232 -0
- Model/IA.py +63 -0
- Model/__init__.py +0 -0
- README.md +8 -0
- Services/Service_Chat.py +103 -0
- Services/Service_Chat_Message.py +87 -0
- Services/Service_IA.py +50 -0
- Services/Service_User.py +62 -0
- Services/__init__.py +0 -0
- main.py +16 -0
- requirements.txt +6 -0
Controllers/Controller_Database.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Blueprint, request, jsonify
|
2 |
+
from Services.Service_User import Service_User
|
3 |
+
from Services.Service_Chat import Service_Chat
|
4 |
+
from Services.Service_Chat_Message import Service_Chat_Message
|
5 |
+
from Exceptions.UserExistsException import UserExistsException
|
6 |
+
from Exceptions.UserOrPasswordIncorrectException import UserOrPasswordIncorrectException
|
7 |
+
|
8 |
+
signup_blueprint = Blueprint('signup', __name__)
|
9 |
+
login_blueprint = Blueprint('login', __name__)
|
10 |
+
user_chat_history_blueprint = Blueprint('user chat history', __name__)
|
11 |
+
delete_chat_blueprint = Blueprint('delete chat', __name__)
|
12 |
+
chat_message_blueprint = Blueprint('chat message', __name__)
|
13 |
+
get_chat_blueprint = Blueprint('get chat', __name__)
|
14 |
+
|
15 |
+
@signup_blueprint.route('/signup', methods=['POST'])
|
16 |
+
def signup_user():
|
17 |
+
service_user = Service_User()
|
18 |
+
|
19 |
+
try:
|
20 |
+
username = request.json['username']
|
21 |
+
password = request.json['password']
|
22 |
+
|
23 |
+
message = service_user.signup_user(username, password)
|
24 |
+
return jsonify({'message': message}), 200
|
25 |
+
|
26 |
+
except UserExistsException as e:
|
27 |
+
return jsonify({'message': str(e)}), 409
|
28 |
+
|
29 |
+
|
30 |
+
@login_blueprint.route('/login', methods=['POST'])
|
31 |
+
def login_user():
|
32 |
+
service_user = Service_User()
|
33 |
+
|
34 |
+
username = request.json['username']
|
35 |
+
password = request.json['password']
|
36 |
+
|
37 |
+
try:
|
38 |
+
user_information = service_user.login_user(username, password)
|
39 |
+
|
40 |
+
if user_information['password_match']:
|
41 |
+
user_information['message'] = 'Connected successfully.'
|
42 |
+
user_information['state'] = True
|
43 |
+
return jsonify(user_information)
|
44 |
+
|
45 |
+
except UserOrPasswordIncorrectException as e:
|
46 |
+
return jsonify({'message': str(e), 'state': False})
|
47 |
+
|
48 |
+
|
49 |
+
@user_chat_history_blueprint.route('/chatHistory', methods=['GET'])
|
50 |
+
def user_chat_history():
|
51 |
+
serviceChat = Service_Chat()
|
52 |
+
|
53 |
+
user_id = request.json['user_id']
|
54 |
+
user_is_connected = request.json['user_is_connected']
|
55 |
+
|
56 |
+
try:
|
57 |
+
chat_history = serviceChat.get_all_user_chats(user_id, user_is_connected)
|
58 |
+
return jsonify(chat_history), 200
|
59 |
+
except Exception as e:
|
60 |
+
return jsonify({'message': "Error fetching chat history."}), 500
|
61 |
+
|
62 |
+
@delete_chat_blueprint.route('/deleteChat', methods=['POST'])
|
63 |
+
def user_chat_history():
|
64 |
+
serviceChat = Service_Chat()
|
65 |
+
|
66 |
+
user_id = request.json['user_id']
|
67 |
+
user_is_connected = request.json['user_is_connected']
|
68 |
+
chat_id = request.json['chat_id']
|
69 |
+
|
70 |
+
try:
|
71 |
+
serviceChat.delete_chat(chat_id, user_is_connected, user_id)
|
72 |
+
return jsonify({'message': "As success"}), 200
|
73 |
+
except Exception as e:
|
74 |
+
return jsonify({'message': "Error fetching chat history."}), 500
|
75 |
+
|
76 |
+
@chat_message_blueprint.route('/chatMessage', methods=['GET'])
|
77 |
+
def chat_message():
|
78 |
+
serviceChatMessage = Service_Chat_Message()
|
79 |
+
|
80 |
+
user_id = request.json['user_id']
|
81 |
+
user_is_connected = request.json['user_is_connected']
|
82 |
+
chat_id = request.json['chat_id']
|
83 |
+
try:
|
84 |
+
chat = serviceChatMessage.get_chat_messages_by_chat_id_and_user_id(chat_id, user_is_connected, user_id)
|
85 |
+
return jsonify(chat), 200
|
86 |
+
except Exception as e:
|
87 |
+
return jsonify({'message': "Error fetching chat history."}), 500
|
88 |
+
|
89 |
+
|
90 |
+
@get_chat_blueprint.route('/getChat', methods=['GET'])
|
91 |
+
def get_chat():
|
92 |
+
serviceChat = Service_Chat()
|
93 |
+
|
94 |
+
user_id = request.json['user_id']
|
95 |
+
is_connected = request.json['user_is_connected']
|
96 |
+
chat_id = request.json['chat_id']
|
97 |
+
|
98 |
+
try:
|
99 |
+
chat = serviceChat.get_chat(chat_id, user_id, is_connected)
|
100 |
+
return jsonify(chat), 200
|
101 |
+
except Exception as e:
|
102 |
+
return jsonify({'message': "Error fetching chat history."}), 500
|
Controllers/Controller_IA.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Blueprint, request, jsonify
|
2 |
+
from Services.Service_IA import Service_IA
|
3 |
+
|
4 |
+
prediction_blueprint = Blueprint('prediction', __name__)
|
5 |
+
|
6 |
+
@prediction_blueprint.route('/predict', methods=['POST'])
|
7 |
+
def predict():
|
8 |
+
|
9 |
+
question = request.json['user_question']
|
10 |
+
user_id = request.json['user_id']
|
11 |
+
chat_id = request.json['chat_id']
|
12 |
+
user_is_connected = request.json['user_is_connected']
|
13 |
+
file_content = request.json['file_content']
|
14 |
+
have_file = request.json['have_file']
|
15 |
+
model = request.json['model_selected']
|
16 |
+
|
17 |
+
try:
|
18 |
+
ia = Service_IA()
|
19 |
+
predictions = ia.generate_responses(question, user_id, chat_id, user_is_connected, file_content, have_file, model)
|
20 |
+
except Exception as e:
|
21 |
+
return jsonify({'message': str(e)}), 500
|
22 |
+
|
23 |
+
return jsonify(predictions)
|
Controllers/__init__.py
ADDED
File without changes
|
Dockerfile
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.12
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
RUN chmod 777 /code
|
6 |
+
|
7 |
+
COPY ./requirements.txt /code/requirements.txt
|
8 |
+
|
9 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
10 |
+
|
11 |
+
COPY . .
|
12 |
+
|
13 |
+
RUN pip install --no-cache-dir --upgrade -r ./requirements.txt
|
14 |
+
|
15 |
+
CMD ["gunicorn", "-b", "0.0.0.0:7860", "main:app"]
|
Exceptions/ModelNotSelectedException.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
class ModelNotSelectedException(Exception):
|
2 |
+
pass
|
Exceptions/UserExistsException.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
class UserExistsException(Exception):
|
2 |
+
pass
|
Exceptions/UserNotConnectedException.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
class UserNotConnectedException(Exception):
|
2 |
+
pass
|
Exceptions/UserOrPasswordIncorrectException.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
class UserOrPasswordIncorrectException(Exception):
|
2 |
+
pass
|
Exceptions/__init__.py
ADDED
File without changes
|
Model/Database.py
ADDED
@@ -0,0 +1,232 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sqlite3
|
2 |
+
|
3 |
+
class Database:
|
4 |
+
|
5 |
+
db_name = "./database.db"
|
6 |
+
|
7 |
+
def __init__(self):
|
8 |
+
self.conn = sqlite3.connect(self.db_name)
|
9 |
+
self.cursor = self.conn.cursor()
|
10 |
+
|
11 |
+
self.cursor.execute('''
|
12 |
+
CREATE TABLE IF NOT EXISTS users (
|
13 |
+
user_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
14 |
+
username TEXT NOT NULL,
|
15 |
+
password TEXT NOT NULL
|
16 |
+
)
|
17 |
+
'''
|
18 |
+
)
|
19 |
+
|
20 |
+
self.cursor.execute('''
|
21 |
+
CREATE TABLE IF NOT EXISTS chat (
|
22 |
+
chat_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
23 |
+
user_id INTEGER NOT NULL,
|
24 |
+
chat_title TEXT NOT NULL,
|
25 |
+
chat_file_content TEXT,
|
26 |
+
chat_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
27 |
+
chat_model TEXT NOT NULL,
|
28 |
+
FOREIGN KEY (user_id) REFERENCES users(user_id)
|
29 |
+
)
|
30 |
+
'''
|
31 |
+
)
|
32 |
+
|
33 |
+
self.cursor.execute('''
|
34 |
+
CREATE TABLE IF NOT EXISTS chat_message(
|
35 |
+
chat_id INTEGER NOT NULL,
|
36 |
+
chat_message_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
37 |
+
chat_message TEXT NOT NULL,
|
38 |
+
chat_message_is_ia BOOLEAN NOT NULL,
|
39 |
+
chat_message_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
40 |
+
FOREIGN KEY (chat_id) REFERENCES chat(chat_id)
|
41 |
+
)
|
42 |
+
'''
|
43 |
+
)
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
"""
|
48 |
+
|
49 |
+
USER TABLE REQUESTS
|
50 |
+
|
51 |
+
"""
|
52 |
+
### SELECT REQUESTS
|
53 |
+
def select_user_by_name(self, username):
|
54 |
+
self.cursor.execute('SELECT user_id, username, password FROM users WHERE username = ?', (username,))
|
55 |
+
return self.cursor.fetchone()
|
56 |
+
|
57 |
+
def select_user_by_id(self, user_id):
|
58 |
+
self.cursor.execute('SELECT user_id, username, password FROM users WHERE user_id = ?', (user_id,))
|
59 |
+
return self.cursor.fetchone()
|
60 |
+
|
61 |
+
### DELETE REQUESTS
|
62 |
+
def delete_user_by_name(self, username):
|
63 |
+
self.cursor.execute('DELETE FROM users WHERE username = ?', (username,))
|
64 |
+
self.conn.commit()
|
65 |
+
|
66 |
+
def delete_user_by_id(self, user_id):
|
67 |
+
self.cursor.execute('DELETE FROM users WHERE user_id = ?', (user_id,))
|
68 |
+
self.conn.commit()
|
69 |
+
|
70 |
+
### UPDATE REQUESTS
|
71 |
+
def update_user_username_by_id(self, user_id, username):
|
72 |
+
self.cursor.execute('UPDATE users SET username = ? WHERE user_id = ?', (username, user_id))
|
73 |
+
self.conn.commit()
|
74 |
+
|
75 |
+
def update_user_password_by_id(self, user_id, password):
|
76 |
+
self.cursor.execute('UPDATE users SET password = ? WHERE user_id = ?', (password, user_id))
|
77 |
+
self.conn.commit()
|
78 |
+
|
79 |
+
def update_user_password_by_name(self, username, password):
|
80 |
+
self.cursor.execute('UPDATE users SET password = ? WHERE username = ?', (password, username))
|
81 |
+
self.conn.commit()
|
82 |
+
|
83 |
+
def update_user_username_by_id(self, user_id, username):
|
84 |
+
self.cursor.execute('UPDATE users SET username = ? WHERE user_id = ?', (username, user_id))
|
85 |
+
self.conn.commit()
|
86 |
+
|
87 |
+
### INSERT REQUESTS
|
88 |
+
def insert_user(self, username, password):
|
89 |
+
self.cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))
|
90 |
+
self.conn.commit()
|
91 |
+
|
92 |
+
### CUSTOM REQUESTS
|
93 |
+
def user_exists(self, username):
|
94 |
+
self.cursor.execute('SELECT user_id FROM users WHERE username = ?', (username,))
|
95 |
+
exists = self.cursor.fetchone() is not None
|
96 |
+
return exists
|
97 |
+
|
98 |
+
|
99 |
+
|
100 |
+
|
101 |
+
"""
|
102 |
+
|
103 |
+
Chat TABLE REQUESTS
|
104 |
+
|
105 |
+
"""
|
106 |
+
### SELECT REQUESTS
|
107 |
+
def select_chat_by_id(self, chat_id):
|
108 |
+
self.cursor.execute('SELECT chat_id, user_id, chat_title, chat_file_content, chat_date, chat_model FROM chat WHERE chat_id = ?', (chat_id,))
|
109 |
+
return self.cursor.fetchone()
|
110 |
+
|
111 |
+
def select_chat_by_user_id(self, user_id):
|
112 |
+
self.cursor.execute('SELECT chat_id, user_id, chat_title, chat_file_content, chat_date, chat_model FROM chat WHERE user_id = ?', (user_id,))
|
113 |
+
return self.cursor.fetchall()
|
114 |
+
|
115 |
+
def select_chat_by_title(self, chat_title):
|
116 |
+
self.cursor.execute('SELECT chat_id, user_id, chat_title, chat_file_content, chat_date, chat_model FROM chat WHERE chat_title = ?', (chat_title,))
|
117 |
+
return self.cursor.fetchone()
|
118 |
+
|
119 |
+
### DELETE REQUESTS
|
120 |
+
def delete_chat_by_id(self, chat_id):
|
121 |
+
self.cursor.execute('DELETE FROM chat WHERE chat_id = ?', (chat_id,))
|
122 |
+
self.conn.commit()
|
123 |
+
|
124 |
+
def delete_chat_by_user_id(self, user_id):
|
125 |
+
self.cursor.execute('DELETE FROM chat WHERE user_id = ?', (user_id,))
|
126 |
+
self.conn.commit()
|
127 |
+
|
128 |
+
def delete_chat_by_title(self, chat_title):
|
129 |
+
self.cursor.execute('DELETE FROM chat WHERE chat_title = ?', (chat_title,))
|
130 |
+
self.conn.commit()
|
131 |
+
|
132 |
+
### UPDATE REQUESTS
|
133 |
+
def update_chat_title_by_id(self, chat_id, chat_title):
|
134 |
+
self.cursor.execute('UPDATE chat SET chat_title = ? WHERE chat_id = ?', (chat_title, chat_id))
|
135 |
+
self.conn.commit()
|
136 |
+
|
137 |
+
def update_chat_title_by_user_id(self, user_id, chat_title):
|
138 |
+
self.cursor.execute('UPDATE chat SET chat_title = ? WHERE user_id = ?', (chat_title, user_id))
|
139 |
+
self.conn.commit()
|
140 |
+
|
141 |
+
def update_chat_title_by_title(self, chat_title, new_chat_title):
|
142 |
+
self.cursor.execute('UPDATE chat SET chat_title = ? WHERE chat_title = ?', (new_chat_title, chat_title))
|
143 |
+
self.conn.commit()
|
144 |
+
|
145 |
+
### INSERT REQUESTS
|
146 |
+
def insert_chat(self, user_id, chat_title, chat_file_content, chat_model):
|
147 |
+
self.cursor.execute("INSERT INTO chat (user_id, chat_title, chat_file_content, chat_date, chat_model) VALUES (?, ?, ?, datetime('now'), ?) RETURNING chat_id", (user_id, chat_title, chat_file_content, chat_model))
|
148 |
+
chat_id = self.cursor.fetchone()[0]
|
149 |
+
self.conn.commit()
|
150 |
+
return chat_id
|
151 |
+
|
152 |
+
### CUSTOM REQUESTS
|
153 |
+
def select_chat_by_id_and_user_id(self, chat_id, user_id):
|
154 |
+
self.cursor.execute('SELECT chat_id, user_id, chat_title, chat_file_content, chat_date, chat_model FROM chat WHERE chat_id = ? AND user_id = ?', (chat_id, user_id))
|
155 |
+
return self.cursor.fetchone()
|
156 |
+
|
157 |
+
def select_all_user_chats(self, user_id):
|
158 |
+
self.cursor.execute('SELECT chat_id, chat_title, chat_file_content, chat_date, chat_model FROM chat WHERE user_id = ?', (user_id,))
|
159 |
+
return self.cursor.fetchall()
|
160 |
+
|
161 |
+
def delete_chat_by_id_and_by_user_id(self, chat_id, user_id):
|
162 |
+
self.cursor.execute('DELETE FROM chat WHERE chat_id = ? AND user_id = ?', (chat_id, user_id))
|
163 |
+
self.conn.commit()
|
164 |
+
|
165 |
+
def update_chat_file_content_by_id(self, chat_id, chat_file_content):
|
166 |
+
self.cursor.execute('UPDATE chat SET chat_file_content = ? WHERE chat_id = ?', (chat_file_content, chat_id))
|
167 |
+
self.conn.commit()
|
168 |
+
|
169 |
+
|
170 |
+
|
171 |
+
"""
|
172 |
+
|
173 |
+
chat_message TABLE REQUESTS
|
174 |
+
|
175 |
+
"""
|
176 |
+
### SELECT REQUESTS
|
177 |
+
def select_chat_message_by_chat_id(self, chat_id):
|
178 |
+
self.cursor.execute('SELECT chat_id, chat_message_id, chat_message, chat_message_is_ia, chat_message_date FROM chat_message WHERE chat_id = ?', (chat_id,))
|
179 |
+
return self.cursor.fetchall()
|
180 |
+
|
181 |
+
def select_chat_message_by_chat_message_id(self, chat_message_id):
|
182 |
+
self.cursor.execute('SELECT chat_id, chat_message_id, chat_message, chat_message_is_ia, chat_message_date FROM chat_message WHERE chat_message_id = ?', (chat_message_id,))
|
183 |
+
return self.cursor.fetchone()
|
184 |
+
|
185 |
+
def select_chat_message_by_chat_message(self, chat_message):
|
186 |
+
self.cursor.execute('SELECT chat_id, chat_message_id, chat_message, chat_message_is_ia, chat_message_date FROM chat_message WHERE chat_message = ?', (chat_message,))
|
187 |
+
return self.cursor.fetchone()
|
188 |
+
|
189 |
+
### DELETE REQUESTS
|
190 |
+
def delete_chat_message_by_chat_id(self, chat_id):
|
191 |
+
self.cursor.execute('DELETE FROM chat_message WHERE chat_id = ?', (chat_id,))
|
192 |
+
self.conn.commit()
|
193 |
+
|
194 |
+
def delete_chat_message_by_chat_message_id(self, chat_message_id):
|
195 |
+
self.cursor.execute('DELETE FROM chat_message WHERE chat_message_id = ?', (chat_message_id,))
|
196 |
+
self.conn.commit()
|
197 |
+
|
198 |
+
def delete_chat_message_by_chat_message(self, chat_message):
|
199 |
+
self.cursor.execute('DELETE FROM chat_message WHERE chat_message = ?', (chat_message,))
|
200 |
+
self.conn.commit()
|
201 |
+
|
202 |
+
### UPDATE REQUESTS
|
203 |
+
def update_chat_message_by_chat_message_id(self, chat_message_id, chat_message):
|
204 |
+
self.cursor.execute('UPDATE chat_message SET chat_message = ? WHERE chat_message_id = ?', (chat_message, chat_message_id))
|
205 |
+
self.conn.commit()
|
206 |
+
|
207 |
+
def update_chat_message_by_chat_message(self, chat_message, new_chat_message):
|
208 |
+
self.cursor.execute('UPDATE chat_message SET chat_message = ? WHERE chat_message = ?', (new_chat_message, chat_message))
|
209 |
+
self.conn.commit()
|
210 |
+
|
211 |
+
### INSERT REQUESTS
|
212 |
+
def insert_chat_message(self, chat_id, chat_message, chat_message_is_ia):
|
213 |
+
self.cursor.execute('INSERT INTO chat_message (chat_id, chat_message, chat_message_is_ia) VALUES (?, ?, ?)', (chat_id, chat_message, chat_message_is_ia))
|
214 |
+
self.conn.commit()
|
215 |
+
|
216 |
+
def select_chat_message_by_chat_id_and_user_id(self, chat_id, user_id):
|
217 |
+
self.cursor.execute('SELECT chat_message_id, chat_message, chat_message_is_ia, chat_message_date FROM chat_message JOIN chat ON chat_message.chat_id = chat.chat_id WHERE chat_message.chat_id = ? AND chat.user_id = ?', (chat_id, user_id))
|
218 |
+
return self.cursor.fetchall()
|
219 |
+
|
220 |
+
|
221 |
+
|
222 |
+
def begin_transaction(self):
|
223 |
+
self.conn.execute('BEGIN')
|
224 |
+
|
225 |
+
def commit_transaction(self):
|
226 |
+
self.conn.commit()
|
227 |
+
|
228 |
+
def rollback_transaction(self):
|
229 |
+
self.conn.rollback()
|
230 |
+
|
231 |
+
def close(self):
|
232 |
+
self.conn.close()
|
Model/IA.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
os.environ['TRANSFORMERS_CACHE'] = '/code/.cache/'
|
4 |
+
|
5 |
+
from transformers import pipeline
|
6 |
+
import torch
|
7 |
+
from googletrans import Translator
|
8 |
+
|
9 |
+
|
10 |
+
class IA:
|
11 |
+
def __init__(self, cache_dir="/code/.cache/"):
|
12 |
+
|
13 |
+
self.model_bert = pipeline("question-answering", model="DracolIA/BERT-Context-based-QA", cache_dir=cache_dir)
|
14 |
+
|
15 |
+
self.model_bigbird = pipeline("question-answering", model="DracolIA/BigBird-Roberta-Context-based-QA", cache_dir=cache_dir)
|
16 |
+
|
17 |
+
self.model_splinter = pipeline("question-answering", model="DracolIA/Splinter-Context-based-QA", cache_dir=cache_dir)
|
18 |
+
|
19 |
+
self.model_squeeze = pipeline("question-answering", model="DracolIA/SqueezeBert-Context-based-QA", cache_dir=cache_dir)
|
20 |
+
|
21 |
+
|
22 |
+
def generate_responses(self, question, file_content, have_file, selected_model):
|
23 |
+
print("file_content: ", file_content)
|
24 |
+
translator = Translator()
|
25 |
+
language = translator.detect(str(question)).lang.upper() # Verify the language of the prompt
|
26 |
+
|
27 |
+
if have_file:
|
28 |
+
context = str(file_content)
|
29 |
+
else:
|
30 |
+
context = str()
|
31 |
+
|
32 |
+
if selected_model.upper() == "BERT":
|
33 |
+
print("Choosen model: BERT")
|
34 |
+
model = self.model_bert
|
35 |
+
|
36 |
+
if selected_model.upper() == "BIGBIRD":
|
37 |
+
print("Choosen model: BIGBIRD")
|
38 |
+
model = self.model_bigbird
|
39 |
+
|
40 |
+
if selected_model.upper() == "ALBERT":
|
41 |
+
print("Choosen model: ALBERT")
|
42 |
+
model = self.model_albert
|
43 |
+
|
44 |
+
if selected_model.upper() == "SPLINTER":
|
45 |
+
print("Choosen model: SPLINTER")
|
46 |
+
model = self.model_splinter
|
47 |
+
|
48 |
+
if selected_model.upper() == "SQUEEZE":
|
49 |
+
print("Choosen model: SQUEEZE")
|
50 |
+
model = self.model_squeeze
|
51 |
+
|
52 |
+
|
53 |
+
if language != "EN":
|
54 |
+
question = translator.translate(str(question), src=language, dest="en").text # Translation of user text to english for the model
|
55 |
+
|
56 |
+
answer = model(context = context, question = question)
|
57 |
+
if answer != "" or len(answer) != 0:
|
58 |
+
answer = answer["answer"]
|
59 |
+
if language != "EN":
|
60 |
+
answer = Translator().translate(str(answer), src="en", dest=language).text # Translation of model's te*xt to user's language
|
61 |
+
|
62 |
+
print("====================> answer: ", answer)
|
63 |
+
return str(answer)
|
Model/__init__.py
ADDED
File without changes
|
README.md
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: SAE Question Answering IA Back
|
3 |
+
emoji: 🐳
|
4 |
+
colorFrom: purple
|
5 |
+
colorTo: blue
|
6 |
+
sdk: docker
|
7 |
+
app_port: 7860
|
8 |
+
---
|
Services/Service_Chat.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from Model.Database import Database
|
2 |
+
from Exceptions.UserNotConnectedException import UserNotConnectedException
|
3 |
+
from Services.Service_Chat_Message import Service_Chat_Message
|
4 |
+
|
5 |
+
class Service_Chat:
|
6 |
+
def __init__(self):
|
7 |
+
pass
|
8 |
+
|
9 |
+
def get_chat(self, chat_id, user_id, is_connected):
|
10 |
+
db = Database()
|
11 |
+
|
12 |
+
if is_connected == False:
|
13 |
+
raise UserNotConnectedException("User not connected")
|
14 |
+
|
15 |
+
try:
|
16 |
+
chat = db.select_chat_by_id_and_user_id(chat_id, user_id)
|
17 |
+
db.close()
|
18 |
+
except Exception as e:
|
19 |
+
db.close()
|
20 |
+
raise e
|
21 |
+
|
22 |
+
print(chat)
|
23 |
+
return chat
|
24 |
+
|
25 |
+
def create_chat(self, user_id, chat_title, chat_file_content, is_connected, model):
|
26 |
+
db = Database()
|
27 |
+
|
28 |
+
if is_connected == False:
|
29 |
+
raise UserNotConnectedException("User not connected")
|
30 |
+
|
31 |
+
try:
|
32 |
+
db.begin_transaction()
|
33 |
+
chat_id = db.insert_chat(user_id, chat_title, chat_file_content, model)
|
34 |
+
db.commit_transaction()
|
35 |
+
except Exception as e:
|
36 |
+
db.rollback_transaction()
|
37 |
+
finally:
|
38 |
+
db.close()
|
39 |
+
|
40 |
+
return chat_id
|
41 |
+
|
42 |
+
def update_chat_file_content(self, user_id, chat_id, chat_file_content, is_connected):
|
43 |
+
db = Database()
|
44 |
+
|
45 |
+
if is_connected == False:
|
46 |
+
raise UserNotConnectedException("User not connected")
|
47 |
+
|
48 |
+
try:
|
49 |
+
old_content = db.select_chat_by_id(chat_id)
|
50 |
+
|
51 |
+
if old_content[3] != chat_file_content:
|
52 |
+
chat_file_content = old_content[3] + " " + chat_file_content
|
53 |
+
db.update_chat_file_content_by_id(chat_id, chat_file_content)
|
54 |
+
|
55 |
+
db.close()
|
56 |
+
except Exception as e:
|
57 |
+
db.close()
|
58 |
+
raise e
|
59 |
+
|
60 |
+
def delete_chat(self, chat_id, is_connected, user_id):
|
61 |
+
db = Database()
|
62 |
+
serviceChatMessage = Service_Chat_Message()
|
63 |
+
|
64 |
+
if is_connected == False:
|
65 |
+
raise UserNotConnectedException("User not connected")
|
66 |
+
|
67 |
+
try:
|
68 |
+
serviceChatMessage.delete_chat_message(chat_id, is_connected)
|
69 |
+
db.delete_chat_by_id_and_by_user_id(chat_id, user_id)
|
70 |
+
db.close()
|
71 |
+
except Exception as e:
|
72 |
+
db.close()
|
73 |
+
raise e
|
74 |
+
|
75 |
+
def get_all_user_chats(self, user_id, is_connected):
|
76 |
+
datas={}
|
77 |
+
|
78 |
+
db = Database()
|
79 |
+
|
80 |
+
if is_connected == False:
|
81 |
+
raise UserNotConnectedException("User not connected")
|
82 |
+
|
83 |
+
try:
|
84 |
+
chats = db.select_all_user_chats(user_id)
|
85 |
+
|
86 |
+
for chat in chats:
|
87 |
+
data = {
|
88 |
+
'chat_id': chat[0],
|
89 |
+
'chat_title': chat[1],
|
90 |
+
'chat_file_content': chat[2],
|
91 |
+
'chat_date': chat[3],
|
92 |
+
'chat_model': chat[4]
|
93 |
+
}
|
94 |
+
|
95 |
+
datas.update({chat[0]: data})
|
96 |
+
|
97 |
+
db.close()
|
98 |
+
except Exception as e:
|
99 |
+
db.close()
|
100 |
+
raise e
|
101 |
+
|
102 |
+
return datas
|
103 |
+
|
Services/Service_Chat_Message.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from Model.Database import Database
|
2 |
+
from Exceptions.UserNotConnectedException import UserNotConnectedException
|
3 |
+
from datetime import datetime
|
4 |
+
|
5 |
+
class Service_Chat_Message:
|
6 |
+
def __init__(self):
|
7 |
+
pass
|
8 |
+
|
9 |
+
def get_chat_messages(self, chat_id, user_is_connected):
|
10 |
+
db = Database()
|
11 |
+
|
12 |
+
if user_is_connected == False:
|
13 |
+
raise UserNotConnectedException("User not connected")
|
14 |
+
|
15 |
+
try:
|
16 |
+
messages = db.select_chat_message_by_chat_id(chat_id)
|
17 |
+
db.close()
|
18 |
+
except Exception as e:
|
19 |
+
db.close()
|
20 |
+
raise e
|
21 |
+
|
22 |
+
return messages
|
23 |
+
|
24 |
+
def delete_chat_message(self, chat_id, user_is_connected):
|
25 |
+
db = Database()
|
26 |
+
|
27 |
+
if user_is_connected == False:
|
28 |
+
raise UserNotConnectedException("User not connected")
|
29 |
+
|
30 |
+
try:
|
31 |
+
db.delete_chat_message_by_chat_id(chat_id)
|
32 |
+
db.close()
|
33 |
+
except Exception as e:
|
34 |
+
db.close()
|
35 |
+
raise e
|
36 |
+
|
37 |
+
def add_user_chat_message(self, chat_id, chat_message, user_is_connected):
|
38 |
+
db = Database()
|
39 |
+
|
40 |
+
if user_is_connected == False:
|
41 |
+
raise UserNotConnectedException("User not connected")
|
42 |
+
|
43 |
+
try:
|
44 |
+
db.insert_chat_message(chat_id, chat_message, 0)
|
45 |
+
db.close()
|
46 |
+
except Exception as e:
|
47 |
+
db.close()
|
48 |
+
raise e
|
49 |
+
|
50 |
+
def add_ia_chat_message(self, chat_id, chat_message):
|
51 |
+
db = Database()
|
52 |
+
|
53 |
+
try:
|
54 |
+
db.insert_chat_message(chat_id, chat_message, 1)
|
55 |
+
db.close()
|
56 |
+
except Exception as e:
|
57 |
+
db.close()
|
58 |
+
raise e
|
59 |
+
|
60 |
+
def get_chat_messages_by_chat_id_and_user_id(self, chat_id, user_is_connected, user_id):
|
61 |
+
data=[]
|
62 |
+
|
63 |
+
db = Database()
|
64 |
+
|
65 |
+
if user_is_connected == False:
|
66 |
+
raise UserNotConnectedException("User not connected")
|
67 |
+
|
68 |
+
try:
|
69 |
+
messages = db.select_chat_message_by_chat_id_and_user_id(chat_id, user_id)
|
70 |
+
|
71 |
+
for message in messages:
|
72 |
+
data.append({
|
73 |
+
'chat_message_id': message[0],
|
74 |
+
'chat_message': message[1],
|
75 |
+
'chat_message_is_ia': message[2],
|
76 |
+
'chat_message_date': message[3]
|
77 |
+
})
|
78 |
+
|
79 |
+
data = sorted(data, key=lambda x: datetime.strptime(x["chat_message_date"], "%Y-%m-%d %H:%M:%S"), reverse=False)
|
80 |
+
|
81 |
+
db.close()
|
82 |
+
|
83 |
+
except Exception as e:
|
84 |
+
db.close()
|
85 |
+
raise e
|
86 |
+
|
87 |
+
return data
|
Services/Service_IA.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from Model.IA import IA
|
2 |
+
from Services.Service_Chat import Service_Chat
|
3 |
+
from Services.Service_Chat_Message import Service_Chat_Message
|
4 |
+
from Exceptions.UserNotConnectedException import UserNotConnectedException
|
5 |
+
from Exceptions.ModelNotSelectedException import ModelNotSelectedException
|
6 |
+
|
7 |
+
class Service_IA:
|
8 |
+
def __init__(self):
|
9 |
+
pass
|
10 |
+
|
11 |
+
def generate_responses(self, user_question, user_id, chat_id, user_is_connected, file_content, have_file, model):
|
12 |
+
try:
|
13 |
+
if user_is_connected == False:
|
14 |
+
raise UserNotConnectedException("User not connected")
|
15 |
+
|
16 |
+
serviceChat = Service_Chat()
|
17 |
+
serviceChatMessage = Service_Chat_Message()
|
18 |
+
ia = IA()
|
19 |
+
|
20 |
+
ia_response_json = {}
|
21 |
+
ia_response_json["chat_id"] = chat_id
|
22 |
+
ia_response_json["user_question"] = user_question
|
23 |
+
|
24 |
+
ia_response = ia.generate_responses(user_question, file_content, have_file, model)
|
25 |
+
ia_response_json["ia_response"] = ia_response
|
26 |
+
|
27 |
+
if chat_id == None:
|
28 |
+
chat_title = self.title_chat(user_question)
|
29 |
+
new_chat_id = serviceChat.create_chat(user_id, chat_title, file_content, user_is_connected, model)
|
30 |
+
ia_response_json["chat_id"] = new_chat_id
|
31 |
+
ia_response_json["chat_title"] = chat_title
|
32 |
+
chat_id = new_chat_id
|
33 |
+
else:
|
34 |
+
serviceChat.update_chat_file_content(user_id, chat_id, file_content, user_is_connected)
|
35 |
+
print("serviceChat.update_chat_file_content in Service_IA.py")
|
36 |
+
|
37 |
+
serviceChatMessage.add_user_chat_message(chat_id, user_question, user_is_connected)
|
38 |
+
serviceChatMessage.add_ia_chat_message(chat_id, str(ia_response))
|
39 |
+
except Exception as e:
|
40 |
+
raise e
|
41 |
+
|
42 |
+
return ia_response_json
|
43 |
+
|
44 |
+
|
45 |
+
def title_chat(self, chat_title):
|
46 |
+
chat_title = chat_title[:19]
|
47 |
+
chat_title += "..."
|
48 |
+
return chat_title
|
49 |
+
|
50 |
+
|
Services/Service_User.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from Model.Database import Database
|
2 |
+
from Exceptions.UserExistsException import UserExistsException
|
3 |
+
from Exceptions.UserOrPasswordIncorrectException import UserOrPasswordIncorrectException
|
4 |
+
import bcrypt
|
5 |
+
|
6 |
+
class Service_User:
|
7 |
+
|
8 |
+
def __init__(self):
|
9 |
+
pass
|
10 |
+
|
11 |
+
### Signup user
|
12 |
+
def signup_user(self, username, password):
|
13 |
+
db = Database()
|
14 |
+
|
15 |
+
if db.user_exists(username):
|
16 |
+
db.close()
|
17 |
+
raise UserExistsException("User already exists")
|
18 |
+
|
19 |
+
try:
|
20 |
+
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
|
21 |
+
|
22 |
+
db.insert_user(username, str(hashed_password))
|
23 |
+
db.close()
|
24 |
+
except Exception as e:
|
25 |
+
db.close()
|
26 |
+
raise e
|
27 |
+
|
28 |
+
return "Account created successfully"
|
29 |
+
|
30 |
+
### Login user
|
31 |
+
def login_user(self, username, password):
|
32 |
+
db = Database()
|
33 |
+
user_info = db.select_user_by_name(username)
|
34 |
+
|
35 |
+
if user_info is None:
|
36 |
+
db.close()
|
37 |
+
raise UserOrPasswordIncorrectException("User or Password incorrect.")
|
38 |
+
|
39 |
+
try:
|
40 |
+
hashed_password_from_db = user_info[2]
|
41 |
+
|
42 |
+
if hashed_password_from_db.startswith("b'") and hashed_password_from_db.endswith("'"):
|
43 |
+
hashed_password_from_db = hashed_password_from_db[2:-1].encode('utf-8')
|
44 |
+
elif isinstance(hashed_password_from_db, str):
|
45 |
+
hashed_password_from_db = hashed_password_from_db.encode('utf-8')
|
46 |
+
|
47 |
+
password_match = bcrypt.checkpw(password.encode('utf-8'), hashed_password_from_db)
|
48 |
+
|
49 |
+
db.close()
|
50 |
+
except Exception as e:
|
51 |
+
db.close()
|
52 |
+
raise e
|
53 |
+
|
54 |
+
if password_match == False:
|
55 |
+
raise UserOrPasswordIncorrectException("User or Password incorrect.")
|
56 |
+
|
57 |
+
return {
|
58 |
+
'user_id': user_info[0],
|
59 |
+
'user_name': user_info[1],
|
60 |
+
'user_is_connected': True,
|
61 |
+
'password_match': password_match
|
62 |
+
}
|
Services/__init__.py
ADDED
File without changes
|
main.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask
|
2 |
+
from Controllers.Controller_IA import prediction_blueprint
|
3 |
+
from Controllers.Controller_Database import signup_blueprint, login_blueprint, user_chat_history_blueprint, delete_chat_blueprint, chat_message_blueprint, get_chat_blueprint
|
4 |
+
from Model.IA import IA
|
5 |
+
|
6 |
+
app = Flask(__name__)
|
7 |
+
|
8 |
+
ia = IA()
|
9 |
+
|
10 |
+
app.register_blueprint(prediction_blueprint)
|
11 |
+
app.register_blueprint(signup_blueprint)
|
12 |
+
app.register_blueprint(login_blueprint)
|
13 |
+
app.register_blueprint(user_chat_history_blueprint)
|
14 |
+
app.register_blueprint(delete_chat_blueprint)
|
15 |
+
app.register_blueprint(chat_message_blueprint)
|
16 |
+
app.register_blueprint(get_chat_blueprint)
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
flask
|
2 |
+
bcrypt
|
3 |
+
transformers
|
4 |
+
torch
|
5 |
+
gunicorn
|
6 |
+
googletrans-py
|