-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_chatmodel.py
306 lines (247 loc) · 10.5 KB
/
upload_chatmodel.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
import mysql.connector
from mysql.connector import Error
from flask import Flask, request, jsonify
from flask_cors import CORS
import os
from langchain_community.vectorstores import FAISS
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains import ConversationalRetrievalChain
from langchain_community.document_loaders import PyPDFLoader, DirectoryLoader
from langchain_community.llms import CTransformers
from langchain.memory import ConversationBufferMemory
from langchain.chains import RetrievalQAWithSourcesChain
from langchain.prompts.chat import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
)
app = Flask(__name__)
CORS(app)
def create_connection():
connection = None
try:
connection = mysql.connector.connect(
host='localhost',
database='mydb',
user='laha',
password='Laha_2002'
)
print("Connected to MySQL database")
except Error as e:
print(f"Error connecting to MySQL database: {e}")
return connection
UPLOAD_FOLDER = 'books'
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
class PDFChatBot:
def __init__(self):
self.data_path = os.path.join('books')
self.db_faiss_path = os.path.join('vector', 'db_faiss')
def create_vector_db(self):
loader = DirectoryLoader(self.data_path,
glob='*.pdf',
loader_cls=PyPDFLoader)
documents = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=400,
chunk_overlap=50)
texts = text_splitter.split_documents(documents)
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2',
model_kwargs={'device': 'cpu'})
db = FAISS.from_documents(texts, embeddings)
db.save_local(self.db_faiss_path)
def load_llm(self):
llm = CTransformers(
model="./models/llama-2-7b-chat.ggmlv3.q8_0.bin",
model_type="llama",
max_new_tokens=2000,
temperature=0.5
)
return llm
def conversational_chain(self):
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2",
model_kwargs={'device': 'cpu'})
db = FAISS.load_local(self.db_faiss_path, embeddings,allow_dangerous_deserialization=True)
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
conversational_chain = ConversationalRetrievalChain.from_llm(llm=self.load_llm(),
retriever=db.as_retriever(search_kwargs={"k": 3}),
verbose=True,
memory=memory
)
return conversational_chain
chatbot = None
@app.route('/ask', methods=['POST'])
def ask_question():
global chatbot
if chatbot is None:
return jsonify({'error': 'Chatbot not initialized. Please initialize first.'}), 500
data = request.json
print("Received data:", data)
if 'question' not in data:
return jsonify({'error': 'Question not provided.'}), 400
question = data['question']
result = chatbot.conversational_chain()({"question": question})
answer = result.get('answer', '')
# Save the question and answer to the history table
connection = create_connection()
if connection:
try:
cursor = connection.cursor()
sql_insert_query = "INSERT INTO history (query, answer) VALUES (%s, %s)"
cursor.execute(sql_insert_query, (question, answer))
connection.commit()
print("Chat history saved to MySQL database")
except Error as e:
print(f"Error inserting chat history into MySQL database: {e}")
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection closed")
serializable_result = {
'answer': answer,
'source_documents': result.get('source_documents', []),
}
return jsonify(serializable_result)
@app.route('/initialize', methods=['POST'])
def initialize_chatbot():
global chatbot
print("Wait for a minute.")
chatbot = PDFChatBot()
chatbot.create_vector_db()
return jsonify({'message': 'Chatbot initialized successfully!'})
@app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return jsonify({'error': 'No file part'})
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'})
file_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(file_path)
connection = create_connection()
if connection:
try:
cursor = connection.cursor()
# Check if the file name already exists in the database
sql_check_query = "SELECT COUNT(*) FROM books WHERE book_path = %s"
cursor.execute(sql_check_query, (file_path,))
count = cursor.fetchone()[0]
if count == 0:
# Insert the file path into the database if it doesn't already exist
sql_insert_query = "INSERT INTO books (book_path) VALUES (%s)"
cursor.execute(sql_insert_query, (file_path,))
connection.commit()
message = "File uploaded and path saved to MySQL database"
else:
message = "File already exists in the database"
print(message)
except Error as e:
print(f"Error inserting file path into MySQL database: {e}")
return jsonify({'error': 'Error inserting file path into MySQL database'}), 500
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection closed")
else:
print("Failed to connect to MySQL database")
return jsonify({'error': 'Failed to connect to MySQL database'}), 500
return jsonify({'message': message})
@app.route('/pdf-files', methods=['GET'])
def get_pdf_files():
connection = create_connection()
pdf_files = []
if connection:
try:
cursor = connection.cursor()
sql_query = "SELECT book_path FROM books"
cursor.execute(sql_query)
pdf_files = [row[0] for row in cursor.fetchall()]
except Error as e:
print(f"Error fetching PDF files from MySQL database: {e}")
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection closed")
return jsonify(pdf_files)
@app.route('/chat-history', methods=['GET'])
def get_chat_history():
connection = create_connection()
chat_history = []
if connection:
try:
cursor = connection.cursor()
sql_query = "SELECT id, query, answer FROM history ORDER BY id DESC"
cursor.execute(sql_query)
result = cursor.fetchall()
chat_history = [{'id': row[0], 'query': row[1], 'answer': row[2]} for row in result]
except Error as e:
print(f"Error fetching chat history from MySQL database: {e}")
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection closed")
return jsonify(chat_history)
@app.route('/remove-pdf/<file_name>', methods=['DELETE'])
def remove_pdf(file_name):
try:
# Construct the absolute file path
file_path = os.path.join(app.config['UPLOAD_FOLDER'], file_name)
# Check if the file exists
if os.path.exists(file_path):
# Remove the file
os.remove(file_path)
print(f"PDF file removed: {file_path}")
else:
print(f"PDF file not found: {file_path}")
return jsonify({'error': 'PDF file not found'}), 404
# Remove the record from the database
connection = create_connection()
if connection:
try:
cursor = connection.cursor()
sql_query = "DELETE FROM books WHERE book_path = %s"
cursor.execute(sql_query, (file_path,))
connection.commit()
print("PDF file record removed from MySQL database")
except Error as e:
print(f"Error removing PDF file record from MySQL database: {e}")
return jsonify({'error': 'Failed to remove PDF file record from database'}), 500
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection closed")
else:
return jsonify({'error': 'Failed to connect to MySQL database'}), 500
return jsonify({'message': 'PDF file removed successfully'})
except Exception as e:
print(f"Error removing PDF file: {e}")
return jsonify({'error': 'Failed to remove PDF file'}), 500
@app.route('/remove-history/<int:history_id>', methods=['DELETE'])
def remove_chat_history(history_id):
connection = create_connection()
if connection:
try:
cursor = connection.cursor()
sql_query = "DELETE FROM history WHERE id = %s"
cursor.execute(sql_query, (history_id,))
connection.commit()
print("Chat history entry removed from MySQL database")
return jsonify({'message': 'Chat history entry removed successfully'})
except Error as e:
print(f"Error removing chat history entry from MySQL database: {e}")
return jsonify({'error': 'Failed to remove chat history entry'}), 500
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection closed")
else:
return jsonify({'error': 'Failed to connect to MySQL database'}), 500
if __name__ == '__main__':
app.run(debug=True)