Update app.py
Browse files490 unmodified lines491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531490 unmodified linesreturn "An unexpected error occurred. Please try again later."def extract_answer(full_response, instructions=None):# First, try to split the response at common instruction phrasesanswer_patterns = [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 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 question.",r"If the context doesn't contain relevant information, state that the information is not available in the document.",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:full_response = match[-1].strip()break# Remove any remaining instruction-like phrasescleanup_patterns = [r"without mentioning the web search or these instructions\.",r"Do not include any source information in your answer\.",r"If the context doesn't contain relevant information, state that the information is not available in the document\."]for pattern in cleanup_patterns:full_response = re.sub(pattern, "", full_response, flags=re.IGNORECASE).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)return full_response.strip()# Gradio interface490 unmodified lines491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525490 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 interface
app.py+19-25