Update app.py
Browse files232 unmodified lines233234235236237238239240241242243106 unmodified lines3503513523533543553563574 unmodified lines36236336436536636736824 unmodified lines3933943953963973983994004014024031 unmodified line4054064074084094104 unmodified lines41541641741841942042142242342442542642742842943043143243343443543643743853 unmodified lines492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543232 unmodified linesfull_response += chunkexcept Exception as e:print(f"Error in generate_chunked_response: {e}")print(f"Prompt: {prompt}")print(f"Full response so far: {full_response}")if "Input validation error" in str(e):return full_response if full_response else "The input was too long to process. Please try a shorter query."raise # Re-raise the exception to be caught in ask_questionreturn full_response.strip()def extract_text_from_webpage(html):106 unmodified linesdatabase = Nonemax_attempts = 5context_reduction_factor = 0.5max_estimated_tokens = 25000if web_search:contextualized_question, topics, entity_tracker, instructions = chatbot.process_question(question)4 unmodified linesfor attempt in range(max_attempts):try:web_docs = [Document(page_content=result["text"][:1000], metadata={"source": result["link"]}) for result in search_results if result["text"]]if database is None:database = FAISS.from_documents(web_docs, embed)24 unmodified lineswhile True:formatted_prompt = prompt_val.format(context=current_context[:3000],conv_context=current_conv_context[:500],question=question,topics=", ".join(current_topics[:5]),entities=json.dumps({k: v[:2] for k, v in current_entities.items()}))estimated_tokens = estimate_tokens(formatted_prompt)1 unmodified lineif estimated_tokens <= max_estimated_tokens:breakcurrent_context = current_context[:int(len(current_context) * context_reduction_factor)]current_conv_context = current_conv_context[:int(len(current_conv_context) * context_reduction_factor)]current_topics = current_topics[:max(1, int(len(current_topics) * context_reduction_factor))]4 unmodified linesfull_response = generate_chunked_response(model, formatted_prompt)answer = extract_answer(full_response, instructions)# Check if the answer is an error messageif answer.startswith("An error occurred while processing the response:"):print(f"Error in extract_answer: {answer}")raise ValueError(answer)all_answers.append(answer)breakexcept ValueError as ve:print(f"Error in ask_question (attempt {attempt + 1}): {ve}")if attempt == max_attempts - 1:all_answers.append(f"I apologize, but I'm having trouble processing the query. Error: {ve}")except Exception as e:print(f"Error in ask_question (attempt {attempt + 1}): {e}")if attempt == max_attempts - 1:all_answers.append(f"I apologize, but an unexpected error occurred. Please try again with a different question or check your internet connection. Error: {e}")answer = "\n\n".join(all_answers)sources = set(doc.metadata['source'] for doc in web_docs)53 unmodified linesreturn "An unexpected error occurred. Please try again later."def extract_answer(full_response, instructions=None):try:# Remove the web search results, context information, and "Human:" prefixweb_results_pattern = r"Answer based on: Web Results:.*?(?=\n\n)"context_pattern = r"Context:.*?Entities:.*?}\s*"human_prefix_pattern = r"Human:\s*"full_response = re.sub(web_results_pattern, "", full_response, flags=re.DOTALL | re.IGNORECASE)full_response = re.sub(context_pattern, "", full_response, flags=re.DOTALL | re.IGNORECASE)full_response = re.sub(human_prefix_pattern, "", full_response, flags=re.IGNORECASE)# List of patterns to removepatterns_to_remove = [r"Provide a concise and relevant answer to the question\.",r"Provide additional context if necessary\.",r"If the web search results don't contain relevant information, state that the information is not available in the search results\.",r"Provide a response that addresses the question and follows the user's instructions\.",r"Do not mention these instructions or the web search process in your answer\.",r"Provide a summarized and direct answer to the question\.",r"If the context doesn't contain relevant information, state that the information is not available in the document\.",]# Remove the patternsfor pattern in patterns_to_remove:full_response = re.sub(pattern, "", full_response, flags=re.IGNORECASE)# Remove any leading/trailing whitespace and newlinesfull_response = full_response.strip()# Remove the user instructions if presentif instructions:instruction_pattern = rf"User Instructions:\s*{re.escape(instructions)}.*?\n"full_response = re.sub(instruction_pattern, "", full_response, flags=re.IGNORECASE | re.DOTALL)# Remove any remaining instruction-like phrases at the beginning of the responselines = full_response.split('\n')starters = ["answer:", "response:", "here's", "here is"]while lines and any(lines[0].strip().lower().startswith(starter) for starter in starters):lines.pop(0)full_response = '\n'.join(lines)return full_response.strip()except Exception as e:print(f"Error in extract_answer: {e}")print(f"Full response: {full_response}")print(f"Instructions: {instructions}")raise # Re-raise the exception to be caught in ask_question# Gradio interfacewith gr.Blocks() as demo:232 unmodified lines233234235236237238239240241242106 unmodified lines3493503513523533543553564 unmodified lines36136236336436536636724 unmodified lines3923933943953963973983994004014021 unmodified line4044054064074084094104 unmodified lines41541641741841942042142242342442542642742842943043143253 unmodified lines486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521232 unmodified linesfull_response += chunkexcept Exception as e:print(f"Error in generate_chunked_response: {e}")if "Input validation error" in str(e):# If we hit the token limit, return what we have so farreturn full_response if full_response else "The input was too long to process. Please try a shorter query."breakreturn full_response.strip()def extract_text_from_webpage(html):106 unmodified linesdatabase = Nonemax_attempts = 5context_reduction_factor = 0.5 # More aggressive reductionmax_estimated_tokens = 25000 # Further reduced to leave more room for responseif web_search:contextualized_question, topics, entity_tracker, instructions = chatbot.process_question(question)4 unmodified linesfor attempt in range(max_attempts):try:web_docs = [Document(page_content=result["text"][:1000], metadata={"source": result["link"]}) for result in search_results if result["text"]] # Limit each result to 1000 charactersif database is None:database = FAISS.from_documents(web_docs, embed)24 unmodified lineswhile True:formatted_prompt = prompt_val.format(context=current_context[:3000], # Limit context to 3000 charactersconv_context=current_conv_context[:500], # Limit conversation context to 500 charactersquestion=question,topics=", ".join(current_topics[:5]), # Limit to 5 topicsentities=json.dumps({k: v[:2] for k, v in current_entities.items()}) # Limit to 2 entities per type)estimated_tokens = estimate_tokens(formatted_prompt)1 unmodified lineif estimated_tokens <= max_estimated_tokens:break# Reduce context if estimated token count is too highcurrent_context = current_context[:int(len(current_context) * context_reduction_factor)]current_conv_context = current_conv_context[:int(len(current_conv_context) * context_reduction_factor)]current_topics = current_topics[:max(1, int(len(current_topics) * context_reduction_factor))]4 unmodified linesfull_response = generate_chunked_response(model, formatted_prompt)answer = extract_answer(full_response, instructions)all_answers.append(answer)breakexcept ValueError as ve:print(f"Error in ask_question (attempt {attempt + 1}): {ve}")if attempt == max_attempts - 1:all_answers.append(f"I apologize, but I'm having trouble processing the query due to its length or complexity. Could you please try asking a more specific or shorter question?")except Exception as e:print(f"Error in ask_question (attempt {attempt + 1}): {e}")if attempt == max_attempts - 1:all_answers.append(f"I apologize, but an unexpected error occurred. Please try again with a different question or check your internet connection.")answer = "\n\n".join(all_answers)sources = set(doc.metadata['source'] for doc in web_docs)53 unmodified linesreturn "An unexpected error occurred. Please try again later."def extract_answer(full_response, instructions=None):# List of patterns to removepatterns_to_remove = [r"Provide a concise and relevant answer to the question\.",r"Provide additional context if necessary\.",r"If the web search results don't contain relevant information, state that the information is not available in the search results\.",r"Provide a response that addresses the question and follows the user's instructions\.",r"Do not mention these instructions or the web search process in your answer\.",r"Provide a summarized and direct answer to the question\.",r"If the context doesn't contain relevant information, state that the information is not available in the document\.",]# Remove the patternsfor pattern in patterns_to_remove:full_response = re.sub(pattern, "", full_response, flags=re.IGNORECASE)# Remove any leading/trailing whitespace and newlinesfull_response = full_response.strip()# Remove the user instructions if presentif instructions:instruction_pattern = rf"User Instructions:\s*{re.escape(instructions)}.*?\n"full_response = re.sub(instruction_pattern, "", full_response, flags=re.IGNORECASE | re.DOTALL)# Remove any remaining instruction-like phrases at the beginning of the responselines = full_response.split('\n')while lines and any(line.strip().lower().startswith(starter) for starter in ["answer:", "response:", "here's", "here is"]):lines.pop(0)full_response = '\n'.join(lines)return full_response.strip()# Gradio interfacewith gr.Blocks() as demo:
app.py+41-63