Spaces:
Paused
Paused
Update src/enhanced_rag_system.py
Browse files- src/enhanced_rag_system.py +26 -4
src/enhanced_rag_system.py
CHANGED
|
@@ -1,4 +1,12 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import plotly.express as px
|
| 4 |
import plotly.graph_objects as go
|
|
@@ -108,17 +116,21 @@ class EnhancedRAGSystem:
|
|
| 108 |
try:
|
| 109 |
print("π§ Initializing ChromaDB...")
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
# Try different ChromaDB configurations for HuggingFace compatibility
|
| 112 |
try:
|
| 113 |
# First try: PersistentClient (newer API)
|
| 114 |
-
self.chroma_client = chromadb.PersistentClient(path=
|
| 115 |
print("β
Using ChromaDB PersistentClient")
|
| 116 |
except Exception as e1:
|
| 117 |
try:
|
| 118 |
# Second try: Client with settings (older API)
|
| 119 |
self.chroma_client = chromadb.Client(Settings(
|
| 120 |
chroma_db_impl="duckdb+parquet",
|
| 121 |
-
persist_directory=
|
| 122 |
))
|
| 123 |
print("β
Using ChromaDB Client with Settings")
|
| 124 |
except Exception as e2:
|
|
@@ -140,12 +152,22 @@ class EnhancedRAGSystem:
|
|
| 140 |
|
| 141 |
# Initialize embedding model with smaller model for HuggingFace
|
| 142 |
try:
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
print("β
Loaded sentence transformer model: all-MiniLM-L6-v2")
|
| 145 |
except Exception as e:
|
| 146 |
# Fallback to even smaller model
|
| 147 |
try:
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
print("β
Loaded fallback sentence transformer model: paraphrase-MiniLM-L3-v2")
|
| 150 |
except Exception as e2:
|
| 151 |
print(f"β Failed to load embedding model: {e2}")
|
|
|
|
| 1 |
import os
|
| 2 |
+
import tempfile
|
| 3 |
+
|
| 4 |
+
# Fix HuggingFace cache directory issue for HuggingFace Spaces
|
| 5 |
+
# Set cache directories to writable temporary directories
|
| 6 |
+
os.environ['TRANSFORMERS_CACHE'] = tempfile.mkdtemp()
|
| 7 |
+
os.environ['HF_HOME'] = tempfile.mkdtemp()
|
| 8 |
+
os.environ['SENTENCE_TRANSFORMERS_HOME'] = tempfile.mkdtemp()
|
| 9 |
+
|
| 10 |
import pandas as pd
|
| 11 |
import plotly.express as px
|
| 12 |
import plotly.graph_objects as go
|
|
|
|
| 116 |
try:
|
| 117 |
print("π§ Initializing ChromaDB...")
|
| 118 |
|
| 119 |
+
# Create a writable directory for ChromaDB
|
| 120 |
+
chroma_dir = tempfile.mkdtemp(prefix="chroma_")
|
| 121 |
+
print(f"π Using ChromaDB directory: {chroma_dir}")
|
| 122 |
+
|
| 123 |
# Try different ChromaDB configurations for HuggingFace compatibility
|
| 124 |
try:
|
| 125 |
# First try: PersistentClient (newer API)
|
| 126 |
+
self.chroma_client = chromadb.PersistentClient(path=chroma_dir)
|
| 127 |
print("β
Using ChromaDB PersistentClient")
|
| 128 |
except Exception as e1:
|
| 129 |
try:
|
| 130 |
# Second try: Client with settings (older API)
|
| 131 |
self.chroma_client = chromadb.Client(Settings(
|
| 132 |
chroma_db_impl="duckdb+parquet",
|
| 133 |
+
persist_directory=chroma_dir
|
| 134 |
))
|
| 135 |
print("β
Using ChromaDB Client with Settings")
|
| 136 |
except Exception as e2:
|
|
|
|
| 152 |
|
| 153 |
# Initialize embedding model with smaller model for HuggingFace
|
| 154 |
try:
|
| 155 |
+
# Set cache directory for sentence transformers
|
| 156 |
+
cache_dir = tempfile.mkdtemp(prefix="sentence_transformers_")
|
| 157 |
+
|
| 158 |
+
self.embedding_model = SentenceTransformer(
|
| 159 |
+
"all-MiniLM-L6-v2",
|
| 160 |
+
cache_folder=cache_dir
|
| 161 |
+
)
|
| 162 |
print("β
Loaded sentence transformer model: all-MiniLM-L6-v2")
|
| 163 |
except Exception as e:
|
| 164 |
# Fallback to even smaller model
|
| 165 |
try:
|
| 166 |
+
cache_dir = tempfile.mkdtemp(prefix="sentence_transformers_fallback_")
|
| 167 |
+
self.embedding_model = SentenceTransformer(
|
| 168 |
+
"paraphrase-MiniLM-L3-v2",
|
| 169 |
+
cache_folder=cache_dir
|
| 170 |
+
)
|
| 171 |
print("β
Loaded fallback sentence transformer model: paraphrase-MiniLM-L3-v2")
|
| 172 |
except Exception as e2:
|
| 173 |
print(f"β Failed to load embedding model: {e2}")
|