localsavageai commited on
Commit
b965bb0
·
verified ·
1 Parent(s): 439d179

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -21
app.py CHANGED
@@ -13,7 +13,7 @@ import requests
13
  from tqdm import tqdm
14
 
15
  # Configuration
16
- DATABASE_DIR = "semantic_memory"
17
  QWEN_API_URL = "Qwen/Qwen2.5-Max-Demo" # Gradio API for Qwen2.5 chat
18
  CHUNK_SIZE = 800
19
  TOP_K_RESULTS = 150
@@ -84,25 +84,6 @@ def split_text_into_chunks(text: str) -> List[str]:
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()) -> str:
107
  """Create a new FAISS database from uploaded file"""
108
  if password != PASSWORD_HASH:
@@ -119,6 +100,10 @@ def create_new_database(file_content: str, db_name: str, password: str, progress
119
  if os.path.exists(db_path):
120
  return f"Database '{db_name}' already exists."
121
 
 
 
 
 
122
  # Initialize embeddings and split text
123
  chunks = split_text_into_chunks(file_content)
124
  if not chunks:
@@ -139,7 +124,13 @@ def create_new_database(file_content: str, db_name: str, password: str, progress
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."
144
  except Exception as e:
145
  logging.error(f"Database creation failed: {str(e)}")
@@ -289,4 +280,10 @@ with gr.Blocks() as app:
289
  )
290
 
291
  if __name__ == "__main__":
 
 
 
 
 
 
292
  app.launch(server_name="0.0.0.0", server_port=7860)
 
13
  from tqdm import tqdm
14
 
15
  # Configuration
16
+ DATABASE_DIR = "semantic_memory" # Persistent directory for FAISS databases
17
  QWEN_API_URL = "Qwen/Qwen2.5-Max-Demo" # Gradio API for Qwen2.5 chat
18
  CHUNK_SIZE = 800
19
  TOP_K_RESULTS = 150
 
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:
 
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)
105
+ logging.info(f"Created database directory: {db_path}")
106
+
107
  # Initialize embeddings and split text
108
  chunks = split_text_into_chunks(file_content)
109
  if not chunks:
 
124
  embedding=embeddings
125
  )
126
  vector_store.save_local(db_path)
127
+ logging.info(f"FAISS database saved to: {db_path}")
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)}")
 
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)