Update app.py
Browse files65 unmodified lines66676869707172737475767778798081826 unmodified lines8990919293949514 unmodified lines110111112113114115116117118119120121406 unmodified lines5285295305315325335 unmodified lines53954054154254354454557 unmodified lines6036046056066076081 unmodified line61061161261361461565 unmodified linesdef get_embeddings():return HuggingFaceEmbeddings(model_name="sentence-transformers/stsb-roberta-large")def update_vectors(files, parser):global uploaded_documentslogging.info(f"Entering update_vectors with {len(files)} files and parser: {parser}")if not files:logging.warning("No files provided for update_vectors")return "Please upload at least one PDF file.", gr.CheckboxGroup(choices=[doc["name"] for doc in uploaded_documents],value=[doc["name"] for doc in uploaded_documents if doc["selected"]],label="Select documents to query")embed = get_embeddings()total_chunks = 06 unmodified lineslogging.info(f"Loaded {len(data)} chunks from {file.name}")all_data.extend(data)total_chunks += len(data)# Append new documents instead of replacingif not any(doc["name"] == file.name for doc in uploaded_documents):uploaded_documents.append({"name": file.name, "selected": True})logging.info(f"Added new document to uploaded_documents: {file.name}")14 unmodified linesdatabase.save_local("faiss_database")logging.info("FAISS database saved")return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}.", gr.CheckboxGroup(choices=[doc["name"] for doc in uploaded_documents],value=[doc["name"] for doc in uploaded_documents if doc["selected"]],label="Select documents to query")def generate_chunked_response(prompt, model, max_tokens=10000, num_calls=3, temperature=0.2, should_stop=False):print(f"Starting generate_chunked_response with {num_calls} calls")406 unmodified lineslabel="Select documents to query")def initial_conversation():return [(None, "Welcome! I'm your AI assistant for web search and PDF analysis. Here's how you can use me:\n\n"5 unmodified lines]# Define the checkbox outside the demo blockdocument_selector = gr.CheckboxGroup(label="Select documents to query")use_web_search = gr.Checkbox(label="Use Web Search", value=True)57 unmodified linesfile_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])parser_dropdown = gr.Dropdown(choices=["pypdf", "llamaparse"], label="Select PDF Parser", value="llamaparse")update_button = gr.Button("Upload Document")update_output = gr.Textbox(label="Update Status")1 unmodified lineupdate_button.click(update_vectors,inputs=[file_input, parser_dropdown],outputs=[update_output, document_selector])gr.Markdown("""65 unmodified lines6667686970717273747576777879808182838485868788899091929394956 unmodified lines10210310410510610714 unmodified lines122123124125126127128129130131132406 unmodified lines5395405415425435445455465475485495505 unmodified lines55655755855956056156257 unmodified lines6206216226236246256261 unmodified line62862963063163263363463563663763865 unmodified linesdef get_embeddings():return HuggingFaceEmbeddings(model_name="sentence-transformers/stsb-roberta-large")# Add this at the beginning of your script, after importsDOCUMENTS_FILE = "uploaded_documents.json"def load_documents():if os.path.exists(DOCUMENTS_FILE):with open(DOCUMENTS_FILE, "r") as f:return json.load(f)return []def save_documents(documents):with open(DOCUMENTS_FILE, "w") as f:json.dump(documents, f)# Replace the global uploaded_documents with thisuploaded_documents = load_documents()# Modify the update_vectors functiondef update_vectors(files, parser):global uploaded_documentslogging.info(f"Entering update_vectors with {len(files)} files and parser: {parser}")if not files:logging.warning("No files provided for update_vectors")return "Please upload at least one PDF file.", display_documents()embed = get_embeddings()total_chunks = 06 unmodified lineslogging.info(f"Loaded {len(data)} chunks from {file.name}")all_data.extend(data)total_chunks += len(data)if not any(doc["name"] == file.name for doc in uploaded_documents):uploaded_documents.append({"name": file.name, "selected": True})logging.info(f"Added new document to uploaded_documents: {file.name}")14 unmodified linesdatabase.save_local("faiss_database")logging.info("FAISS database saved")# Save the updated list of documentssave_documents(uploaded_documents)return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}.", display_documents()def generate_chunked_response(prompt, model, max_tokens=10000, num_calls=3, temperature=0.2, should_stop=False):print(f"Starting generate_chunked_response with {num_calls} calls")406 unmodified lineslabel="Select documents to query")# Add this new functiondef refresh_documents():global uploaded_documentsuploaded_documents = load_documents()return display_documents()def initial_conversation():return [(None, "Welcome! I'm your AI assistant for web search and PDF analysis. Here's how you can use me:\n\n"5 unmodified lines]# Define the checkbox outside the demo blockdocument_selector = display_documents()use_web_search = gr.Checkbox(label="Use Web Search", value=True)57 unmodified linesfile_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])parser_dropdown = gr.Dropdown(choices=["pypdf", "llamaparse"], label="Select PDF Parser", value="llamaparse")update_button = gr.Button("Upload Document")refresh_button = gr.Button("Refresh Document List")update_output = gr.Textbox(label="Update Status")1 unmodified lineupdate_button.click(update_vectors,inputs=[file_input, parser_dropdown],outputs=[update_output, document_selector])# Add the refresh button functionalityrefresh_button.click(refresh_documents,inputs=[],outputs=[document_selector])gr.Markdown("""
app.py+36-13