farmax commited on
Commit
dda91f4
·
verified ·
1 Parent(s): 1eff01a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -14
app.py CHANGED
@@ -22,7 +22,7 @@ import tqdm
22
  import accelerate
23
  import re
24
 
25
-
26
 
27
  # default_persist_directory = './chroma_HF/'
28
  list_llm = ["mistralai/Mistral-7B-Instruct-v0.2", "mistralai/Mixtral-8x7B-Instruct-v0.1", "mistralai/Mistral-7B-Instruct-v0.1", \
@@ -207,23 +207,34 @@ def create_collection_name(filepath):
207
  print('Collection name: ', collection_name)
208
  return collection_name
209
 
210
-
211
- # Initialize database
212
  def initialize_database(list_file_obj, chunk_size, chunk_overlap, progress=gr.Progress()):
 
 
 
 
 
 
 
 
 
 
213
  # Create list of documents (when valid)
214
  list_file_path = [x.name for x in list_file_obj if x is not None]
 
215
  # Create collection_name for vector database
216
- progress(0.1, desc="Creating collection name...")
217
  collection_name = create_collection_name(list_file_path[0])
218
- progress(0.25, desc="Loading document...")
 
219
  # Load document and create splits
220
  doc_splits = load_doc(list_file_path, chunk_size, chunk_overlap)
 
221
  # Create or load vector database
222
  progress(0.5, desc="Generating vector database...")
223
- # global vector_db
224
  vector_db = create_db(doc_splits, collection_name)
225
- progress(0.9, desc="Done!")
226
- return vector_db, collection_name, "Complete!"
227
 
228
  def initialize_LLM(llm_option, llm_temperature, max_tokens, top_k, vector_db, progress=gr.Progress()):
229
  # print("llm_option",llm_option)
@@ -244,7 +255,7 @@ def format_chat_history(message, chat_history):
244
 
245
  def conversation(qa_chain, message, history):
246
  formatted_chat_history = format_chat_history(message, history)
247
- #print("formatted_chat_history",formatted_chat_history)
248
 
249
  # Generate response using QA chain
250
  response = qa_chain({"question": message, "chat_history": formatted_chat_history})
@@ -259,8 +270,8 @@ def conversation(qa_chain, message, history):
259
  response_source1_page = response_sources[0].metadata["page"] + 1
260
  response_source2_page = response_sources[1].metadata["page"] + 1
261
  response_source3_page = response_sources[2].metadata["page"] + 1
262
- # print ('chat response: ', response_answer)
263
- # print('DB source', response_sources)
264
 
265
  # Append user message and response to chat history
266
  new_history = history + [(message, response_answer)]
@@ -310,9 +321,9 @@ def demo():
310
  with gr.Row():
311
  db_progress = gr.Textbox(label="Vector database initialization", value="None")
312
  with gr.Row():
313
- db_btn = gr.Button("Generate vector database")
314
 
315
- with gr.Tab("Step 3 - Initializia QA chain"):
316
  with gr.Row():
317
  llm_btn = gr.Radio(list_llm_simple, \
318
  label="LLM models", value = list_llm_simple[0], type="index", info="Scegli il tuo modello LLM")
@@ -328,7 +339,7 @@ def demo():
328
  with gr.Row():
329
  llm_progress = gr.Textbox(value="None",label="QA chain initialization")
330
  with gr.Row():
331
- qachain_btn = gr.Button("Initializza Question Answering chain")
332
 
333
 
334
  with gr.Tab("Passo 4 - Chatbot"):
 
22
  import accelerate
23
  import re
24
 
25
+ from chromadb.utils import get_default_config
26
 
27
  # default_persist_directory = './chroma_HF/'
28
  list_llm = ["mistralai/Mistral-7B-Instruct-v0.2", "mistralai/Mixtral-8x7B-Instruct-v0.1", "mistralai/Mistral-7B-Instruct-v0.1", \
 
207
  print('Collection name: ', collection_name)
208
  return collection_name
209
 
210
+ # Inizializzazione database
 
211
  def initialize_database(list_file_obj, chunk_size, chunk_overlap, progress=gr.Progress()):
212
+ # Check if a database already exists
213
+ try:
214
+ # Get the current configuration
215
+ config = get_default_config()
216
+ # Delete the existing database
217
+ chromadb.delete(config)
218
+ print("Existing database deleted successfully.")
219
+ except Exception as e:
220
+ print(f"Error deleting existing database: {str(e)}")
221
+
222
  # Create list of documents (when valid)
223
  list_file_path = [x.name for x in list_file_obj if x is not None]
224
+
225
  # Create collection_name for vector database
226
+ progress(0.1, desc="Creazione collection name...")
227
  collection_name = create_collection_name(list_file_path[0])
228
+ progress(0.25, desc="Caricamento documenti..")
229
+
230
  # Load document and create splits
231
  doc_splits = load_doc(list_file_path, chunk_size, chunk_overlap)
232
+
233
  # Create or load vector database
234
  progress(0.5, desc="Generating vector database...")
 
235
  vector_db = create_db(doc_splits, collection_name)
236
+ progress(0.9, desc="Fatto!")
237
+ return vector_db, collection_name, "Completato!"
238
 
239
  def initialize_LLM(llm_option, llm_temperature, max_tokens, top_k, vector_db, progress=gr.Progress()):
240
  # print("llm_option",llm_option)
 
255
 
256
  def conversation(qa_chain, message, history):
257
  formatted_chat_history = format_chat_history(message, history)
258
+ print("formatted_chat_history",formatted_chat_history)
259
 
260
  # Generate response using QA chain
261
  response = qa_chain({"question": message, "chat_history": formatted_chat_history})
 
270
  response_source1_page = response_sources[0].metadata["page"] + 1
271
  response_source2_page = response_sources[1].metadata["page"] + 1
272
  response_source3_page = response_sources[2].metadata["page"] + 1
273
+ print ('chat response: ', response_answer)
274
+ print('DB source', response_sources)
275
 
276
  # Append user message and response to chat history
277
  new_history = history + [(message, response_answer)]
 
321
  with gr.Row():
322
  db_progress = gr.Textbox(label="Vector database initialization", value="None")
323
  with gr.Row():
324
+ db_btn = gr.Button("Genera vector database")
325
 
326
+ with gr.Tab("Step 3 - Inizializza QA chain"):
327
  with gr.Row():
328
  llm_btn = gr.Radio(list_llm_simple, \
329
  label="LLM models", value = list_llm_simple[0], type="index", info="Scegli il tuo modello LLM")
 
339
  with gr.Row():
340
  llm_progress = gr.Textbox(value="None",label="QA chain initialization")
341
  with gr.Row():
342
+ qachain_btn = gr.Button("Inizializza Question Answering chain")
343
 
344
 
345
  with gr.Tab("Passo 4 - Chatbot"):