Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 20 |
print("β³ Checking for Database...")
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
if
|
| 24 |
-
print("π¦ Found
|
| 25 |
with zipfile.ZipFile("./chroma_db.zip", 'r') as zip_ref:
|
| 26 |
zip_ref.extractall(".")
|
| 27 |
print("β
Unzip complete.")
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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=
|
| 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 |
|