localsavageai commited on
Commit
5c262f5
·
verified ·
1 Parent(s): b965bb0

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -24
app.py CHANGED
@@ -84,21 +84,40 @@ def split_text_into_chunks(text: str) -> List[str]:
84
 
85
  return chunks
86
 
87
- def create_new_database(file_content: str, db_name: str, password: str, progress=gr.Progress()) -> str:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  """Create a new FAISS database from uploaded file"""
89
  if password != PASSWORD_HASH:
90
- return "Incorrect password. Database creation failed."
91
 
92
  if not file_content.strip():
93
- return "Uploaded file is empty. Database creation failed."
94
 
95
  if not db_name.isalnum():
96
- return "Database name must be alphanumeric. Database creation failed."
97
 
98
  try:
99
  db_path = os.path.join(DATABASE_DIR, db_name)
100
  if os.path.exists(db_path):
101
- return f"Database '{db_name}' already exists."
102
 
103
  # Create the database directory
104
  os.makedirs(db_path, exist_ok=True)
@@ -107,7 +126,7 @@ def create_new_database(file_content: str, db_name: str, password: str, progress
107
  # Initialize embeddings and split text
108
  chunks = split_text_into_chunks(file_content)
109
  if not chunks:
110
- return "No valid chunks generated. Database creation failed."
111
 
112
  logging.info(f"Creating {len(chunks)} chunks...")
113
  progress(0, desc="Starting embedding process...")
@@ -128,13 +147,16 @@ def create_new_database(file_content: str, db_name: str, password: str, progress
128
 
129
  # Verify files were created
130
  if not os.listdir(db_path):
131
- return f"Failed to save FAISS database files in: {db_path}"
132
  logging.info(f"FAISS database files: {os.listdir(db_path)}")
133
 
134
- return f"Database '{db_name}' created successfully."
 
 
 
135
  except Exception as e:
136
  logging.error(f"Database creation failed: {str(e)}")
137
- return f"Error creating database: {str(e)}"
138
 
139
  def generate_response(user_input: str, db_name: str) -> Optional[str]:
140
  """Generate response using Qwen2.5 MAX"""
@@ -212,7 +234,7 @@ with gr.Blocks() as app:
212
  def update_db_list():
213
  """Update the list of available databases"""
214
  if not os.path.exists(DATABASE_DIR):
215
- return []
216
  return [name for name in os.listdir(DATABASE_DIR) if os.path.isdir(os.path.join(DATABASE_DIR, name))]
217
 
218
  with gr.Tab("Create Database"):
@@ -225,7 +247,7 @@ with gr.Blocks() as app:
225
 
226
  def handle_create(file, db_name, password, progress=gr.Progress()):
227
  if not file or not db_name or not password:
228
- return "Please provide all required inputs."
229
 
230
  # Check if the file is valid
231
  if isinstance(file, str): # Gradio provides the file path as a string
@@ -233,15 +255,12 @@ with gr.Blocks() as app:
233
  with open(file, "r", encoding="utf-8") as f:
234
  file_content = f.read()
235
  except Exception as e:
236
- return f"Error reading file: {str(e)}"
237
  else:
238
- return "Invalid file format. Please upload a .txt file."
239
 
240
- result = create_new_database(file_content, db_name, password, progress)
241
- if "created successfully" in result:
242
- # Update the database list
243
- return result, update_db_list()
244
- return result, None
245
 
246
  create_button.click(
247
  handle_create,
@@ -280,10 +299,4 @@ with gr.Blocks() as app:
280
  )
281
 
282
  if __name__ == "__main__":
283
- # Ensure DATABASE_DIR exists
284
- if not os.path.exists(DATABASE_DIR):
285
- logging.info(f"Creating persistent DATABASE_DIR: {DATABASE_DIR}")
286
- os.makedirs(DATABASE_DIR, exist_ok=True)
287
- logging.info(f"Contents of DATABASE_DIR: {os.listdir(DATABASE_DIR)}")
288
-
289
  app.launch(server_name="0.0.0.0", server_port=7860)
 
84
 
85
  return chunks
86
 
87
+ def initialize_vector_store(embeddings: Embeddings, db_name: str) -> FAISS:
88
+ """Initialize or load a FAISS vector store"""
89
+ db_path = os.path.join(DATABASE_DIR, db_name)
90
+ if os.path.exists(db_path):
91
+ try:
92
+ logging.info(f"Loading existing database: {db_name}")
93
+ return FAISS.load_local(
94
+ db_path,
95
+ embeddings,
96
+ allow_dangerous_deserialization=True
97
+ )
98
+ except Exception as e:
99
+ logging.error(f"FAISS load error: {str(e)}")
100
+ raise
101
+
102
+ logging.info(f"Creating new vector database: {db_name}")
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()) -> Tuple[str, List[str]]:
107
  """Create a new FAISS database from uploaded file"""
108
  if password != PASSWORD_HASH:
109
+ return "Incorrect password. Database creation failed.", []
110
 
111
  if not file_content.strip():
112
+ return "Uploaded file is empty. Database creation failed.", []
113
 
114
  if not db_name.isalnum():
115
+ return "Database name must be alphanumeric. Database creation failed.", []
116
 
117
  try:
118
  db_path = os.path.join(DATABASE_DIR, db_name)
119
  if os.path.exists(db_path):
120
+ return f"Database '{db_name}' already exists.", []
121
 
122
  # Create the database directory
123
  os.makedirs(db_path, exist_ok=True)
 
126
  # Initialize embeddings and split text
127
  chunks = split_text_into_chunks(file_content)
128
  if not chunks:
129
+ return "No valid chunks generated. Database creation failed.", []
130
 
131
  logging.info(f"Creating {len(chunks)} chunks...")
132
  progress(0, desc="Starting embedding process...")
 
147
 
148
  # Verify files were created
149
  if not os.listdir(db_path):
150
+ return f"Failed to save FAISS database files in: {db_path}", []
151
  logging.info(f"FAISS database files: {os.listdir(db_path)}")
152
 
153
+ # Update the list of available databases
154
+ db_list = [name for name in os.listdir(DATABASE_DIR) if os.path.isdir(os.path.join(DATABASE_DIR, name))]
155
+ return f"Database '{db_name}' created successfully.", db_list
156
+
157
  except Exception as e:
158
  logging.error(f"Database creation failed: {str(e)}")
159
+ return f"Error creating database: {str(e)}", []
160
 
161
  def generate_response(user_input: str, db_name: str) -> Optional[str]:
162
  """Generate response using Qwen2.5 MAX"""
 
234
  def update_db_list():
235
  """Update the list of available databases"""
236
  if not os.path.exists(DATABASE_DIR):
237
+ os.makedirs(DATABASE_DIR, exist_ok=True)
238
  return [name for name in os.listdir(DATABASE_DIR) if os.path.isdir(os.path.join(DATABASE_DIR, name))]
239
 
240
  with gr.Tab("Create Database"):
 
247
 
248
  def handle_create(file, db_name, password, progress=gr.Progress()):
249
  if not file or not db_name or not password:
250
+ return "Please provide all required inputs.", []
251
 
252
  # Check if the file is valid
253
  if isinstance(file, str): # Gradio provides the file path as a string
 
255
  with open(file, "r", encoding="utf-8") as f:
256
  file_content = f.read()
257
  except Exception as e:
258
+ return f"Error reading file: {str(e)}", []
259
  else:
260
+ return "Invalid file format. Please upload a .txt file.", []
261
 
262
+ result, db_list = create_new_database(file_content, db_name, password, progress)
263
+ return result, db_list
 
 
 
264
 
265
  create_button.click(
266
  handle_create,
 
299
  )
300
 
301
  if __name__ == "__main__":
 
 
 
 
 
 
302
  app.launch(server_name="0.0.0.0", server_port=7860)