Update app.py
Browse files
app.py
CHANGED
|
@@ -136,6 +136,42 @@ class BasicAgent:
|
|
| 136 |
return error_msg
|
| 137 |
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 140 |
"""
|
| 141 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
@@ -196,6 +232,9 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 196 |
task_id = item.get("task_id")
|
| 197 |
question_text = item.get("question")
|
| 198 |
file_name = item.get("file_name")
|
|
|
|
|
|
|
|
|
|
| 199 |
if not task_id or question_text is None:
|
| 200 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 201 |
continue
|
|
|
|
| 136 |
return error_msg
|
| 137 |
|
| 138 |
|
| 139 |
+
def download_file(task_id, base_url, filename="MY_NAME", headers=None):
|
| 140 |
+
"""
|
| 141 |
+
Download a file from the API endpoint and save it locally.
|
| 142 |
+
|
| 143 |
+
Args:
|
| 144 |
+
task_id (str): The task ID for the file to download
|
| 145 |
+
base_url (str): Base URL of the API (e.g., 'https://api.example.com')
|
| 146 |
+
filename (str): Local filename to save as (default: 'MY_NAME')
|
| 147 |
+
headers (dict): Optional headers for authentication/authorization
|
| 148 |
+
|
| 149 |
+
Returns:
|
| 150 |
+
bool: True if download successful, False otherwise
|
| 151 |
+
"""
|
| 152 |
+
try:
|
| 153 |
+
# Construct the full URL
|
| 154 |
+
url = f"{base_url}/files/{task_id}"
|
| 155 |
+
|
| 156 |
+
# Make the GET request
|
| 157 |
+
response = requests.get(url, headers=headers, stream=True)
|
| 158 |
+
|
| 159 |
+
# Check if request was successful
|
| 160 |
+
response.raise_for_status()
|
| 161 |
+
|
| 162 |
+
# Save the file locally
|
| 163 |
+
with open(filename, 'wb') as file:
|
| 164 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 165 |
+
if chunk:
|
| 166 |
+
file.write(chunk)
|
| 167 |
+
|
| 168 |
+
print(f"File downloaded successfully as '{filename}'")
|
| 169 |
+
return True
|
| 170 |
+
except Exception as e:
|
| 171 |
+
print(f"An exception occurred during file download: {e}")
|
| 172 |
+
return False
|
| 173 |
+
|
| 174 |
+
|
| 175 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 176 |
"""
|
| 177 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
|
| 232 |
task_id = item.get("task_id")
|
| 233 |
question_text = item.get("question")
|
| 234 |
file_name = item.get("file_name")
|
| 235 |
+
if file_name:
|
| 236 |
+
# download and save the file
|
| 237 |
+
download_file(task_id=task_id, base_url=api_url, filename=file_name)
|
| 238 |
if not task_id or question_text is None:
|
| 239 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 240 |
continue
|