Spaces:
Sleeping
Sleeping
Samuel Thomas
commited on
Commit
·
5145d77
1
Parent(s):
9c250a6
strip final answer
Browse files
app.py
CHANGED
|
@@ -4,9 +4,28 @@ import requests
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
import traceback
|
|
|
|
| 7 |
from huggingface_hub import login
|
| 8 |
from tools import create_memory_safe_workflow, get_file_type, write_bytes_to_temp_dir, AgentState, extract_final_answer, run_agent
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# (Keep Constants as is)
|
| 11 |
# --- Constants ---
|
| 12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
@@ -105,7 +124,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 105 |
task_id = hf_questions[r]['task_id']
|
| 106 |
question_text = hf_questions[r]['question']
|
| 107 |
full_answer = run_agent(agent, s)
|
| 108 |
-
submitted_answer = extract_final_answer(full_answer[-1].content)
|
| 109 |
print(f"\n\nQuestion {r+1} Answer: {submitted_answer}\n\n")
|
| 110 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 111 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
import traceback
|
| 7 |
+
import re
|
| 8 |
from huggingface_hub import login
|
| 9 |
from tools import create_memory_safe_workflow, get_file_type, write_bytes_to_temp_dir, AgentState, extract_final_answer, run_agent
|
| 10 |
|
| 11 |
+
import re
|
| 12 |
+
|
| 13 |
+
def strip_final_answer(text):
|
| 14 |
+
"""
|
| 15 |
+
Removes 'FINAL ANSWER:' (case-insensitive) and all following whitespace from the start of the string.
|
| 16 |
+
Returns the remainder of the string.
|
| 17 |
+
"""
|
| 18 |
+
# The regex matches 'FINAL ANSWER:', optional colon, and all whitespace after it
|
| 19 |
+
return re.sub(r'^\s*FINAL ANSWER:\s*', '', text, flags=re.IGNORECASE)
|
| 20 |
+
|
| 21 |
+
# Example usage:
|
| 22 |
+
s = "FINAL ANSWER: Joe Torre"
|
| 23 |
+
print(strip_final_answer(s)) # Output: Joe Torre
|
| 24 |
+
|
| 25 |
+
s2 = " FINAL ANSWER: Jane Doe"
|
| 26 |
+
print(strip_final_answer(s2)) # Output: Jane Doe
|
| 27 |
+
|
| 28 |
+
|
| 29 |
# (Keep Constants as is)
|
| 30 |
# --- Constants ---
|
| 31 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 124 |
task_id = hf_questions[r]['task_id']
|
| 125 |
question_text = hf_questions[r]['question']
|
| 126 |
full_answer = run_agent(agent, s)
|
| 127 |
+
submitted_answer = strip_final_answer(extract_final_answer(full_answer[-1].content))
|
| 128 |
print(f"\n\nQuestion {r+1} Answer: {submitted_answer}\n\n")
|
| 129 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 130 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|