Update app.py
Browse files
app.py
CHANGED
|
@@ -15,6 +15,21 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 15 |
from smolagents import CodeAgent, LiteLLMModel
|
| 16 |
from my_tools import my_tool_list
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
class BasicAgent:
|
| 19 |
def __init__(self):
|
| 20 |
api_key = os.getenv("OPENAI_API_KEY") # ← Read enviroment variables in space.
|
|
@@ -121,19 +136,39 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 121 |
results_log = []
|
| 122 |
answers_payload = []
|
| 123 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 124 |
-
for idx, item in enumerate(questions_data[:
|
| 125 |
task_id = item.get("task_id")
|
| 126 |
question_text = item.get("question")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
print(f"===== [Celum is answering No. {idx+1}/{len(questions_data)} ] =====")
|
| 128 |
try:
|
| 129 |
submitted_answer = safe_run_agent(agent, question_text, idx, len(questions_data))
|
| 130 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 131 |
-
results_log.append({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
except Exception as e:
|
| 133 |
print(f"[Celum Error at Q{idx+1}]: {e}")
|
| 134 |
-
results_log.append({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
time.sleep(7)
|
| 136 |
-
|
| 137 |
if not answers_payload:
|
| 138 |
print("Agent did not produce any answers to submit.")
|
| 139 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
|
|
|
| 15 |
from smolagents import CodeAgent, LiteLLMModel
|
| 16 |
from my_tools import my_tool_list
|
| 17 |
|
| 18 |
+
def download_file(task_id, filename, save_dir="attachments"):
|
| 19 |
+
os.makedirs(save_dir, exist_ok=True)
|
| 20 |
+
url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}"
|
| 21 |
+
save_path = os.path.join(save_dir, filename)
|
| 22 |
+
try:
|
| 23 |
+
resp = requests.get(url, timeout=15)
|
| 24 |
+
resp.raise_for_status()
|
| 25 |
+
with open(save_path, "wb") as f:
|
| 26 |
+
f.write(resp.content)
|
| 27 |
+
print(f"Downloaded attachment for task {task_id} -> {save_path}")
|
| 28 |
+
return save_path
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f"Attachment download failed for {task_id}: {e}")
|
| 31 |
+
return None
|
| 32 |
+
|
| 33 |
class BasicAgent:
|
| 34 |
def __init__(self):
|
| 35 |
api_key = os.getenv("OPENAI_API_KEY") # ← Read enviroment variables in space.
|
|
|
|
| 136 |
results_log = []
|
| 137 |
answers_payload = []
|
| 138 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 139 |
+
for idx, item in enumerate(questions_data[3:4]):
|
| 140 |
task_id = item.get("task_id")
|
| 141 |
question_text = item.get("question")
|
| 142 |
+
file_list = item.get("files", [])
|
| 143 |
+
|
| 144 |
+
local_files = []
|
| 145 |
+
for fname in file_list:
|
| 146 |
+
fpath = download_file(task_id, fname)
|
| 147 |
+
if fpath:
|
| 148 |
+
local_files.append(fpath)
|
| 149 |
+
if local_files:
|
| 150 |
+
print(f"Downloaded attachments: {local_files}")
|
| 151 |
+
|
| 152 |
print(f"===== [Celum is answering No. {idx+1}/{len(questions_data)} ] =====")
|
| 153 |
try:
|
| 154 |
submitted_answer = safe_run_agent(agent, question_text, idx, len(questions_data))
|
| 155 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 156 |
+
results_log.append({
|
| 157 |
+
"Task ID": task_id,
|
| 158 |
+
"Question": question_text,
|
| 159 |
+
"Submitted Answer": submitted_answer,
|
| 160 |
+
"Files": local_files
|
| 161 |
+
})
|
| 162 |
except Exception as e:
|
| 163 |
print(f"[Celum Error at Q{idx+1}]: {e}")
|
| 164 |
+
results_log.append({
|
| 165 |
+
"Task ID": task_id,
|
| 166 |
+
"Question": question_text,
|
| 167 |
+
"Submitted Answer": f"AGENT ERROR: {e}",
|
| 168 |
+
"Files": local_files
|
| 169 |
+
})
|
| 170 |
time.sleep(7)
|
| 171 |
+
|
| 172 |
if not answers_payload:
|
| 173 |
print("Agent did not produce any answers to submit.")
|
| 174 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|