Spaces:
Sleeping
Sleeping
Add answer caching
Browse files
app.py
CHANGED
|
@@ -71,10 +71,11 @@ class MasterAgent:
|
|
| 71 |
|
| 72 |
print("Starting instantiation of master")
|
| 73 |
self.master_agent = CodeAgent(
|
| 74 |
-
tools=[
|
|
|
|
| 75 |
WebSearchTool(),
|
| 76 |
analyze_image,
|
| 77 |
-
|
| 78 |
model=model,
|
| 79 |
add_base_tools=True,
|
| 80 |
planning_interval=5,
|
|
@@ -167,10 +168,27 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 167 |
cache_dir = "file_cache"
|
| 168 |
if not os.path.exists(cache_dir):
|
| 169 |
os.makedirs(cache_dir)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
for item in questions_data:
|
| 172 |
task_id = item.get("task_id")
|
| 173 |
question_text = item.get("question")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
attached_file = item.get("file_name")
|
| 175 |
if attached_file != "":
|
| 176 |
local_file_path = os.path.join(cache_dir, attached_file)
|
|
@@ -199,9 +217,15 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 199 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 200 |
continue
|
| 201 |
try:
|
| 202 |
-
|
|
|
|
|
|
|
| 203 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 204 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
except Exception as e:
|
| 206 |
print(f"Error running agent on task {task_id}: {e}")
|
| 207 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
|
|
| 71 |
|
| 72 |
print("Starting instantiation of master")
|
| 73 |
self.master_agent = CodeAgent(
|
| 74 |
+
tools=[
|
| 75 |
+
VisitWebpageTool(),
|
| 76 |
WebSearchTool(),
|
| 77 |
analyze_image,
|
| 78 |
+
],
|
| 79 |
model=model,
|
| 80 |
add_base_tools=True,
|
| 81 |
planning_interval=5,
|
|
|
|
| 168 |
cache_dir = "file_cache"
|
| 169 |
if not os.path.exists(cache_dir):
|
| 170 |
os.makedirs(cache_dir)
|
| 171 |
+
|
| 172 |
+
answers_dir = "answers_cache"
|
| 173 |
+
if not os.path.exists(answers_dir):
|
| 174 |
+
os.makedirs(answers_dir)
|
| 175 |
|
| 176 |
for item in questions_data:
|
| 177 |
task_id = item.get("task_id")
|
| 178 |
question_text = item.get("question")
|
| 179 |
+
|
| 180 |
+
task_answer_path = os.path.join(cache_dir, task_id, '.yaml')
|
| 181 |
+
if os.path.exists(task_answer_path):
|
| 182 |
+
print(f"Found existing answer for task {task_id}, loading")
|
| 183 |
+
with open(task_answer_path, 'r') as stream:
|
| 184 |
+
task_answer = yaml.safe_load(stream)
|
| 185 |
+
|
| 186 |
+
try:
|
| 187 |
+
submitted_answer = task_answer.get("submitted_answer")
|
| 188 |
+
except Exception as e:
|
| 189 |
+
print(f"Existing answer for task {task_id} is invalid.")
|
| 190 |
+
submitted_answer = None
|
| 191 |
+
|
| 192 |
attached_file = item.get("file_name")
|
| 193 |
if attached_file != "":
|
| 194 |
local_file_path = os.path.join(cache_dir, attached_file)
|
|
|
|
| 217 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 218 |
continue
|
| 219 |
try:
|
| 220 |
+
if not submitted_answer:
|
| 221 |
+
submitted_answer = agent(question_text, attached_file)
|
| 222 |
+
|
| 223 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 224 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 225 |
+
|
| 226 |
+
with open(task_answer_path, 'w'):
|
| 227 |
+
yaml.dump({"task_id": task_id, "question_text": question_text, "submitted_answer": submitted_answer})
|
| 228 |
+
|
| 229 |
except Exception as e:
|
| 230 |
print(f"Error running agent on task {task_id}: {e}")
|
| 231 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|