File size: 15,519 Bytes
4d49b06 |
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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
from smolagents import Tool
import pandas as pd
import os
import tempfile
import requests
from urllib.parse import urlparse
import json
import re
from datetime import datetime, timedelta
class ReverseTextTool(Tool):
name = "reverse_text"
description = "Reverses the text in a string."
inputs = {
"text": {
"type": "string",
"description": "The text to reverse."
}
}
output_type = "string"
def forward(self, text: str) -> str:
return text[::-1]
class ExtractTextFromImageTool(Tool):
name = "extract_text_from_image"
description = "Extracts text from an image file using OCR."
inputs = {
"image_path": {
"type": "string",
"description": "Path to the image file."
}
}
output_type = "string"
def forward(self, image_path: str) -> str:
try:
# Try to import pytesseract
import pytesseract
from PIL import Image
# Open the image
image = Image.open(image_path)
# Try different configurations for better results
configs = [
'--psm 6', # Assume a single uniform block of text
'--psm 3', # Automatic page segmentation, but no OSD
'--psm 1', # Automatic page segmentation with OSD
]
results = []
for config in configs:
try:
text = pytesseract.image_to_string(image, config=config)
if text.strip():
results.append(text)
except Exception:
continue
if results:
# Return the longest result, which is likely the most complete
return f"Extracted text from image:\n\n{max(results, key=len)}"
else:
return "No text could be extracted from the image."
except ImportError:
return "Error: pytesseract is not installed. Please install it with 'pip install pytesseract' and ensure Tesseract OCR is installed on your system."
except Exception as e:
return f"Error extracting text from image: {str(e)}"
class AnalyzeCSVTool(Tool):
name = "analyze_csv_file"
description = "Analyzes a CSV file and provides information about its contents."
inputs = {
"file_path": {
"type": "string",
"description": "Path to the CSV file."
},
"query": {
"type": "string",
"description": "Optional query about the data.",
"default": "",
"nullable": True
}
}
output_type = "string"
def forward(self, file_path: str, query: str = "") -> str:
try:
# Read CSV file with different encodings if needed
for encoding in ['utf-8', 'latin1', 'iso-8859-1', 'cp1252']:
try:
df = pd.read_csv(file_path, encoding=encoding)
break
except UnicodeDecodeError:
continue
else:
return "Error: Could not read the CSV file with any of the attempted encodings."
# Basic information
result = f"CSV file has {len(df)} rows and {len(df.columns)} columns.\n"
result += f"Columns: {', '.join(df.columns)}\n\n"
# If there's a specific query
if query:
if "count" in query.lower():
result += f"Row count: {len(df)}\n"
# Look for column-specific queries
for col in df.columns:
if col.lower() in query.lower():
result += f"\nColumn '{col}' information:\n"
if pd.api.types.is_numeric_dtype(df[col]):
result += f"Min: {df[col].min()}\n"
result += f"Max: {df[col].max()}\n"
result += f"Mean: {df[col].mean()}\n"
result += f"Median: {df[col].median()}\n"
else:
# For categorical data
value_counts = df[col].value_counts().head(10)
result += f"Unique values: {df[col].nunique()}\n"
result += f"Top values:\n{value_counts.to_string()}\n"
# General statistics for all columns
else:
# For numeric columns
numeric_cols = df.select_dtypes(include=['number']).columns
if len(numeric_cols) > 0:
result += "Numeric columns statistics:\n"
result += df[numeric_cols].describe().to_string()
result += "\n\n"
# For categorical columns, show counts of unique values
cat_cols = df.select_dtypes(exclude=['number']).columns
if len(cat_cols) > 0:
result += "Categorical columns:\n"
for col in cat_cols[:5]: # Limit to first 5 columns
result += f"- {col}: {df[col].nunique()} unique values\n"
return result
except Exception as e:
return f"Error analyzing CSV file: {str(e)}"
class AnalyzeExcelTool(Tool):
name = "analyze_excel_file"
description = "Analyzes an Excel file and provides information about its contents."
inputs = {
"file_path": {
"type": "string",
"description": "Path to the Excel file."
},
"query": {
"type": "string",
"description": "Optional query about the data.",
"default": "",
"nullable": True
},
"sheet_name": {
"type": "string",
"description": "Name of the sheet to analyze (defaults to first sheet).",
"default": None,
"nullable": True
}
}
output_type = "string"
def forward(self, file_path: str, query: str = "", sheet_name: str = None) -> str:
try:
# Read sheet names first
excel_file = pd.ExcelFile(file_path)
sheet_names = excel_file.sheet_names
# Info about all sheets
result = f"Excel file contains {len(sheet_names)} sheets: {', '.join(sheet_names)}\n\n"
# If sheet name is specified, use it; otherwise use first sheet
if sheet_name is None:
sheet_name = sheet_names[0]
elif sheet_name not in sheet_names:
return f"Error: Sheet '{sheet_name}' not found. Available sheets: {', '.join(sheet_names)}"
# Read the specified sheet
df = pd.read_excel(file_path, sheet_name=sheet_name)
# Basic information
result += f"Sheet '{sheet_name}' has {len(df)} rows and {len(df.columns)} columns.\n"
result += f"Columns: {', '.join(df.columns)}\n\n"
# Handle query similar to CSV tool
if query:
if "count" in query.lower():
result += f"Row count: {len(df)}\n"
# Look for column-specific queries
for col in df.columns:
if col.lower() in query.lower():
result += f"\nColumn '{col}' information:\n"
if pd.api.types.is_numeric_dtype(df[col]):
result += f"Min: {df[col].min()}\n"
result += f"Max: {df[col].max()}\n"
result += f"Mean: {df[col].mean()}\n"
result += f"Median: {df[col].median()}\n"
else:
# For categorical data
value_counts = df[col].value_counts().head(10)
result += f"Unique values: {df[col].nunique()}\n"
result += f"Top values:\n{value_counts.to_string()}\n"
else:
# For numeric columns
numeric_cols = df.select_dtypes(include=['number']).columns
if len(numeric_cols) > 0:
result += "Numeric columns statistics:\n"
result += df[numeric_cols].describe().to_string()
result += "\n\n"
# For categorical columns, show counts of unique values
cat_cols = df.select_dtypes(exclude=['number']).columns
if len(cat_cols) > 0:
result += "Categorical columns:\n"
for col in cat_cols[:5]: # Limit to first 5 columns
result += f"- {col}: {df[col].nunique()} unique values\n"
return result
except Exception as e:
return f"Error analyzing Excel file: {str(e)}"
class DateCalculatorTool(Tool):
name = "date_calculator"
description = "Performs date calculations like adding days, formatting dates, etc."
inputs = {
"query": {
"type": "string",
"description": "The date calculation to perform (e.g., 'What day is 10 days from today?', 'Format 2023-05-15 as MM/DD/YYYY')"
}
}
output_type = "string"
def forward(self, query: str) -> str:
try:
# Get current date/time
if re.search(r'(today|now|current date|current time)', query, re.IGNORECASE):
now = datetime.now()
if 'time' in query.lower():
return f"Current date and time: {now.strftime('%Y-%m-%d %H:%M:%S')}"
else:
return f"Today's date: {now.strftime('%Y-%m-%d')}"
# Add days to a date
add_match = re.search(r'(what|when).+?(\d+)\s+(day|days|week|weeks|month|months|year|years)\s+(from|after)\s+(.+)', query, re.IGNORECASE)
if add_match:
amount = int(add_match.group(2))
unit = add_match.group(3).lower()
date_text = add_match.group(5).strip()
# Parse the date
if date_text.lower() in ['today', 'now']:
base_date = datetime.now()
else:
try:
# Try various date formats
for fmt in ['%Y-%m-%d', '%m/%d/%Y', '%d/%m/%Y', '%B %d, %Y']:
try:
base_date = datetime.strptime(date_text, fmt)
break
except ValueError:
continue
else:
return f"Could not parse date: {date_text}"
except Exception as e:
return f"Error parsing date: {e}"
# Calculate new date
if 'day' in unit:
new_date = base_date + timedelta(days=amount)
elif 'week' in unit:
new_date = base_date + timedelta(weeks=amount)
elif 'month' in unit:
# Simplified month calculation
new_month = base_date.month + amount
new_year = base_date.year + (new_month - 1) // 12
new_month = ((new_month - 1) % 12) + 1
new_date = base_date.replace(year=new_year, month=new_month)
elif 'year' in unit:
new_date = base_date.replace(year=base_date.year + amount)
return f"Date {amount} {unit} from {base_date.strftime('%Y-%m-%d')} is {new_date.strftime('%Y-%m-%d')}"
# Format a date
format_match = re.search(r'format\s+(.+?)\s+as\s+(.+)', query, re.IGNORECASE)
if format_match:
date_text = format_match.group(1).strip()
format_spec = format_match.group(2).strip()
# Parse the date
if date_text.lower() in ['today', 'now']:
date_obj = datetime.now()
else:
try:
# Try various date formats
for fmt in ['%Y-%m-%d', '%m/%d/%Y', '%d/%m/%Y', '%B %d, %Y']:
try:
date_obj = datetime.strptime(date_text, fmt)
break
except ValueError:
continue
else:
return f"Could not parse date: {date_text}"
except Exception as e:
return f"Error parsing date: {e}"
# Convert format specification to strftime format
format_mapping = {
'YYYY': '%Y',
'YY': '%y',
'MM': '%m',
'DD': '%d',
'HH': '%H',
'mm': '%M',
'ss': '%S'
}
strftime_format = format_spec
for key, value in format_mapping.items():
strftime_format = strftime_format.replace(key, value)
return f"Formatted date: {date_obj.strftime(strftime_format)}"
return "I couldn't understand the date calculation query."
except Exception as e:
return f"Error performing date calculation: {str(e)}"
class DownloadFileTool(Tool):
name = "download_file"
description = "Downloads a file from a URL and saves it locally."
inputs = {
"url": {
"type": "string",
"description": "The URL to download from."
},
"filename": {
"type": "string",
"description": "Optional filename to save as (default: derived from URL).",
"default": None,
"nullable": True
}
}
output_type = "string"
def forward(self, url: str, filename: str = None) -> str:
try:
# Parse URL to get filename if not provided
if not filename:
path = urlparse(url).path
filename = os.path.basename(path)
if not filename:
# Generate a random name if we couldn't extract one
import uuid
filename = f"downloaded_{uuid.uuid4().hex[:8]}"
# Create temporary file
temp_dir = tempfile.gettempdir()
filepath = os.path.join(temp_dir, filename)
# Download the file
response = requests.get(url, stream=True)
response.raise_for_status()
# Save the file
with open(filepath, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
return f"File downloaded to {filepath}. You can now analyze this file."
except Exception as e:
return f"Error downloading file: {str(e)}" |