Spaces:
Sleeping
Sleeping
Update back.py
Browse files
back.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import os
|
2 |
import logging
|
3 |
from typing import List, Dict, Any, Optional, Union
|
@@ -9,6 +10,7 @@ from langchain_core.embeddings import Embeddings
|
|
9 |
import google.generativeai as genai
|
10 |
from datetime import datetime
|
11 |
import json
|
|
|
12 |
|
13 |
@dataclass
|
14 |
class UserInfo:
|
@@ -26,10 +28,11 @@ class ChatConfig:
|
|
26 |
"""Configuration for the chatbot"""
|
27 |
embedding_model_name: str = 'all-MiniLM-L6-v2'
|
28 |
device: str = 'cuda' if torch.cuda.is_available() else 'cpu'
|
29 |
-
max_history: int =
|
30 |
gemini_api_key: str = os.getenv("GEMINI_API") # Replace with your API key
|
31 |
log_file: str = "chat_history.txt"
|
32 |
user_data_file: str = "user_data.json"
|
|
|
33 |
|
34 |
# In the UserManager class, modify these methods:
|
35 |
class UserManager:
|
@@ -178,7 +181,7 @@ class GeminiRAG:
|
|
178 |
"max_output_tokens": 8192,
|
179 |
}
|
180 |
self.model = genai.GenerativeModel(
|
181 |
-
model_name="gemini-
|
182 |
generation_config=self.generation_config,
|
183 |
safety_settings={'HATE': 'BLOCK_NONE','HARASSMENT': 'BLOCK_NONE','SEXUAL' : 'BLOCK_NONE','DANGEROUS' : 'BLOCK_NONE'}
|
184 |
)
|
@@ -253,7 +256,23 @@ class ProductDatabase:
|
|
253 |
device=config.device
|
254 |
)
|
255 |
self.vectorstore = None
|
|
|
|
|
256 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
257 |
def process_markdown(self, markdown_content: str):
|
258 |
"""Process markdown content and create vector store"""
|
259 |
try:
|
@@ -278,16 +297,19 @@ class ProductDatabase:
|
|
278 |
texts = [doc["content"] for doc in documents]
|
279 |
metadatas = [{"section": doc["section"]} for doc in documents]
|
280 |
|
281 |
-
self.vectorstore
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
|
|
|
|
|
|
286 |
|
287 |
except Exception as e:
|
288 |
raise Exception(f"Error processing markdown content: {str(e)}")
|
289 |
|
290 |
-
def search(self, query: str, k: int =
|
291 |
"""Search for relevant documents"""
|
292 |
if not self.vectorstore:
|
293 |
raise ValueError("Database not initialized. Please process documents first.")
|
|
|
1 |
+
# back.py
|
2 |
import os
|
3 |
import logging
|
4 |
from typing import List, Dict, Any, Optional, Union
|
|
|
10 |
import google.generativeai as genai
|
11 |
from datetime import datetime
|
12 |
import json
|
13 |
+
import pickle
|
14 |
|
15 |
@dataclass
|
16 |
class UserInfo:
|
|
|
28 |
"""Configuration for the chatbot"""
|
29 |
embedding_model_name: str = 'all-MiniLM-L6-v2'
|
30 |
device: str = 'cuda' if torch.cuda.is_available() else 'cpu'
|
31 |
+
max_history: int = 6
|
32 |
gemini_api_key: str = os.getenv("GEMINI_API") # Replace with your API key
|
33 |
log_file: str = "chat_history.txt"
|
34 |
user_data_file: str = "user_data.json"
|
35 |
+
database_file: str = "faiss_db.pkl" # Added database file path
|
36 |
|
37 |
# In the UserManager class, modify these methods:
|
38 |
class UserManager:
|
|
|
181 |
"max_output_tokens": 8192,
|
182 |
}
|
183 |
self.model = genai.GenerativeModel(
|
184 |
+
model_name="gemini-2.0-flash-exp",
|
185 |
generation_config=self.generation_config,
|
186 |
safety_settings={'HATE': 'BLOCK_NONE','HARASSMENT': 'BLOCK_NONE','SEXUAL' : 'BLOCK_NONE','DANGEROUS' : 'BLOCK_NONE'}
|
187 |
)
|
|
|
256 |
device=config.device
|
257 |
)
|
258 |
self.vectorstore = None
|
259 |
+
self.config = config
|
260 |
+
self.load_database()
|
261 |
|
262 |
+
def load_database(self):
|
263 |
+
"""Loads the FAISS database from file"""
|
264 |
+
try:
|
265 |
+
if os.path.exists(self.config.database_file):
|
266 |
+
with open(self.config.database_file, "rb") as f:
|
267 |
+
self.vectorstore = pickle.load(f)
|
268 |
+
print("Database loaded successfully from file.")
|
269 |
+
else:
|
270 |
+
print("Database file not found. Please run setup.py to create it.")
|
271 |
+
except Exception as e:
|
272 |
+
logging.error(f"Error loading database: {str(e)}")
|
273 |
+
print(f"Error loading database: {str(e)}")
|
274 |
+
self.vectorstore = None
|
275 |
+
|
276 |
def process_markdown(self, markdown_content: str):
|
277 |
"""Process markdown content and create vector store"""
|
278 |
try:
|
|
|
297 |
texts = [doc["content"] for doc in documents]
|
298 |
metadatas = [{"section": doc["section"]} for doc in documents]
|
299 |
|
300 |
+
if self.vectorstore is None:
|
301 |
+
self.vectorstore = FAISS.from_texts(
|
302 |
+
texts=texts,
|
303 |
+
embedding=self.embeddings,
|
304 |
+
metadatas=metadatas
|
305 |
+
)
|
306 |
+
else:
|
307 |
+
self.vectorstore.add_texts(texts=texts, metadatas=metadatas, embedding=self.embeddings)
|
308 |
|
309 |
except Exception as e:
|
310 |
raise Exception(f"Error processing markdown content: {str(e)}")
|
311 |
|
312 |
+
def search(self, query: str, k: int = 15) -> List[Dict[str, Any]]:
|
313 |
"""Search for relevant documents"""
|
314 |
if not self.vectorstore:
|
315 |
raise ValueError("Database not initialized. Please process documents first.")
|