Spaces:
Sleeping
Sleeping
get file
Browse files- .gitignore +6 -0
- app.py +65 -5
.gitignore
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/.idea/.gitignore
|
2 |
+
/.idea/Final_Assignment_Template.iml
|
3 |
+
/.idea/misc.xml
|
4 |
+
/.idea/modules.xml
|
5 |
+
/.idea/vcs.xml
|
6 |
+
/app2.py
|
app.py
CHANGED
@@ -4,21 +4,61 @@ import requests
|
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
from smolagents import OpenAIServerModel, DuckDuckGoSearchTool, PythonInterpreterTool, CodeAgent
|
|
|
|
|
7 |
|
8 |
# (Keep Constants as is)
|
9 |
# --- Constants ---
|
10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
# --- Basic Agent Definition ---
|
13 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
14 |
class BasicAgent:
|
15 |
def __init__(self):
|
16 |
-
print("BasicAgent initialized.")
|
17 |
-
def __call__(self, question: str) -> str:
|
18 |
self.agent = CodeAgent(
|
19 |
-
model
|
20 |
-
tools=[DuckDuckGoSearchTool(),
|
|
|
21 |
)
|
|
|
|
|
|
|
|
|
22 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
23 |
fixed_answer = self.agent.run(question)
|
24 |
print(f"Agent returning answer: {fixed_answer}")
|
@@ -81,11 +121,31 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
81 |
for item in questions_data:
|
82 |
task_id = item.get("task_id")
|
83 |
question_text = item.get("question")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
if not task_id or question_text is None:
|
85 |
print(f"Skipping item with missing task_id or question: {item}")
|
86 |
continue
|
87 |
try:
|
88 |
-
submitted_answer = agent(
|
89 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
90 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
91 |
except Exception as e:
|
|
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
from smolagents import OpenAIServerModel, DuckDuckGoSearchTool, PythonInterpreterTool, CodeAgent
|
7 |
+
from pathlib import Path
|
8 |
+
import tempfile
|
9 |
|
10 |
# (Keep Constants as is)
|
11 |
# --- Constants ---
|
12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
13 |
|
14 |
+
|
15 |
+
def download_file_if_any(base_api_url: str, task_id: str) -> str | None:
|
16 |
+
"""
|
17 |
+
Try GET /files/{task_id}.
|
18 |
+
• On HTTP 200 → save to a temp dir and return local path.
|
19 |
+
• On 404 → return None.
|
20 |
+
• On other errors → raise so caller can log / handle.
|
21 |
+
"""
|
22 |
+
url = f"{base_api_url}/files/{task_id}"
|
23 |
+
try:
|
24 |
+
resp = requests.get(url, timeout=30)
|
25 |
+
if resp.status_code == 404:
|
26 |
+
return None # no file
|
27 |
+
resp.raise_for_status() # raise on 4xx/5xx ≠ 404
|
28 |
+
except requests.exceptions.HTTPError as e:
|
29 |
+
# propagate non-404 errors (403, 500, …)
|
30 |
+
raise e
|
31 |
+
|
32 |
+
# ▸ Save bytes to a named file inside the system temp dir
|
33 |
+
# Try to keep original extension from Content-Disposition if present.
|
34 |
+
cdisp = resp.headers.get("content-disposition", "")
|
35 |
+
filename = task_id # default base name
|
36 |
+
if "filename=" in cdisp:
|
37 |
+
import re
|
38 |
+
m = re.search(r'filename="([^"]+)"', cdisp)
|
39 |
+
if m:
|
40 |
+
filename = m.group(1) # keep provided name
|
41 |
+
|
42 |
+
tmp_dir = Path(tempfile.gettempdir()) / "gaia_files"
|
43 |
+
tmp_dir.mkdir(exist_ok=True)
|
44 |
+
file_path = tmp_dir / filename
|
45 |
+
with open(file_path, "wb") as f:
|
46 |
+
f.write(resp.content)
|
47 |
+
return str(file_path)
|
48 |
+
|
49 |
# --- Basic Agent Definition ---
|
50 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
51 |
class BasicAgent:
|
52 |
def __init__(self):
|
|
|
|
|
53 |
self.agent = CodeAgent(
|
54 |
+
model=OpenAIServerModel(model_id="gpt-4o"),
|
55 |
+
tools=[DuckDuckGoSearchTool()],
|
56 |
+
add_base_tools=True,
|
57 |
)
|
58 |
+
|
59 |
+
print("BasicAgent initialized.")
|
60 |
+
|
61 |
+
def __call__(self, question: str) -> str:
|
62 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
63 |
fixed_answer = self.agent.run(question)
|
64 |
print(f"Agent returning answer: {fixed_answer}")
|
|
|
121 |
for item in questions_data:
|
122 |
task_id = item.get("task_id")
|
123 |
question_text = item.get("question")
|
124 |
+
|
125 |
+
# ----------fetch any attached file ----------
|
126 |
+
try:
|
127 |
+
file_path = download_file_if_any(api_url, task_id)
|
128 |
+
except Exception as e:
|
129 |
+
file_path = None
|
130 |
+
print(f"[file fetch error] {task_id}: {e}")
|
131 |
+
|
132 |
+
# ---------- Build the prompt sent to the agent ----------
|
133 |
+
if file_path:
|
134 |
+
q_for_agent = (
|
135 |
+
f"{question_text}\n\n"
|
136 |
+
f"---\n"
|
137 |
+
f"A file was downloaded for this task and saved locally at:\n"
|
138 |
+
f"{file_path}\n"
|
139 |
+
f"---\n\n"
|
140 |
+
)
|
141 |
+
else:
|
142 |
+
q_for_agent = question_text
|
143 |
+
|
144 |
if not task_id or question_text is None:
|
145 |
print(f"Skipping item with missing task_id or question: {item}")
|
146 |
continue
|
147 |
try:
|
148 |
+
submitted_answer = agent(q_for_agent)
|
149 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
150 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
151 |
except Exception as e:
|