localsavageai commited on
Commit
d59a8ff
·
verified ·
1 Parent(s): c2715c9

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -4
app.py CHANGED
@@ -103,7 +103,7 @@ def initialize_vector_store(embeddings: Embeddings, db_name: str) -> FAISS:
103
  os.makedirs(db_path, exist_ok=True)
104
  return None
105
 
106
- def create_new_database(file_content: str, db_name: str, password: str) -> str:
107
  """Create a new FAISS database from uploaded file"""
108
  if password != PASSWORD_HASH:
109
  return "Incorrect password. Database creation failed."
@@ -125,7 +125,19 @@ def create_new_database(file_content: str, db_name: str, password: str) -> str:
125
  return "No valid chunks generated. Database creation failed."
126
 
127
  logging.info(f"Creating {len(chunks)} chunks...")
128
- vector_store = FAISS.from_texts(chunks, embeddings)
 
 
 
 
 
 
 
 
 
 
 
 
129
  vector_store.save_local(db_path)
130
  logging.info(f"Vector store '{db_name}' initialized successfully")
131
  return f"Database '{db_name}' created successfully."
@@ -208,7 +220,7 @@ with gr.Blocks() as app:
208
  create_output = gr.Textbox(label="Status")
209
  create_button = gr.Button("Create Database")
210
 
211
- def handle_create(file, db_name, password):
212
  if not file or not db_name or not password:
213
  return "Please provide all required inputs."
214
 
@@ -222,7 +234,7 @@ with gr.Blocks() as app:
222
  else:
223
  return "Invalid file format. Please upload a .txt file."
224
 
225
- return create_new_database(file_content, db_name, password)
226
 
227
  create_button.click(
228
  handle_create,
@@ -253,6 +265,7 @@ with gr.Blocks() as app:
253
  queue=True
254
  )
255
 
 
256
  db_select.choices = update_db_list()
257
 
258
  if __name__ == "__main__":
 
103
  os.makedirs(db_path, exist_ok=True)
104
  return None
105
 
106
+ def create_new_database(file_content: str, db_name: str, password: str, progress=gr.Progress()) -> str:
107
  """Create a new FAISS database from uploaded file"""
108
  if password != PASSWORD_HASH:
109
  return "Incorrect password. Database creation failed."
 
125
  return "No valid chunks generated. Database creation failed."
126
 
127
  logging.info(f"Creating {len(chunks)} chunks...")
128
+ progress(0, desc="Starting embedding process...")
129
+
130
+ # Create embeddings with progress tracking
131
+ embeddings_list = []
132
+ for i, chunk in enumerate(chunks):
133
+ progress(i / len(chunks), desc=f"Embedding chunk {i+1}/{len(chunks)}")
134
+ embeddings_list.append(embeddings.embed_query(chunk))
135
+
136
+ # Create FAISS database
137
+ vector_store = FAISS.from_embeddings(
138
+ text_embeddings=list(zip(chunks, embeddings_list)),
139
+ embedding=embeddings
140
+ )
141
  vector_store.save_local(db_path)
142
  logging.info(f"Vector store '{db_name}' initialized successfully")
143
  return f"Database '{db_name}' created successfully."
 
220
  create_output = gr.Textbox(label="Status")
221
  create_button = gr.Button("Create Database")
222
 
223
+ def handle_create(file, db_name, password, progress=gr.Progress()):
224
  if not file or not db_name or not password:
225
  return "Please provide all required inputs."
226
 
 
234
  else:
235
  return "Invalid file format. Please upload a .txt file."
236
 
237
+ return create_new_database(file_content, db_name, password, progress)
238
 
239
  create_button.click(
240
  handle_create,
 
265
  queue=True
266
  )
267
 
268
+ # Update database list on page load
269
  db_select.choices = update_db_list()
270
 
271
  if __name__ == "__main__":