Thanh Vinh Vo
commited on
Commit
·
d0e6fef
1
Parent(s):
d95fdbc
update
Browse files
app.py
CHANGED
|
@@ -285,13 +285,17 @@ def get_wikipedia_section_tables(
|
|
| 285 |
def download_file(question_id: str, file_name: str) -> str:
|
| 286 |
"""
|
| 287 |
A tool that downloads file that was mentioned in a question and store it as local file.
|
| 288 |
-
|
|
|
|
| 289 |
Args:
|
| 290 |
question_id: Question ID.
|
| 291 |
file_name: File name.
|
| 292 |
Returns:
|
| 293 |
-
str:
|
|
|
|
|
|
|
| 294 |
"""
|
|
|
|
| 295 |
import os
|
| 296 |
|
| 297 |
import requests
|
|
@@ -317,15 +321,41 @@ def download_file(question_id: str, file_name: str) -> str:
|
|
| 317 |
# Use original filename directly
|
| 318 |
local_path = file_name
|
| 319 |
|
| 320 |
-
# Save the
|
| 321 |
with open(local_path, "wb") as f:
|
| 322 |
f.write(response.content)
|
| 323 |
|
| 324 |
print(f"File saved to: {local_path}")
|
| 325 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
|
| 327 |
except requests.exceptions.RequestException as e:
|
| 328 |
-
raise ValueError(f"Failed to
|
| 329 |
except Exception as e:
|
| 330 |
# Print first 200 characters of response content for debugging
|
| 331 |
content_preview = (
|
|
@@ -334,7 +364,7 @@ def download_file(question_id: str, file_name: str) -> str:
|
|
| 334 |
else b"No response"
|
| 335 |
)
|
| 336 |
print(f"Error downloading file. Content preview: {content_preview}")
|
| 337 |
-
raise ValueError(f"Failed to download
|
| 338 |
|
| 339 |
|
| 340 |
# --- Basic Agent Definition ---
|
|
@@ -373,7 +403,6 @@ class BasicAgent:
|
|
| 373 |
"pytubefix",
|
| 374 |
"pytubefix.cli",
|
| 375 |
"youtube_transcript_api",
|
| 376 |
-
"open",
|
| 377 |
],
|
| 378 |
name="multimodal_agent",
|
| 379 |
description="""
|
|
@@ -415,7 +444,6 @@ class BasicAgent:
|
|
| 415 |
"pytubefix",
|
| 416 |
"pytubefix.cli",
|
| 417 |
"youtube_transcript_api",
|
| 418 |
-
"open",
|
| 419 |
],
|
| 420 |
planning_interval=2,
|
| 421 |
max_steps=10,
|
|
|
|
| 285 |
def download_file(question_id: str, file_name: str) -> str:
|
| 286 |
"""
|
| 287 |
A tool that downloads file that was mentioned in a question and store it as local file.
|
| 288 |
+
Returns a JSON string containing the file path and optionally the text content if the file has a text MIME type.
|
| 289 |
+
|
| 290 |
Args:
|
| 291 |
question_id: Question ID.
|
| 292 |
file_name: File name.
|
| 293 |
Returns:
|
| 294 |
+
str: JSON string containing file information. Structure:
|
| 295 |
+
- For text files: {"path": "local_path", "content": "file_content"}
|
| 296 |
+
- For non-text files: {"path": "local_path"}
|
| 297 |
"""
|
| 298 |
+
import json
|
| 299 |
import os
|
| 300 |
|
| 301 |
import requests
|
|
|
|
| 321 |
# Use original filename directly
|
| 322 |
local_path = file_name
|
| 323 |
|
| 324 |
+
# Save the file locally
|
| 325 |
with open(local_path, "wb") as f:
|
| 326 |
f.write(response.content)
|
| 327 |
|
| 328 |
print(f"File saved to: {local_path}")
|
| 329 |
+
|
| 330 |
+
# Check if the file has a text MIME type
|
| 331 |
+
text_mime_types = [
|
| 332 |
+
"text/",
|
| 333 |
+
"application/json",
|
| 334 |
+
"application/xml",
|
| 335 |
+
"application/javascript",
|
| 336 |
+
"application/csv",
|
| 337 |
+
"application/x-csv",
|
| 338 |
+
"text/csv",
|
| 339 |
+
]
|
| 340 |
+
|
| 341 |
+
is_text_file = any(
|
| 342 |
+
content_type.startswith(mime_type) for mime_type in text_mime_types
|
| 343 |
+
)
|
| 344 |
+
|
| 345 |
+
result = {"path": local_path}
|
| 346 |
+
|
| 347 |
+
if is_text_file:
|
| 348 |
+
# Decode response content directly as text using UTF-8
|
| 349 |
+
text_content = response.content.decode("utf-8")
|
| 350 |
+
result["content"] = text_content
|
| 351 |
+
print(
|
| 352 |
+
f"Added text content to result (length: {len(text_content)} characters)"
|
| 353 |
+
)
|
| 354 |
+
|
| 355 |
+
return json.dumps(result)
|
| 356 |
|
| 357 |
except requests.exceptions.RequestException as e:
|
| 358 |
+
raise ValueError(f"Failed to download file from {url}: {e}")
|
| 359 |
except Exception as e:
|
| 360 |
# Print first 200 characters of response content for debugging
|
| 361 |
content_preview = (
|
|
|
|
| 364 |
else b"No response"
|
| 365 |
)
|
| 366 |
print(f"Error downloading file. Content preview: {content_preview}")
|
| 367 |
+
raise ValueError(f"Failed to download file from {url}: {e}")
|
| 368 |
|
| 369 |
|
| 370 |
# --- Basic Agent Definition ---
|
|
|
|
| 403 |
"pytubefix",
|
| 404 |
"pytubefix.cli",
|
| 405 |
"youtube_transcript_api",
|
|
|
|
| 406 |
],
|
| 407 |
name="multimodal_agent",
|
| 408 |
description="""
|
|
|
|
| 444 |
"pytubefix",
|
| 445 |
"pytubefix.cli",
|
| 446 |
"youtube_transcript_api",
|
|
|
|
| 447 |
],
|
| 448 |
planning_interval=2,
|
| 449 |
max_steps=10,
|