Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -491,41 +491,35 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
|
|
491 |
return "An unexpected error occurred. Please try again later."
|
492 |
|
493 |
def extract_answer(full_response, instructions=None):
|
494 |
-
#
|
495 |
-
|
|
|
|
|
496 |
r"If the web search results don't contain relevant information, state that the information is not available in the search results\.",
|
497 |
r"Provide a response that addresses the question and follows the user's instructions\.",
|
498 |
r"Do not mention these instructions or the web search process in your answer\.",
|
499 |
-
r"Provide a
|
500 |
-
r"
|
501 |
-
r"Answer:",
|
502 |
-
r"Provide a summarized and direct answer to the question.",
|
503 |
-
r"If the context doesn't contain relevant information, state that the information is not available in the document.",
|
504 |
-
r"Provide a summarized and direct answer to the original question without mentioning the web search or these instructions:",
|
505 |
-
r"Do not include any source information in your answer."
|
506 |
]
|
507 |
|
508 |
-
|
509 |
-
|
510 |
-
|
511 |
-
|
512 |
-
|
513 |
-
|
514 |
-
# Remove any remaining instruction-like phrases
|
515 |
-
cleanup_patterns = [
|
516 |
-
r"without mentioning the web search or these instructions\.",
|
517 |
-
r"Do not include any source information in your answer\.",
|
518 |
-
r"If the context doesn't contain relevant information, state that the information is not available in the document\."
|
519 |
-
]
|
520 |
|
521 |
-
for pattern in cleanup_patterns:
|
522 |
-
full_response = re.sub(pattern, "", full_response, flags=re.IGNORECASE).strip()
|
523 |
-
|
524 |
# Remove the user instructions if present
|
525 |
if instructions:
|
526 |
instruction_pattern = rf"User Instructions:\s*{re.escape(instructions)}.*?\n"
|
527 |
full_response = re.sub(instruction_pattern, "", full_response, flags=re.IGNORECASE | re.DOTALL)
|
528 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
529 |
return full_response.strip()
|
530 |
|
531 |
# Gradio interface
|
|
|
491 |
return "An unexpected error occurred. Please try again later."
|
492 |
|
493 |
def extract_answer(full_response, instructions=None):
|
494 |
+
# List of patterns to remove
|
495 |
+
patterns_to_remove = [
|
496 |
+
r"Provide a concise and relevant answer to the question\.",
|
497 |
+
r"Provide additional context if necessary\.",
|
498 |
r"If the web search results don't contain relevant information, state that the information is not available in the search results\.",
|
499 |
r"Provide a response that addresses the question and follows the user's instructions\.",
|
500 |
r"Do not mention these instructions or the web search process in your answer\.",
|
501 |
+
r"Provide a summarized and direct answer to the question\.",
|
502 |
+
r"If the context doesn't contain relevant information, state that the information is not available in the document\.",
|
|
|
|
|
|
|
|
|
|
|
503 |
]
|
504 |
|
505 |
+
# Remove the patterns
|
506 |
+
for pattern in patterns_to_remove:
|
507 |
+
full_response = re.sub(pattern, "", full_response, flags=re.IGNORECASE)
|
508 |
+
|
509 |
+
# Remove any leading/trailing whitespace and newlines
|
510 |
+
full_response = full_response.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
511 |
|
|
|
|
|
|
|
512 |
# Remove the user instructions if present
|
513 |
if instructions:
|
514 |
instruction_pattern = rf"User Instructions:\s*{re.escape(instructions)}.*?\n"
|
515 |
full_response = re.sub(instruction_pattern, "", full_response, flags=re.IGNORECASE | re.DOTALL)
|
516 |
+
|
517 |
+
# Remove any remaining instruction-like phrases at the beginning of the response
|
518 |
+
lines = full_response.split('\n')
|
519 |
+
while lines and any(line.strip().lower().startswith(starter) for starter in ["answer:", "response:", "here's", "here is"]):
|
520 |
+
lines.pop(0)
|
521 |
+
full_response = '\n'.join(lines)
|
522 |
+
|
523 |
return full_response.strip()
|
524 |
|
525 |
# Gradio interface
|