Spaces:
Sleeping
Sleeping
File size: 5,012 Bytes
3894cc5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | """Custom smolagents tools for GAIA multimodal and file tasks."""
from __future__ import annotations
import base64
import os
import re
import subprocess
import sys
from pathlib import Path
import pandas as pd
from huggingface_hub import InferenceClient
from smolagents import tool
from youtube_transcript_api import YouTubeTranscriptApi
def _hf_client() -> InferenceClient:
token = os.getenv("HF_TOKEN")
return InferenceClient(token=token)
def _vision_model() -> str:
return os.getenv("HF_VISION_MODEL", "Qwen/Qwen2-VL-7B-Instruct")
def _asr_model() -> str:
return os.getenv("HF_ASR_MODEL", "openai/whisper-large-v3")
@tool
def read_spreadsheet(file_path: str) -> str:
"""Read an Excel or CSV file and return its contents as text for analysis.
Args:
file_path: Absolute or relative path to .xlsx, .xls, or .csv file.
"""
path = Path(file_path)
if not path.exists():
return f"File not found: {file_path}"
suffix = path.suffix.lower()
if suffix == ".csv":
df = pd.read_csv(path)
elif suffix in {".xlsx", ".xls"}:
df = pd.read_excel(path)
else:
return f"Unsupported spreadsheet type: {suffix}"
buffer = []
buffer.append(f"Shape: {df.shape[0]} rows x {df.shape[1]} columns")
buffer.append(f"Columns: {', '.join(str(c) for c in df.columns)}")
buffer.append("\n--- data ---")
buffer.append(df.to_string(index=False))
text = "\n".join(buffer)
return text[:50000]
@tool
def execute_python_file(file_path: str) -> str:
"""Execute a Python file in a subprocess and return stdout/stderr.
Args:
file_path: Path to a .py file to run.
"""
path = Path(file_path)
if not path.exists():
return f"File not found: {file_path}"
if path.suffix.lower() != ".py":
return f"Not a Python file: {file_path}"
try:
completed = subprocess.run(
[sys.executable, str(path.resolve())],
capture_output=True,
text=True,
timeout=45,
cwd=str(path.parent.resolve()),
)
except subprocess.TimeoutExpired:
return "Execution timed out after 45 seconds."
parts = []
if completed.stdout:
parts.append(f"STDOUT:\n{completed.stdout}")
if completed.stderr:
parts.append(f"STDERR:\n{completed.stderr}")
parts.append(f"Exit code: {completed.returncode}")
return "\n".join(parts)[:20000]
@tool
def transcribe_audio(file_path: str) -> str:
"""Transcribe speech from an audio file (mp3/wav) to text.
Args:
file_path: Path to the audio file.
"""
path = Path(file_path)
if not path.exists():
return f"File not found: {file_path}"
client = _hf_client()
with path.open("rb") as audio_file:
result = client.automatic_speech_recognition(
audio=audio_file.read(),
model=_asr_model(),
)
if isinstance(result, dict):
return str(result.get("text", result))[:20000]
return str(getattr(result, "text", result))[:20000]
@tool
def analyze_image(file_path: str, question: str) -> str:
"""Analyze an image file to answer a specific question about it.
Args:
file_path: Path to png/jpg/jpeg/webp image.
question: What to determine from the image.
"""
path = Path(file_path)
if not path.exists():
return f"File not found: {file_path}"
mime = {
".png": "image/png",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".webp": "image/webp",
}.get(path.suffix.lower(), "image/png")
encoded = base64.b64encode(path.read_bytes()).decode("ascii")
client = _hf_client()
response = client.chat_completion(
model=_vision_model(),
messages=[
{
"role": "user",
"content": [
{"type": "image_url", "image_url": {"url": f"data:{mime};base64,{encoded}"}},
{"type": "text", "text": question},
],
}
],
max_tokens=1024,
)
return response.choices[0].message.content.strip()[:10000]
@tool
def get_youtube_transcript(video_url: str) -> str:
"""Fetch the transcript/captions of a YouTube video.
Args:
video_url: Full YouTube URL or 11-character video ID.
"""
match = re.search(
r"(?:youtube\.com/watch\?v=|youtu\.be/|youtube\.com/embed/)([A-Za-z0-9_-]{11})",
video_url,
)
video_id = match.group(1) if match else video_url.strip()
try:
api = YouTubeTranscriptApi()
fetched = api.fetch(video_id)
lines = [snippet.text for snippet in fetched.snippets]
except Exception as exc:
return f"Could not fetch transcript: {exc}"
return " ".join(lines)[:30000]
def build_custom_tools() -> list:
return [
read_spreadsheet,
execute_python_file,
transcribe_audio,
analyze_image,
get_youtube_transcript,
]
|