Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -391,7 +391,7 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
|
|
391 |
)
|
392 |
|
393 |
full_response = generate_chunked_response(model, formatted_prompt)
|
394 |
-
answer = extract_answer(full_response)
|
395 |
all_answers.append(answer)
|
396 |
break
|
397 |
|
@@ -448,7 +448,7 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
|
|
448 |
|
449 |
return "An unexpected error occurred. Please try again later."
|
450 |
|
451 |
-
def extract_answer(full_response):
|
452 |
# First, try to split the response at common instruction phrases
|
453 |
answer_patterns = [
|
454 |
r"Provide a concise and direct answer to the question without mentioning the web search or these instructions:",
|
@@ -466,7 +466,7 @@ def extract_answer(full_response):
|
|
466 |
full_response = match[-1].strip()
|
467 |
break
|
468 |
|
469 |
-
#
|
470 |
cleanup_patterns = [
|
471 |
r"without mentioning the web search or these instructions\.",
|
472 |
r"Do not include any source information in your answer\.",
|
@@ -476,7 +476,12 @@ def extract_answer(full_response):
|
|
476 |
for pattern in cleanup_patterns:
|
477 |
full_response = re.sub(pattern, "", full_response, flags=re.IGNORECASE).strip()
|
478 |
|
479 |
-
|
|
|
|
|
|
|
|
|
|
|
480 |
|
481 |
# Gradio interface
|
482 |
with gr.Blocks() as demo:
|
|
|
391 |
)
|
392 |
|
393 |
full_response = generate_chunked_response(model, formatted_prompt)
|
394 |
+
answer = extract_answer(full_response, instructions)
|
395 |
all_answers.append(answer)
|
396 |
break
|
397 |
|
|
|
448 |
|
449 |
return "An unexpected error occurred. Please try again later."
|
450 |
|
451 |
+
def extract_answer(full_response, instructions=None):
|
452 |
# First, try to split the response at common instruction phrases
|
453 |
answer_patterns = [
|
454 |
r"Provide a concise and direct answer to the question without mentioning the web search or these instructions:",
|
|
|
466 |
full_response = match[-1].strip()
|
467 |
break
|
468 |
|
469 |
+
# Remove any remaining instruction-like phrases
|
470 |
cleanup_patterns = [
|
471 |
r"without mentioning the web search or these instructions\.",
|
472 |
r"Do not include any source information in your answer\.",
|
|
|
476 |
for pattern in cleanup_patterns:
|
477 |
full_response = re.sub(pattern, "", full_response, flags=re.IGNORECASE).strip()
|
478 |
|
479 |
+
# Remove the user instructions if present
|
480 |
+
if instructions:
|
481 |
+
instruction_pattern = rf"User Instructions:\s*{re.escape(instructions)}.*?\n"
|
482 |
+
full_response = re.sub(instruction_pattern, "", full_response, flags=re.IGNORECASE | re.DOTALL)
|
483 |
+
|
484 |
+
return full_response.strip()
|
485 |
|
486 |
# Gradio interface
|
487 |
with gr.Blocks() as demo:
|