Spaces:
Sleeping
Sleeping
Update app.py
Browse filesHandling deletion of indexes
57 unmodified lines585960616263646566676869707172735 unmodified lines7980817273747576777879828384858610 unmodified lines979899961001011021031001041051061074 unmodified lines11211311411511611711811912012112212311712412512612710 unmodified lines13813914013414114214314412 unmodified lines15715815915315415515616016116218 unmodified lines1811821831811841851861871881891861901911921931944 unmodified lines1992002012022032042052062072082091971981992002012022032042052102112122132142152162172182192202072082092212222232242252262272282292302132142312322332 unmodified lines23623723822322423924024124224324424524634 unmodified lines28128228326528428528626726828728828929029113 unmodified lines30530630730830931031131257 unmodified lines # Combine base name with unique ID, ensuring total length is under 45 chars return f"{base_name}-{unique_id}" # This will be like "docdb-12345678"
def cleanup_pinecone_index(): """Clean up existing Pinecone index for the current session""" try: pc = Pinecone(api_key=os.getenv("PINECONE_API_KEY")) index_name = get_session_index_name() if index_name in pc.list_indexes().names(): pc.delete_index(index_name) except Exception as e: print(f"Error cleaning up index: {str(e)}") # Log error internally
if not st.session_state.initialized: # Clear everything only on first run or page refresh if os.path.exists("data"):5 unmodified lines st.session_state.retriever = None st.session_state.initialized = True # Delete any existing index for this session (in case of page refresh) try: pc = Pinecone(api_key=os.getenv("PINECONE_API_KEY")) index_name = get_session_index_name() if index_name in pc.list_indexes().names(): pc.delete_index(index_name) except Exception as e: st.error(f"Error cleaning up old index: {str(e)}") # Clean up any existing index cleanup_pinecone_index()
def save_uploaded_file(uploaded_file): """Save uploaded file to the data directory"""10 unmodified lines if os.path.exists(file_path): return file_path else: st.error(f"File not saved: {file_path}") print(f"File not saved: {file_path}") # Log error internally return None except Exception as e: st.error(f"Error saving file: {str(e)}") print(f"Error saving file: {str(e)}") # Log error internally return None
def process_documents(uploaded_files_dict):4 unmodified lines try: with st.spinner('Processing documents...'): # Clean up existing index before processing cleanup_pinecone_index() docs = [] # Process each file for filename, file_info in uploaded_files_dict.items(): file_path = file_info["path"] if not os.path.exists(file_path): st.error(f"File not found: {file_path}") print(f"File not found: {file_path}") # Log error internally continue if filename.endswith(".pdf"):10 unmodified lines docs.extend(file_doc)
if not docs: st.error("No documents were successfully processed") st.warning("Unable to process the documents. Please try again.") return False
# Split documents12 unmodified lines index_name = get_session_index_name()
try: # Recreate the index if index_name in pc.list_indexes().names(): pc.delete_index(index_name) pc.create_index( name=index_name, dimension=512,18 unmodified lines return True except PineconeApiException as e: st.error("File upload failed! Avoid interrupting document processing by uploading or removing files. Kindly refresh the app to continue.") print(f"Pinecone API error: {str(e)}") # Log error internally st.warning("Unable to process documents at the moment. Please try again.") st.session_state.chat_enabled = False return False except Exception as e: st.error(f"An error occurred during processing: {str(e)}") print(f"Processing error: {str(e)}") # Log error internally st.warning("Unable to process documents at the moment. Please try again.") st.session_state.chat_enabled = False return False finally:4 unmodified lines
def format_reranked_docs(pc, retriever, question): """Rerank documents using Pinecone's reranking model""" # Get relevant docs and ensure they're not empty relevant_docs = [doc.page_content for doc in retriever.invoke(question) if doc.page_content.strip()]
if not relevant_docs: return "I don't have enough context to answer this question."
try: # Format documents for reranking relevant_docs = [doc.page_content for doc in retriever.invoke(question) if len(doc.page_content)>5]
reranked_docs = pc.inference.rerank( model="pinecone-rerank-v0", query=question, documents=relevant_docs, top_n=3, return_documents=True ) formatted_docs = [{"text": doc} for doc in relevant_docs] reranked_docs = pc.inference.rerank( model="pinecone-rerank-v0", query=question, documents=formatted_docs, top_n=3, return_documents=True )
# Extract text from reranked documents final_docs = [d.document.text for d in reranked_docs.data] context = doc2str(final_docs) return context final_docs = [d.document["text"] for d in reranked_docs.data] context = "\n\n".join(final_docs) return context except Exception as e: print(f"Error during reranking: {str(e)}") # Log error internally # Fallback to using retrieved docs without reranking return "\n\n".join(relevant_docs[:3])
def run_chatbot(retriever, pc, llm): """Run the chatbot with the given components""" # st.markdown("<h4>💬 Chat with your Documents</h4>", unsafe_allow_html=True) # Initialize chat prompt prompt = ChatPromptTemplate.from_template(""" You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know.2 unmodified lines {context} </context>
Important: Don't start revealing context in your responses until its asked. First look at the question and then think if the context is needed to answer this or its a normal question, once you have judged then only answer the question.
<important> Don't start revealing context in your responses until its asked. First look at the question and then think if the context is needed to answer this or its a normal question, once you have judged then only answer the question. When there is no context, just respond on your own knowledge as a normal assistant. </important> Answer the following question:
{question}""")34 unmodified lines # Add assistant response to chat history st.session_state.messages.append({"role": "assistant", "content": response}) except Exception as e: error_msg = f"An error occurred while processing your question: {str(e)}" print(f"Chat error: {str(e)}") # Log error internally with st.chat_message("assistant"): error_msg = "I'm having trouble processing your question. Please try asking something else." st.error(error_msg) st.session_state.messages.append({"role": "assistant", "content": f"❌ {error_msg}"}) st.markdown(error_msg) st.session_state.messages.append({"role": "assistant", "content": error_msg})
def process_and_chat(): """Process documents and handle chat interface"""13 unmodified lines # Check for removed files files_to_remove = set(st.session_state.uploaded_files.keys()) - current_uploaded_filenames if files_to_remove: # Clean up index when files are removed cleanup_pinecone_index() for file_name in files_to_remove: # Remove file from session state if file_name in st.session_state.uploaded_files:app.py+59-37