Update app.py
Browse files
app.py
CHANGED
|
@@ -15,22 +15,30 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 15 |
# --- Agent Definition ---
|
| 16 |
|
| 17 |
# 1. Define Your Tools
|
|
|
|
| 18 |
@tool
|
| 19 |
def file_reader(file_path: str) -> str:
|
| 20 |
-
"""
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
"""
|
| 24 |
try:
|
| 25 |
if file_path.startswith("http://") or file_path.startswith("https://"):
|
| 26 |
response = requests.get(file_path, timeout=20)
|
| 27 |
response.raise_for_status()
|
|
|
|
| 28 |
with open("temp_file", "wb") as f:
|
| 29 |
f.write(response.content)
|
| 30 |
elements = partition("temp_file")
|
| 31 |
os.remove("temp_file") # Clean up
|
| 32 |
else:
|
|
|
|
| 33 |
elements = partition(file_path)
|
|
|
|
| 34 |
return "\n\n".join([str(el) for el in elements])
|
| 35 |
except Exception as e:
|
| 36 |
return f"Error reading or processing file '{file_path}': {e}"
|
|
|
|
| 15 |
# --- Agent Definition ---
|
| 16 |
|
| 17 |
# 1. Define Your Tools
|
| 18 |
+
|
| 19 |
@tool
|
| 20 |
def file_reader(file_path: str) -> str:
|
| 21 |
+
"""Reads the content of a file and returns its text content.
|
| 22 |
+
|
| 23 |
+
This tool supports various file types like PDF, TXT, CSV, etc., from either
|
| 24 |
+
a local path or a web URL.
|
| 25 |
+
|
| 26 |
+
Args:
|
| 27 |
+
file_path (str): The local path or web URL of the file to be read.
|
| 28 |
"""
|
| 29 |
try:
|
| 30 |
if file_path.startswith("http://") or file_path.startswith("https://"):
|
| 31 |
response = requests.get(file_path, timeout=20)
|
| 32 |
response.raise_for_status()
|
| 33 |
+
# Use a temporary file to process with unstructured
|
| 34 |
with open("temp_file", "wb") as f:
|
| 35 |
f.write(response.content)
|
| 36 |
elements = partition("temp_file")
|
| 37 |
os.remove("temp_file") # Clean up
|
| 38 |
else:
|
| 39 |
+
# Assumes it's a local path within the Space
|
| 40 |
elements = partition(file_path)
|
| 41 |
+
|
| 42 |
return "\n\n".join([str(el) for el in elements])
|
| 43 |
except Exception as e:
|
| 44 |
return f"Error reading or processing file '{file_path}': {e}"
|