Spaces:
Runtime error
Runtime error
Enhance file handling in app.py by adding functionality to download associated files for questions, create a download directory, and log download status. Update agent invocation to include file paths for processing.
Browse files
app.py
CHANGED
|
@@ -8,11 +8,15 @@ from agent import BasicAgent
|
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 13 |
"""
|
| 14 |
-
Fetches all questions, runs the BasicAgent on them,
|
| 15 |
-
and displays the results.
|
| 16 |
"""
|
| 17 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 18 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
|
@@ -28,6 +32,14 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 28 |
questions_url = f"{api_url}/questions"
|
| 29 |
submit_url = f"{api_url}/submit"
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 32 |
try:
|
| 33 |
agent = BasicAgent()
|
|
@@ -66,16 +78,60 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 66 |
for item in questions_data:
|
| 67 |
task_id = item.get("task_id")
|
| 68 |
question_text = item.get("question")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
if not task_id or question_text is None:
|
| 70 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 71 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
try:
|
| 73 |
-
submitted_answer = agent(question_text)
|
| 74 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 75 |
-
results_log.append({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
except Exception as e:
|
|
|
|
| 77 |
print(f"Error running agent on task {task_id}: {e}")
|
| 78 |
-
results_log.append({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
if not answers_payload:
|
| 81 |
print("Agent did not produce any answers to submit.")
|
|
|
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
+
DOWNLOAD_DIR = "downloaded_task_files" # Directory to save downloaded files
|
| 12 |
+
|
| 13 |
+
# --- Basic Agent Definition is now imported from agent.py ---
|
| 14 |
+
# (Remove the inline BasicAgent class definition from here if it exists)
|
| 15 |
|
| 16 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 17 |
"""
|
| 18 |
+
Fetches all questions, downloads associated files, runs the BasicAgent on them,
|
| 19 |
+
submits all answers, and displays the results.
|
| 20 |
"""
|
| 21 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 22 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
|
|
|
| 32 |
questions_url = f"{api_url}/questions"
|
| 33 |
submit_url = f"{api_url}/submit"
|
| 34 |
|
| 35 |
+
# Create download directory if it doesn't exist
|
| 36 |
+
try:
|
| 37 |
+
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
|
| 38 |
+
print(f"Ensured download directory exists: {DOWNLOAD_DIR}")
|
| 39 |
+
except OSError as e:
|
| 40 |
+
print(f"Error creating download directory {DOWNLOAD_DIR}: {e}")
|
| 41 |
+
return f"Error creating download directory: {e}", None
|
| 42 |
+
|
| 43 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 44 |
try:
|
| 45 |
agent = BasicAgent()
|
|
|
|
| 78 |
for item in questions_data:
|
| 79 |
task_id = item.get("task_id")
|
| 80 |
question_text = item.get("question")
|
| 81 |
+
file_name = item.get("file_name")
|
| 82 |
+
local_file_path = None
|
| 83 |
+
download_status = "No file specified"
|
| 84 |
+
|
| 85 |
if not task_id or question_text is None:
|
| 86 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 87 |
continue
|
| 88 |
+
|
| 89 |
+
if file_name:
|
| 90 |
+
# Construct download URL and local file path
|
| 91 |
+
# Assuming files are served from a /files/ endpoint on the API server
|
| 92 |
+
download_url = f"{api_url}/files/{file_name}"
|
| 93 |
+
local_file_path = os.path.join(DOWNLOAD_DIR, file_name)
|
| 94 |
+
print(f"Attempting to download file for task {task_id}: {file_name} from {download_url}")
|
| 95 |
+
try:
|
| 96 |
+
file_response = requests.get(download_url, stream=True, timeout=30)
|
| 97 |
+
file_response.raise_for_status()
|
| 98 |
+
with open(local_file_path, 'wb') as f:
|
| 99 |
+
for chunk in file_response.iter_content(chunk_size=8192):
|
| 100 |
+
f.write(chunk)
|
| 101 |
+
download_status = f"Successfully downloaded to {local_file_path}"
|
| 102 |
+
print(download_status)
|
| 103 |
+
except requests.exceptions.RequestException as e:
|
| 104 |
+
download_status = f"Failed to download {file_name}: {e}"
|
| 105 |
+
print(download_status)
|
| 106 |
+
local_file_path = None # Ensure agent doesn't get a path to a non-existent/failed file
|
| 107 |
+
except Exception as e:
|
| 108 |
+
download_status = f"An unexpected error occurred downloading {file_name}: {e}"
|
| 109 |
+
print(download_status)
|
| 110 |
+
local_file_path = None
|
| 111 |
+
|
| 112 |
+
|
| 113 |
try:
|
| 114 |
+
submitted_answer = agent(question_text, file_path=local_file_path)
|
| 115 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 116 |
+
results_log.append({
|
| 117 |
+
"Task ID": task_id,
|
| 118 |
+
"Question": question_text,
|
| 119 |
+
"File Name": file_name if file_name else "N/A",
|
| 120 |
+
"Download Status": download_status,
|
| 121 |
+
"Local File Path": local_file_path if local_file_path else "N/A",
|
| 122 |
+
"Submitted Answer": submitted_answer
|
| 123 |
+
})
|
| 124 |
except Exception as e:
|
| 125 |
+
error_message = f"AGENT ERROR: {e}"
|
| 126 |
print(f"Error running agent on task {task_id}: {e}")
|
| 127 |
+
results_log.append({
|
| 128 |
+
"Task ID": task_id,
|
| 129 |
+
"Question": question_text,
|
| 130 |
+
"File Name": file_name if file_name else "N/A",
|
| 131 |
+
"Download Status": download_status,
|
| 132 |
+
"Local File Path": local_file_path if local_file_path else "N/A",
|
| 133 |
+
"Submitted Answer": error_message
|
| 134 |
+
})
|
| 135 |
|
| 136 |
if not answers_payload:
|
| 137 |
print("Agent did not produce any answers to submit.")
|