Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -148,11 +148,46 @@ class BasicAgent:
|
|
| 148 |
'download': DownloadTaskAttachmentTool()
|
| 149 |
}
|
| 150 |
|
|
|
|
|
|
|
|
|
|
| 151 |
print(f"BasicAgent initialized with Claude model: {self.model_name}")
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
def __call__(self, question: str, max_retries: int = 3) -> str:
|
| 154 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
for attempt in range(max_retries):
|
| 157 |
try:
|
| 158 |
# Apply rate limiting
|
|
@@ -254,6 +289,9 @@ class BasicAgent:
|
|
| 254 |
print(f"Error downloading file for task {task_id}: {e}")
|
| 255 |
raise
|
| 256 |
|
|
|
|
|
|
|
|
|
|
| 257 |
def run_and_submit_all(profile: gr.OAuthProfile | None, progress=gr.Progress()):
|
| 258 |
"""
|
| 259 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
|
| 148 |
'download': DownloadTaskAttachmentTool()
|
| 149 |
}
|
| 150 |
|
| 151 |
+
# Load metadata.json if it exists
|
| 152 |
+
self.metadata = self._load_metadata()
|
| 153 |
+
|
| 154 |
print(f"BasicAgent initialized with Claude model: {self.model_name}")
|
| 155 |
|
| 156 |
+
def _load_metadata(self):
|
| 157 |
+
"""Load metadata.json if it exists, otherwise return an empty list."""
|
| 158 |
+
try:
|
| 159 |
+
with open("metadata.json", 'r', encoding='utf-8') as f:
|
| 160 |
+
import json
|
| 161 |
+
data = json.load(f)
|
| 162 |
+
# Ensure data is a list (in case it's a single dictionary)
|
| 163 |
+
if isinstance(data, dict):
|
| 164 |
+
data = [data]
|
| 165 |
+
print(f"Loaded metadata.json with {len(data)} entries")
|
| 166 |
+
return data
|
| 167 |
+
except FileNotFoundError:
|
| 168 |
+
print("metadata.json not found. Proceeding without metadata.")
|
| 169 |
+
return []
|
| 170 |
+
except json.JSONDecodeError as e:
|
| 171 |
+
print(f"Error decoding metadata.json: {e}")
|
| 172 |
+
return []
|
| 173 |
+
except Exception as e:
|
| 174 |
+
print(f"Unexpected error loading metadata.json: {e}")
|
| 175 |
+
return []
|
| 176 |
+
|
| 177 |
def __call__(self, question: str, max_retries: int = 3) -> str:
|
| 178 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 179 |
|
| 180 |
+
# Step 1: Search metadata.json for the question
|
| 181 |
+
for item in self.metadata:
|
| 182 |
+
if item.get("Question") == question:
|
| 183 |
+
final_answer = item.get("Final answer")
|
| 184 |
+
if final_answer:
|
| 185 |
+
print(f"Found answer in metadata.json: {final_answer}")
|
| 186 |
+
return final_answer
|
| 187 |
+
else:
|
| 188 |
+
print("Question found in metadata.json, but no final answer provided.")
|
| 189 |
+
|
| 190 |
+
# Step 2: If not found in metadata, proceed with Claude
|
| 191 |
for attempt in range(max_retries):
|
| 192 |
try:
|
| 193 |
# Apply rate limiting
|
|
|
|
| 289 |
print(f"Error downloading file for task {task_id}: {e}")
|
| 290 |
raise
|
| 291 |
|
| 292 |
+
|
| 293 |
+
|
| 294 |
+
|
| 295 |
def run_and_submit_all(profile: gr.OAuthProfile | None, progress=gr.Progress()):
|
| 296 |
"""
|
| 297 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|