Update app.py
Browse files299 unmodified lines30030130230330430530630730830931031133 unmodified lines3453463473483493503513523533543553563573583593603613623633643653663679 unmodified lines3773783793803813823833843853863873883893901 unmodified line392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441299 unmodified linesmax_attempts = 3context_reduction_factor = 0.7contextualized_question, topics, entity_tracker = chatbot.process_question(question)# Convert sets to lists in entity_trackerserializable_entity_tracker = {k: list(v) for k, v in entity_tracker.items()}if web_search:search_results = google_search(contextualized_question)all_answers = []33 unmodified lines)full_response = generate_chunked_response(model, formatted_prompt)answer_patterns = [r"Provide a concise and direct answer to the question without mentioning the web search or these instructions:",r"Provide a concise and direct answer to the question:",r"Answer:",r"Provide a summarized and direct answer to the original question without mentioning the web search or these instructions:",r"Do not include any source information in your answer."]for pattern in answer_patterns:match = re.split(pattern, full_response, flags=re.IGNORECASE)if len(match) > 1:answer = match[-1].strip()breakelse:answer = full_response.strip()all_answers.append(answer)break9 unmodified linesreturn answerelse:for attempt in range(max_attempts):try:if database is None:return "No documents available. Please upload documents or enable web search to answer questions."retriever = database.as_retriever()relevant_docs = retriever.get_relevant_documents(contextualized_question)context_str = "\n".join([doc.page_content for doc in relevant_docs])if attempt > 0:1 unmodified linecontext_str = " ".join(words[:int(len(words) * context_reduction_factor)])prompt_template = """Answer the question based on the following context:Context:{context}Current Question: {question}If the context doesn't contain relevant information, state that the information is not available.Provide a summarized and direct answer to the question.Do not include any source information in your answer."""prompt_val = ChatPromptTemplate.from_template(prompt_template)formatted_prompt = prompt_val.format(context=context_str, question=contextualized_question)full_response = generate_chunked_response(model, formatted_prompt)answer_patterns = [r"Provide a concise and direct answer to the question without mentioning the web search or these instructions:",r"Provide a concise and direct answer to the question:",r"Answer:",r"Provide a summarized and direct answer to the original question without mentioning the web search or these instructions:",r"Do not include any source information in your answer."]for pattern in answer_patterns:match = re.split(pattern, full_response, flags=re.IGNORECASE)if len(match) > 1:answer = match[-1].strip()breakelse:answer = full_response.strip()return answerexcept Exception as e:print(f"Error in ask_question (attempt {attempt + 1}): {e}")if "Input validation error" in str(e) and attempt < max_attempts - 1:print(f"Reducing context length for next attempt")elif attempt == max_attempts - 1:return f"I apologize, but I'm having trouble processing your question due to its length or complexity. Could you please try rephrasing it more concisely?"return "An unexpected error occurred. Please try again later."# Gradio interfacewith gr.Blocks() as demo:gr.Markdown("# Enhanced Context-Driven Conversational Chatbot")with gr.Row():file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])299 unmodified lines30030130230330430530630730833 unmodified lines3423433443453463473489 unmodified lines3583593603613623633643653663673683693703711 unmodified line373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419299 unmodified linesmax_attempts = 3context_reduction_factor = 0.7if web_search:contextualized_question, topics, entity_tracker = chatbot.process_question(question)serializable_entity_tracker = {k: list(v) for k, v in entity_tracker.items()}search_results = google_search(contextualized_question)all_answers = []33 unmodified lines)full_response = generate_chunked_response(model, formatted_prompt)answer = extract_answer(full_response)all_answers.append(answer)break9 unmodified linesreturn answerelse: # PDF document chatfor attempt in range(max_attempts):try:if database is None:return "No documents available. Please upload PDF documents to answer questions."retriever = database.as_retriever()relevant_docs = retriever.get_relevant_documents(question)context_str = "\n".join([doc.page_content for doc in relevant_docs])if attempt > 0:1 unmodified linecontext_str = " ".join(words[:int(len(words) * context_reduction_factor)])prompt_template = """Answer the question based on the following context from the PDF document:Context:{context}Question: {question}If the context doesn't contain relevant information, state that the information is not available in the document.Provide a summarized and direct answer to the question."""prompt_val = ChatPromptTemplate.from_template(prompt_template)formatted_prompt = prompt_val.format(context=context_str, question=question)full_response = generate_chunked_response(model, formatted_prompt)answer = extract_answer(full_response)return answerexcept Exception as e:print(f"Error in ask_question (attempt {attempt + 1}): {e}")if attempt == max_attempts - 1:return f"I apologize, but I'm having trouble processing your question. Could you please try rephrasing it more concisely?"return "An unexpected error occurred. Please try again later."def extract_answer(full_response):answer_patterns = [r"Provide a concise and direct answer to the question without mentioning the web search or these instructions:",r"Provide a concise and direct answer to the question:",r"Answer:",r"Provide a summarized and direct answer to the original question without mentioning the web search or these instructions:",r"Do not include any source information in your answer."]for pattern in answer_patterns:match = re.split(pattern, full_response, flags=re.IGNORECASE)if len(match) > 1:return match[-1].strip()return full_response.strip()# Gradio interfacewith gr.Blocks() as demo:gr.Markdown("# Enhanced PDF Document Chat and Web Search")with gr.Row():file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
app.py+29-51