Update app.py
Browse files3 unmodified lines456789102 unmodified lines1314151617181917 unmodified lines3738394041424323 unmodified lines6768697071727366 unmodified lines140141142143144145146147123 unmodified lines2712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183 unmodified lines32232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643710 unmodified lines44844945045145245345445544 unmodified lines50050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955053 unmodified lines60460560660760860961049 unmodified lines6606616626636646656666676686696706711 unmodified line67367467567667767867968068168268368416 unmodified lines7017027037047057067077087097107113 unmodified lines7157167177187197207217227237244 unmodified lines7297307317327337347357367377387397407417427437447457467473 unmodified linesimport gradio as grimport requestsfrom duckduckgo_search import DDGSfrom typing import Listfrom pydantic import BaseModel, Fieldfrom tempfile import NamedTemporaryFilefrom langchain_community.vectorstores import FAISS2 unmodified linesfrom langchain_community.document_loaders import PyPDFLoaderfrom langchain_community.embeddings import HuggingFaceEmbeddingsfrom llama_parse import LlamaParsefrom langchain_core.documents import Documentfrom huggingface_hub import InferenceClientimport inspectimport logging17 unmodified lines"mistralai/Mistral-7B-Instruct-v0.3","mistralai/Mixtral-8x7B-Instruct-v0.1","@cf/meta/llama-3.1-8b-instruct","mistralai/Mistral-Nemo-Instruct-2407"]# Initialize LlamaParse23 unmodified linesraise ValueError("Invalid parser specified. Use 'pypdf' or 'llamaparse'.")def 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"66 unmodified lines# 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 delete_documents(selected_docs):global uploaded_documents123 unmodified linesprint(f"Final clean response: {final_response[:100]}...")return final_responseclass SimpleDDGSearch:def search(self, query: str, num_results: int = 3):results = DDGS().text(query, region='wt-wt', safesearch='off', max_results=num_results)return [res["href"] for res in results]class TrafilaturaWebCrawler:def get_website_content_from_url(self, url: str) -> str:try:downloaded = fetch_url(url)if downloaded is None:return f"Failed to fetch content from URL: {url}"result = extract(downloaded, output_format='json', include_comments=False, with_metadata=True, url=url)if result:result_dict = json.loads(result)title = result_dict.get('title', 'No title found')content = result_dict.get('text', 'No content extracted')if content == 'No content extracted':content = extract(downloaded, include_comments=False)return f'=========== Website Title: {title} ===========\n\n=========== Website URL: {url} ===========\n\n=========== Website Content ===========\n\n{content}\n\n=========== Website Content End ===========\n\n'else:return f"No content extracted from URL: {url}"except Exception as e:return f"An error occurred while processing {url}: {str(e)}"class CitingSources(BaseModel):sources: List[str] = Field(...,description="List of sources to cite. Should be an URL of the source.")def chatbot_interface(message, history, use_web_search, model, temperature, num_calls):if not message.strip():return "", historyhistory = history + [(message, "")]try:for response in respond(message, history, model, temperature, num_calls, use_web_search):history[-1] = (message, response)yield historyexcept gr.CancelledError:3 unmodified lineshistory[-1] = (message, f"An unexpected error occurred: {str(e)}")yield historydef retry_last_response(history, use_web_search, model, temperature, num_calls):if not history:return historylast_user_msg = history[-1][0]history = history[:-1] # Remove the last responsereturn chatbot_interface(last_user_msg, history, use_web_search, model, temperature, num_calls)def respond(message, history, model, temperature, num_calls, use_web_search, selected_docs, instruction_key):logging.info(f"User Query: {message}")logging.info(f"Model Used: {model}")logging.info(f"Search Type: {'Web Search' if use_web_search else 'PDF Search'}")logging.info(f"Selected Documents: {selected_docs}")logging.info(f"Instruction Key: {instruction_key}")try:if instruction_key and instruction_key != "None":# This is a summary generation requestinstruction = INSTRUCTION_PROMPTS[instruction_key]context_str = get_context_for_summary(selected_docs)message = f"{instruction}\n\nUsing the following context from the PDF documents:\n{context_str}\nGenerate a detailed summary."use_web_search = False # Ensure we use PDF search for summariesif use_web_search:for main_content, sources in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature):response = f"{main_content}\n\n{sources}"first_line = response.split('\n')[0] if response else ''# logging.info(f"Generated Response (first line): {first_line}")yield responseelse:embed = get_embeddings()if os.path.exists("faiss_database"):database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)retriever = database.as_retriever()# Filter relevant documents based on user selectionall_relevant_docs = retriever.get_relevant_documents(message)relevant_docs = [doc for doc in all_relevant_docs if doc.metadata["source"] in selected_docs]if not relevant_docs:yield "No relevant information found in the selected documents. Please try selecting different documents or rephrasing your query."returncontext_str = "\n".join([doc.page_content for doc in relevant_docs])else:context_str = "No documents available."yield "No documents available. Please upload PDF documents to answer questions."returnif model == "@cf/meta/llama-3.1-8b-instruct":# Use Cloudflare APIfor partial_response in get_response_from_cloudflare(prompt="", context=context_str, query=message, num_calls=num_calls, temperature=temperature, search_type="pdf"):first_line = partial_response.split('\n')[0] if partial_response else ''# logging.info(f"Generated Response (first line): {first_line}")yield partial_responseelse:# Use Hugging Face APIfor partial_response in get_response_from_pdf(message, model, selected_docs, num_calls=num_calls, temperature=temperature):first_line = partial_response.split('\n')[0] if partial_response else ''# logging.info(f"Generated Response (first line): {first_line}")yield partial_responseexcept Exception as e:logging.error(f"Error with {model}: {str(e)}")if "microsoft/Phi-3-mini-4k-instruct" in model:logging.info("Falling back to Mistral model due to Phi-3 error")fallback_model = "mistralai/Mistral-7B-Instruct-v0.3"yield from respond(message, history, fallback_model, temperature, num_calls, use_web_search, selected_docs, instruction_key)else:yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."logging.basicConfig(level=logging.DEBUG)def get_context_for_summary(selected_docs):embed = get_embeddings()if os.path.exists("faiss_database"):database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)retriever = database.as_retriever(search_kwargs={"k": 5}) # Retrieve top 5 most relevant chunks# Create a generic query that covers common financial summary topicsgeneric_query = "financial performance revenue profit assets liabilities cash flow key metrics highlights"relevant_docs = retriever.get_relevant_documents(generic_query)filtered_docs = [doc for doc in relevant_docs if doc.metadata["source"] in selected_docs]if not filtered_docs:return "No relevant information found in the selected documents for summary generation."context_str = "\n".join([doc.page_content for doc in filtered_docs])return context_strelse:return "No documents available for summary generation."def get_context_for_query(query, selected_docs):embed = get_embeddings()if os.path.exists("faiss_database"):database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)retriever = database.as_retriever(search_kwargs={"k": 3}) # Retrieve top 3 most relevant chunksrelevant_docs = retriever.get_relevant_documents(query)filtered_docs = [doc for doc in relevant_docs if doc.metadata["source"] in selected_docs]if not filtered_docs:return "No relevant information found in the selected documents for the given query."context_str = "\n".join([doc.page_content for doc in filtered_docs])return context_strelse:return "No documents available to answer the query."def get_response_from_cloudflare(prompt, context, query, num_calls=3, temperature=0.2, search_type="pdf"):headers = {10 unmodified linesinstruction = f"""Using the following context:{context}Write a detailed and complete research document that fulfills the following user request: '{query}'Also the generated research document should include the source URL as a hyperlink within the text. Ensure that the hyperlink is embedded in a relevant phrase or word within the response.Format the hyperlink in Markdown (e.g., [relevant text](URL))."""inputs = [{"role": "system", "content": instruction},44 unmodified linesreturn FAISS.from_documents(documents, embed)def get_response_with_search(query, model, num_calls=3, temperature=0.2):searcher = SimpleDDGSearch()search_results = searcher.search(query, num_results=3)crawler = TrafilaturaWebCrawler()context = ""for url in search_results:context += crawler.get_website_content_from_url(url) + "\n"prompt = f"""You are an expert AI named Sentinel and have been given a task to create a detailed and complete research article using the following context from web search results:{context} that fulfills the following user request: '{query}'Cover all the key points discussed in the {context} provided to you. Also stick with the context provided to you do not report any misleading or out of context information.Also the research document should include the source URL as a hyperlink within the text. Ensure that the hyperlink is embedded in a relevant phrase or word within the response.Format the hyperlink in Markdown (e.g., [relevant text](URL))."""if model == "@cf/meta/llama-3.1-8b-instruct":# Use Cloudflare APIfor response in get_response_from_cloudflare(prompt="", context=context, query=query, num_calls=num_calls, temperature=temperature, search_type="web"):yield response, "" # Yield streaming response without sourceselse:# Use Hugging Face APIclient = InferenceClient(model, token=huggingface_token)main_content = ""for i in range(num_calls):for message in client.chat_completion(messages=[{"role": "user", "content": prompt}],max_tokens=10000,temperature=temperature,stream=True,):if message.choices and message.choices[0].delta and message.choices[0].delta.content:chunk = message.choices[0].delta.contentmain_content += chunkyield main_content, "" # Yield partial main content without sourcesINSTRUCTION_PROMPTS = {"Asset Managers": "Summarize the key financial metrics, assets under management, and performance highlights for this asset management company.","Consumer Finance Companies": "Provide a summary of the company's loan portfolio, interest income, credit quality, and key operational metrics.","Mortgage REITs": "Summarize the REIT's mortgage-backed securities portfolio, net interest income, book value per share, and dividend yield.",# Add more instruction prompts as needed}def get_response_from_pdf(query, model, selected_docs, num_calls=3, temperature=0.2):logging.info(f"Entering get_response_from_pdf with query: {query}, model: {model}, selected_docs: {selected_docs}")53 unmodified lineslogging.info(f"API call {i+1}/{num_calls}")for message in client.chat_completion(messages=[{"role": "user", "content": prompt}],max_tokens=10000,temperature=temperature,stream=True,):49 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)custom_placeholder = "Ask a question (Note: You can toggle between Web Search and PDF Chat in Additional Inputs below)"instruction_choices = ["None"] + list(INSTRUCTION_PROMPTS.keys())demo = gr.ChatInterface(respond,additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=True, render=False),1 unmodified linegr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),use_web_search,document_selector,gr.Dropdown(choices=instruction_choices, label="Select Entity Type for Summary", value="None")],title="AI-powered Web Search and PDF Chat Assistant",description="Chat with your PDFs, use web search to answer questions, or generate summaries. Select an Entity Type for Summary to generate a specific summary.",theme=gr.themes.Soft(primary_hue="orange",secondary_hue="amber",16 unmodified linesexamples=[["Tell me about the contents of the uploaded PDFs."],["What are the main topics discussed in the documents?"],["Can you summarize the key points from the PDFs?"]],cache_examples=False,analytics_enabled=False,textbox=gr.Textbox(placeholder=custom_placeholder, container=False, scale=7),chatbot = gr.Chatbot(show_copy_button=True,likeable=True,3 unmodified lines))# Add file upload functionalitywith demo:gr.Markdown("## Upload and Manage PDF Documents")with gr.Row():file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])parser_dropdown = gr.Dropdown(choices=["pypdf", "llamaparse"], label="Select PDF Parser", value="llamaparse")4 unmodified linesdelete_button = gr.Button("Delete Selected Documents")# Update both the output text and the document selectorupdate_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])# Add the delete button functionalitydelete_button.click(delete_documents,inputs=[document_selector],outputs=[update_output, document_selector])gr.Markdown("""3 unmodified lines456789102 unmodified lines13141516171817 unmodified lines36373839404142434445464723 unmodified lines7172737475767766 unmodified lines144145146147148149150151152153123 unmodified lines2772782792802812822832842852862872882892903 unmodified lines29429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347410 unmodified lines48548648748848949049144 unmodified lines53653753853954054153 unmodified lines59559659759859960060149 unmodified lines6516526536546556566576586596606616621 unmodified line66466566666766866967067167267367416 unmodified lines6916926936946956966976986997007017023 unmodified lines7067077087097107117127137147154 unmodified lines7207217227237247257267277287297307317327337347357367377387397407417427437443 unmodified linesimport gradio as grimport requestsfrom duckduckgo_search import DDGSfrom typing import List, Dictfrom pydantic import BaseModel, Fieldfrom tempfile import NamedTemporaryFilefrom langchain_community.vectorstores import FAISS2 unmodified linesfrom langchain_community.document_loaders import PyPDFLoaderfrom langchain_community.embeddings import HuggingFaceEmbeddingsfrom llama_parse import LlamaParsefrom huggingface_hub import InferenceClientimport inspectimport logging17 unmodified lines"mistralai/Mistral-7B-Instruct-v0.3","mistralai/Mixtral-8x7B-Instruct-v0.1","@cf/meta/llama-3.1-8b-instruct","mistralai/Mistral-Nemo-Instruct-2407","meta-llama/Meta-Llama-3.1-8B-Instruct","duckduckgo/gpt-4o-mini","duckduckgo/claude-3-haiku","duckduckgo/llama-3.1-70b","duckduckgo/mixtral-8x7b"]# Initialize LlamaParse23 unmodified linesraise ValueError("Invalid parser specified. Use 'pypdf' or 'llamaparse'.")def get_embeddings():return HuggingFaceEmbeddings(model_name="avsolatorio/GIST-Embedding-v0")# Add this at the beginning of your script, after importsDOCUMENTS_FILE = "uploaded_documents.json"66 unmodified lines# Save the updated list of documentssave_documents(uploaded_documents)# Return a tuple with the status message and the updated document listreturn f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}.", display_documents()def delete_documents(selected_docs):global uploaded_documents123 unmodified linesprint(f"Final clean response: {final_response[:100]}...")return final_responsedef chatbot_interface(message, history, model, temperature, num_calls):if not message.strip():return "", historyhistory = history + [(message, "")]try:for response in respond(message, history, model, temperature, num_calls):history[-1] = (message, response)yield historyexcept gr.CancelledError:3 unmodified lineshistory[-1] = (message, f"An unexpected error occurred: {str(e)}")yield historydef retry_last_response(history, model, temperature, num_calls):if not history:return historylast_user_msg = history[-1][0]history = history[:-1] # Remove the last responsereturn chatbot_interface(last_user_msg, history, model, temperature, num_calls)def truncate_context(context, max_length=16000):"""Truncate the context to a maximum length."""if len(context) <= max_length:return contextreturn context[:max_length] + "..."def get_response_from_duckduckgo(query, model, context, num_calls=1, temperature=0.2):logging.info(f"Using DuckDuckGo chat with model: {model}")ddg_model = model.split('/')[-1] # Extract the model name from the full string# Truncate the context to avoid exceeding input limitstruncated_context = truncate_context(context)full_response = ""for _ in range(num_calls):try:# Include truncated context in the querycontextualized_query = f"Using the following context:\n{truncated_context}\n\nUser question: {query}"results = DDGS().chat(contextualized_query, model=ddg_model)full_response += results + "\n"logging.info(f"DuckDuckGo API response received. Length: {len(results)}")except Exception as e:logging.error(f"Error in generating response from DuckDuckGo: {str(e)}")yield f"An error occurred with the {model} model: {str(e)}. Please try again."returnyield full_response.strip()class ConversationManager:def __init__(self):self.history = []self.current_context = Nonedef add_interaction(self, query, response):self.history.append((query, response))self.current_context = f"Previous query: {query}\nPrevious response summary: {response[:200]}..."def get_context(self):return self.current_contextconversation_manager = ConversationManager()def get_web_search_results(query: str, max_results: int = 10) -> List[Dict[str, str]]:try:results = list(DDGS().text(query, max_results=max_results))if not results:print(f"No results found for query: {query}")return resultsexcept Exception as e:print(f"An error occurred during web search: {str(e)}")return [{"error": f"An error occurred during web search: {str(e)}"}]def rephrase_query(original_query: str, conversation_manager: ConversationManager) -> str:context = conversation_manager.get_context()if context:prompt = f"""You are a highly intelligent conversational chatbot. Your task is to analyze the given context and new query, then decide whether to rephrase the query with or without incorporating the context. Follow these steps:1. Determine if the new query is a continuation of the previous conversation or an entirely new topic.2. If it's a continuation, rephrase the query by incorporating relevant information from the context to make it more specific and contextual.3. If it's a new topic, rephrase the query to make it more appropriate for a web search, focusing on clarity and accuracy without using the previous context.4. Provide ONLY the rephrased query without any additional explanation or reasoning.Context: {context}New query: {original_query}Rephrased query:"""response = DDGS().chat(prompt, model="llama-3.1-70b")rephrased_query = response.split('\n')[0].strip()return rephrased_queryreturn original_querydef summarize_web_results(query: str, search_results: List[Dict[str, str]], conversation_manager: ConversationManager) -> str:try:context = conversation_manager.get_context()search_context = "\n\n".join([f"Title: {result['title']}\nContent: {result['body']}" for result in search_results])prompt = f"""You are a highly intelligent & expert analyst and your job is to skillfully articulate the web search results about '{query}' and considering the context: {context},You have to create a comprehensive news summary FOCUSING on the context provided to you.Include key facts, relevant statistics, and expert opinions if available.Ensure the article is well-structured with an introduction, main body, and conclusion, IF NECESSARY.Address the query in the context of the ongoing conversation IF APPLICABLE.Cite sources directly within the generated text and not at the end of the generated text, integrating URLs where appropriate to support the information provided:{search_context}Article:"""summary = DDGS().chat(prompt, model="llama-3.1-70b")return summaryexcept Exception as e:return f"An error occurred during summarization: {str(e)}"# Modify the existing respond function to handle both PDF and web searchdef respond(message, history, model, temperature, num_calls, use_web_search, selected_docs):logging.info(f"User Query: {message}")logging.info(f"Model Used: {model}")logging.info(f"Selected Documents: {selected_docs}")logging.info(f"Use Web Search: {use_web_search}")if use_web_search:original_query = messagerephrased_query = rephrase_query(message, conversation_manager)logging.info(f"Original query: {original_query}")logging.info(f"Rephrased query: {rephrased_query}")final_summary = ""for _ in range(num_calls):search_results = get_web_search_results(rephrased_query)if not search_results:final_summary += f"No search results found for the query: {rephrased_query}\n\n"elif "error" in search_results[0]:final_summary += search_results[0]["error"] + "\n\n"else:summary = summarize_web_results(rephrased_query, search_results, conversation_manager)final_summary += summary + "\n\n"if final_summary:conversation_manager.add_interaction(original_query, final_summary)yield final_summaryelse:yield "Unable to generate a response. Please try a different query."else:# Existing PDF search logictry:embed = get_embeddings()if os.path.exists("faiss_database"):database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)retriever = database.as_retriever(search_kwargs={"k": 20})all_relevant_docs = retriever.get_relevant_documents(message)relevant_docs = [doc for doc in all_relevant_docs if doc.metadata["source"] in selected_docs]if not relevant_docs:yield "No relevant information found in the selected documents. Please try selecting different documents or rephrasing your query."returncontext_str = "\n".join([doc.page_content for doc in relevant_docs])logging.info(f"Context length: {len(context_str)}")else:context_str = "No documents available."yield "No documents available. Please upload PDF documents to answer questions."returnif model.startswith("duckduckgo/"):# Use DuckDuckGo chat with contextfor partial_response in get_response_from_duckduckgo(message, model, context_str, num_calls, temperature):yield partial_responseelif model == "@cf/meta/llama-3.1-8b-instruct":# Use Cloudflare APIfor partial_response in get_response_from_cloudflare(prompt="", context=context_str, query=message, num_calls=num_calls, temperature=temperature, search_type="pdf"):yield partial_responseelse:# Use Hugging Face APIfor partial_response in get_response_from_pdf(message, model, selected_docs, num_calls=num_calls, temperature=temperature):yield partial_responseexcept Exception as e:logging.error(f"Error with {model}: {str(e)}")if "microsoft/Phi-3-mini-4k-instruct" in model:logging.info("Falling back to Mistral model due to Phi-3 error")fallback_model = "mistralai/Mistral-7B-Instruct-v0.3"yield from respond(message, history, fallback_model, temperature, num_calls, selected_docs)else:yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."logging.basicConfig(level=logging.DEBUG)def get_response_from_cloudflare(prompt, context, query, num_calls=3, temperature=0.2, search_type="pdf"):headers = {10 unmodified linesinstruction = f"""Using the following context:{context}Write a detailed and complete research document that fulfills the following user request: '{query}'After writing the document, please provide a list of sources used in your response."""inputs = [{"role": "system", "content": instruction},44 unmodified linesreturn FAISS.from_documents(documents, embed)def get_response_from_pdf(query, model, selected_docs, num_calls=3, temperature=0.2):logging.info(f"Entering get_response_from_pdf with query: {query}, model: {model}, selected_docs: {selected_docs}")53 unmodified lineslogging.info(f"API call {i+1}/{num_calls}")for message in client.chat_completion(messages=[{"role": "user", "content": prompt}],max_tokens=20000,temperature=temperature,stream=True,):49 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=False)custom_placeholder = "Ask a question (Note: You can toggle between Web Search and PDF Chat in Additional Inputs below)"# Update the demo interface# Update the Gradio interfacedemo = gr.ChatInterface(respond,additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=True, render=False),1 unmodified linegr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),gr.Checkbox(label="Use Web Search", value=True),gr.CheckboxGroup(label="Select documents to query")],title="AI-powered PDF Chat and Web Search Assistant",description="Chat with your PDFs or use web search to answer questions.",theme=gr.themes.Soft(primary_hue="orange",secondary_hue="amber",16 unmodified linesexamples=[["Tell me about the contents of the uploaded PDFs."],["What are the main topics discussed in the documents?"],["Can you summarize the key points from the PDFs?"],["What's the latest news about artificial intelligence?"]],cache_examples=False,analytics_enabled=False,textbox=gr.Textbox(placeholder="Ask a question about the uploaded PDFs or any topic", container=False, scale=7),chatbot = gr.Chatbot(show_copy_button=True,likeable=True,3 unmodified lines))# Add file upload functionality# Add file upload functionalitywith demo:gr.Markdown("## Upload and Manage PDF Documents")with gr.Row():file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])parser_dropdown = gr.Dropdown(choices=["pypdf", "llamaparse"], label="Select PDF Parser", value="llamaparse")4 unmodified linesdelete_button = gr.Button("Delete Selected Documents")# Update both the output text and the document selectorupdate_button.click(update_vectors,inputs=[file_input, parser_dropdown],outputs=[update_output, demo.additional_inputs[-1]] # Use the CheckboxGroup from additional_inputs)# Add the refresh button functionalityrefresh_button.click(refresh_documents,inputs=[],outputs=[demo.additional_inputs[-1]] # Use the CheckboxGroup from additional_inputs)# Add the delete button functionalitydelete_button.click(delete_documents,inputs=[demo.additional_inputs[-1]], # Use the CheckboxGroup from additional_inputsoutputs=[update_output, demo.additional_inputs[-1]])gr.Markdown("""
app.py+179-182