duniele commited on
Commit
b93d070
Β·
verified Β·
1 Parent(s): b8bcd10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -8
app.py CHANGED
@@ -16,18 +16,40 @@ from langchain_huggingface import HuggingFacePipeline, HuggingFaceEmbeddings
16
  from langchain_chroma import Chroma
17
  from typing import Dict, Any, List
18
 
19
- # --- 2. UNZIP DATABASE (The Fix) ---
20
  print("⏳ Checking for Database...")
21
 
22
- # If the folder doesn't exist, but the ZIP does, unzip it!
23
- if not os.path.exists("./chroma_db") and os.path.exists("./chroma_db.zip"):
24
- print("πŸ“¦ Found chroma_db.zip! Unzipping...")
25
  with zipfile.ZipFile("./chroma_db.zip", 'r') as zip_ref:
26
  zip_ref.extractall(".")
27
  print("βœ… Unzip complete.")
28
 
29
- if not os.path.exists("./chroma_db"):
30
- raise ValueError("❌ Error: Could not find 'chroma_db' folder or 'chroma_db.zip'. Please upload the zip file.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  # --- 3. MODEL SETUP ---
33
  print("⏳ Loading Embeddings...")
@@ -36,9 +58,9 @@ embedding_function = HuggingFaceEmbeddings(
36
  model_kwargs={"trust_remote_code": True, "device": "cpu"}
37
  )
38
 
39
- print("⏳ Loading Database...")
40
  vector_db = Chroma(
41
- persist_directory="./chroma_db",
42
  embedding_function=embedding_function
43
  )
44
 
 
16
  from langchain_chroma import Chroma
17
  from typing import Dict, Any, List
18
 
19
+ # --- 2. UNZIP & AUTO-DETECT PATH ---
20
  print("⏳ Checking for Database...")
21
 
22
+ # Unzip if the zip exists
23
+ if os.path.exists("./chroma_db.zip"):
24
+ print("πŸ“¦ Found zip file! Unzipping...")
25
  with zipfile.ZipFile("./chroma_db.zip", 'r') as zip_ref:
26
  zip_ref.extractall(".")
27
  print("βœ… Unzip complete.")
28
 
29
+ # SMART DETECTION: Find where the database went
30
+ db_path = ""
31
+
32
+ if os.path.exists("./chroma_db/chroma.sqlite3"):
33
+ # Case A: It's inside the folder (Perfect)
34
+ db_path = "./chroma_db"
35
+ print(f"πŸ“‚ Found database in folder: {db_path}")
36
+
37
+ elif os.path.exists("./chroma.sqlite3"):
38
+ # Case B: It spilled into the root directory
39
+ db_path = "."
40
+ print(f"πŸ“‚ Found database in root directory: {db_path}")
41
+
42
+ elif os.path.exists("./content/chroma_db/chroma.sqlite3"):
43
+ # Case C: It's inside a 'content' folder (Common Colab issue)
44
+ db_path = "./content/chroma_db"
45
+ print(f"πŸ“‚ Found database in content folder: {db_path}")
46
+
47
+ else:
48
+ # Case D: Panic
49
+ # Let's list the files to debug
50
+ print("❌ ERROR: Cannot find chroma.sqlite3. Current files in folder:")
51
+ print(os.listdir("."))
52
+ raise ValueError("Could not find the database file after unzipping!")
53
 
54
  # --- 3. MODEL SETUP ---
55
  print("⏳ Loading Embeddings...")
 
58
  model_kwargs={"trust_remote_code": True, "device": "cpu"}
59
  )
60
 
61
+ print(f"⏳ Loading Database from {db_path}...")
62
  vector_db = Chroma(
63
+ persist_directory=db_path,
64
  embedding_function=embedding_function
65
  )
66