Spaces:
Sleeping
Sleeping
Update app.py
Browse filesAdded functionality to generate a new index everytime
app.py
CHANGED
|
@@ -17,6 +17,7 @@ from langchain_pinecone import PineconeVectorStore
|
|
| 17 |
from pinecone import Pinecone, ServerlessSpec
|
| 18 |
from pinecone import PineconeApiException, NotFoundException
|
| 19 |
import shutil
|
|
|
|
| 20 |
|
| 21 |
from dotenv import load_dotenv
|
| 22 |
load_dotenv()
|
|
@@ -46,6 +47,16 @@ if 'chat_history' not in st.session_state:
|
|
| 46 |
st.session_state.chat_history = []
|
| 47 |
if 'chat_enabled' not in st.session_state:
|
| 48 |
st.session_state.chat_enabled = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
if not st.session_state.initialized:
|
| 51 |
# Clear everything only on first run or page refresh
|
|
@@ -57,6 +68,15 @@ if not st.session_state.initialized:
|
|
| 57 |
st.session_state.vectorstore = None
|
| 58 |
st.session_state.retriever = None
|
| 59 |
st.session_state.initialized = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
def save_uploaded_file(uploaded_file):
|
| 62 |
"""Save uploaded file to the data directory"""
|
|
@@ -127,7 +147,7 @@ def process_documents(uploaded_files_dict):
|
|
| 127 |
|
| 128 |
# Initialize Pinecone
|
| 129 |
pc = Pinecone(api_key=os.getenv("PINECONE_API_KEY"))
|
| 130 |
-
index_name =
|
| 131 |
|
| 132 |
try:
|
| 133 |
# Recreate the index
|
|
@@ -316,7 +336,7 @@ def process_and_chat():
|
|
| 316 |
# Initialize components for chat
|
| 317 |
llm = ChatGroq(temperature=0, model_name="mixtral-8x7b-32768", groq_api_key=os.getenv("GROQ_API_KEY"))
|
| 318 |
pc = Pinecone(api_key=os.getenv("PINECONE_API_KEY"))
|
| 319 |
-
index_name =
|
| 320 |
pc_index = pc.Index(index_name)
|
| 321 |
|
| 322 |
# Create vectorstore
|
|
|
|
| 17 |
from pinecone import Pinecone, ServerlessSpec
|
| 18 |
from pinecone import PineconeApiException, NotFoundException
|
| 19 |
import shutil
|
| 20 |
+
import uuid
|
| 21 |
|
| 22 |
from dotenv import load_dotenv
|
| 23 |
load_dotenv()
|
|
|
|
| 47 |
st.session_state.chat_history = []
|
| 48 |
if 'chat_enabled' not in st.session_state:
|
| 49 |
st.session_state.chat_enabled = False
|
| 50 |
+
if 'session_id' not in st.session_state:
|
| 51 |
+
# Generate a unique session ID using UUID
|
| 52 |
+
st.session_state.session_id = str(uuid.uuid4())[:8]
|
| 53 |
+
|
| 54 |
+
def get_session_index_name():
|
| 55 |
+
"""Get unique index name for current session"""
|
| 56 |
+
base_name = "docdb" # Using a short base name to leave room for the unique identifier
|
| 57 |
+
unique_id = st.session_state.session_id
|
| 58 |
+
# Combine base name with unique ID, ensuring total length is under 45 chars
|
| 59 |
+
return f"{base_name}-{unique_id}" # This will be like "docdb-12345678"
|
| 60 |
|
| 61 |
if not st.session_state.initialized:
|
| 62 |
# Clear everything only on first run or page refresh
|
|
|
|
| 68 |
st.session_state.vectorstore = None
|
| 69 |
st.session_state.retriever = None
|
| 70 |
st.session_state.initialized = True
|
| 71 |
+
|
| 72 |
+
# Delete any existing index for this session (in case of page refresh)
|
| 73 |
+
try:
|
| 74 |
+
pc = Pinecone(api_key=os.getenv("PINECONE_API_KEY"))
|
| 75 |
+
index_name = get_session_index_name()
|
| 76 |
+
if index_name in pc.list_indexes().names():
|
| 77 |
+
pc.delete_index(index_name)
|
| 78 |
+
except Exception as e:
|
| 79 |
+
st.error(f"Error cleaning up old index: {str(e)}")
|
| 80 |
|
| 81 |
def save_uploaded_file(uploaded_file):
|
| 82 |
"""Save uploaded file to the data directory"""
|
|
|
|
| 147 |
|
| 148 |
# Initialize Pinecone
|
| 149 |
pc = Pinecone(api_key=os.getenv("PINECONE_API_KEY"))
|
| 150 |
+
index_name = get_session_index_name()
|
| 151 |
|
| 152 |
try:
|
| 153 |
# Recreate the index
|
|
|
|
| 336 |
# Initialize components for chat
|
| 337 |
llm = ChatGroq(temperature=0, model_name="mixtral-8x7b-32768", groq_api_key=os.getenv("GROQ_API_KEY"))
|
| 338 |
pc = Pinecone(api_key=os.getenv("PINECONE_API_KEY"))
|
| 339 |
+
index_name = get_session_index_name()
|
| 340 |
pc_index = pc.Index(index_name)
|
| 341 |
|
| 342 |
# Create vectorstore
|