new read function
Browse files
tools.py
CHANGED
|
@@ -71,84 +71,29 @@ def get_youtube_transcript(video_url: str) -> str:
|
|
| 71 |
except Exception as e:
|
| 72 |
return f"Unavailable. Error: {str(e)}"
|
| 73 |
|
| 74 |
-
|
| 75 |
|
| 76 |
-
|
|
|
|
| 77 |
"""
|
| 78 |
file_spec can be:
|
| 79 |
- a local file path (e.g., "data.xlsx")
|
| 80 |
- a task_id (e.g., "abc123") to download from GAIA
|
| 81 |
"""
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
download_url = f"https://agents-course-unit4-scoring.hf.space/files/{quote(file_spec)}"
|
| 91 |
-
print(f"📥 Downloading from: {download_url}")
|
| 92 |
try:
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
# Determinar extensión desde Content-Type o por defecto .xlsx
|
| 97 |
-
content_type = response.headers.get('content-type', '').lower()
|
| 98 |
-
if 'pdf' in content_type:
|
| 99 |
-
suffix = '.pdf'
|
| 100 |
-
elif 'excel' in content_type or 'sheet' in content_type:
|
| 101 |
-
suffix = '.xlsx'
|
| 102 |
-
elif 'json' in content_type:
|
| 103 |
-
suffix = '.json'
|
| 104 |
-
elif 'text' in content_type or 'csv' in content_type:
|
| 105 |
-
suffix = '.csv'
|
| 106 |
-
else:
|
| 107 |
-
suffix = '.xlsx' # fallback
|
| 108 |
-
|
| 109 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
|
| 110 |
-
tmp.write(response.content)
|
| 111 |
-
actual_path = tmp.name
|
| 112 |
-
print(f"✅ Saved as: {actual_path}")
|
| 113 |
-
|
| 114 |
except Exception as e:
|
| 115 |
-
return f"
|
| 116 |
|
| 117 |
-
else:
|
| 118 |
-
# 2. Buscar localmente
|
| 119 |
-
possible_paths = [file_spec, f"./{file_spec}", f"/tmp/{file_spec}"]
|
| 120 |
-
for p in possible_paths:
|
| 121 |
-
if os.path.exists(p):
|
| 122 |
-
actual_path = p
|
| 123 |
-
break
|
| 124 |
|
| 125 |
-
if not actual_path:
|
| 126 |
-
return f"File not found locally or downloadable: {file_spec}"
|
| 127 |
-
|
| 128 |
-
# 3. Leer (tu lógica existente)
|
| 129 |
-
try:
|
| 130 |
-
ext = os.path.splitext(actual_path)[1].lower()
|
| 131 |
-
if ext in ['.mp3', '.wav', '.m4a', '.flac', '.ogg']:
|
| 132 |
-
transcription = transcribe_audio_openai(actual_path)
|
| 133 |
-
result = f"Audio transcription: {transcription}"
|
| 134 |
-
elif ext in ['.txt', '.pdf', '.docx', '.csv', '.json', '.md', '.xlsx']:
|
| 135 |
-
reader = SimpleDirectoryReader(input_files=[actual_path])
|
| 136 |
-
docs = reader.load_data()
|
| 137 |
-
full_text = "\n\n".join(doc.text for doc in docs) if docs else ""
|
| 138 |
-
result = f"File: {os.path.basename(actual_path)}\n\n{full_text}"
|
| 139 |
-
else:
|
| 140 |
-
result = f"Unsupported file type: {ext}"
|
| 141 |
-
except Exception as e:
|
| 142 |
-
result = f"Error reading {actual_path}: {str(e)}"
|
| 143 |
-
finally:
|
| 144 |
-
# Limpiar temporal
|
| 145 |
-
if actual_path and actual_path not in possible_paths:
|
| 146 |
-
try:
|
| 147 |
-
os.unlink(actual_path)
|
| 148 |
-
except:
|
| 149 |
-
pass
|
| 150 |
-
|
| 151 |
-
return result
|
| 152 |
|
| 153 |
|
| 154 |
|
|
|
|
| 71 |
except Exception as e:
|
| 72 |
return f"Unavailable. Error: {str(e)}"
|
| 73 |
|
|
|
|
| 74 |
|
| 75 |
+
|
| 76 |
+
def read_document(file_path: str) -> str:
|
| 77 |
"""
|
| 78 |
file_spec can be:
|
| 79 |
- a local file path (e.g., "data.xlsx")
|
| 80 |
- a task_id (e.g., "abc123") to download from GAIA
|
| 81 |
"""
|
| 82 |
+
name = "read_file"
|
| 83 |
+
description = "Reads a file and returns its content"
|
| 84 |
+
inputs = {
|
| 85 |
+
"file_path": {"type": "string", "description": "Path to the file to read"},
|
| 86 |
+
}
|
| 87 |
+
output_type = "string"
|
| 88 |
+
|
| 89 |
+
def forward(self, file_path: str) -> str:
|
|
|
|
|
|
|
| 90 |
try:
|
| 91 |
+
with open(file_path, "r") as file:
|
| 92 |
+
return file.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
except Exception as e:
|
| 94 |
+
return f"Error reading file: {str(e)}"
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
|
| 99 |
|