Spaces:
Running
Running
File size: 11,427 Bytes
92cf0b4 38812af 92cf0b4 38812af 92cf0b4 38812af 92cf0b4 |
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 |
"""
Enhanced tools for GAIA Agent including Wikipedia search, file processing, and web browsing.
"""
import os
import json
import requests
import wikipedia
from typing import Optional, Dict, Any, List
from langchain.tools import Tool
from langchain_community.tools import DuckDuckGoSearchRun
import pandas as pd
from PIL import Image
import PyPDF2
from bs4 import BeautifulSoup
from io import BytesIO
class WikipediaSearchTool:
"""Tool for searching Wikipedia with better error handling and content extraction."""
def __init__(self):
wikipedia.set_lang("en")
def search_wikipedia(self, query: str, max_results: int = 3) -> str:
"""
Search Wikipedia for information and return a summary.
Args:
query: Search query string
max_results: Maximum number of results to return
Returns:
Formatted string with search results
"""
try:
# Search for pages
search_results = wikipedia.search(query, results=max_results)
if not search_results:
return f"No Wikipedia articles found for query: '{query}'"
results = []
for title in search_results[:max_results]:
try:
# Get page summary
page = wikipedia.page(title)
summary = wikipedia.summary(title, sentences=3)
results.append({
"title": page.title,
"url": page.url,
"summary": summary
})
except wikipedia.exceptions.DisambiguationError as e:
# Handle disambiguation by taking the first option
try:
page = wikipedia.page(e.options[0])
summary = wikipedia.summary(e.options[0], sentences=3)
results.append({
"title": page.title,
"url": page.url,
"summary": summary
})
except:
continue
except wikipedia.exceptions.PageError:
continue
except Exception as e:
continue
if not results:
return f"Could not retrieve information for query: '{query}'"
# Format results
formatted_results = f"Wikipedia search results for '{query}':\n\n"
for i, result in enumerate(results, 1):
formatted_results += f"{i}. **{result['title']}**\n"
formatted_results += f" URL: {result['url']}\n"
formatted_results += f" Summary: {result['summary']}\n\n"
return formatted_results
except Exception as e:
return f"Error searching Wikipedia: {str(e)}"
class FileProcessorTool:
"""Tool for processing various file formats (PDF, Excel, images, etc.)."""
def process_file(self, file_path: str) -> str:
"""
Process different file types and extract content/information.
Args:
file_path: Path to the file to process
Returns:
Extracted content or file information
"""
try:
if not os.path.exists(file_path):
return f"File not found: {file_path}"
file_extension = os.path.splitext(file_path)[1].lower()
if file_extension == '.pdf':
return self._process_pdf(file_path)
elif file_extension in ['.xlsx', '.xls', '.csv']:
return self._process_spreadsheet(file_path)
elif file_extension in ['.jpg', '.jpeg', '.png', '.gif', '.bmp']:
return self._process_image(file_path)
elif file_extension == '.txt':
return self._process_text(file_path)
else:
return f"Unsupported file type: {file_extension}"
except Exception as e:
return f"Error processing file {file_path}: {str(e)}"
def _process_pdf(self, file_path: str) -> str:
"""Extract text from PDF file."""
try:
with open(file_path, 'rb') as file:
pdf_reader = PyPDF2.PdfReader(file)
text_content = ""
for page_num in range(len(pdf_reader.pages)):
page = pdf_reader.pages[page_num]
text_content += page.extract_text() + "\n"
return f"PDF Content from {file_path}:\n{text_content[:2000]}..." if len(text_content) > 2000 else f"PDF Content from {file_path}:\n{text_content}"
except Exception as e:
return f"Error reading PDF: {str(e)}"
def _process_spreadsheet(self, file_path: str) -> str:
"""Process Excel/CSV files and extract data information."""
try:
if file_path.endswith('.csv'):
df = pd.read_csv(file_path)
else:
df = pd.read_excel(file_path)
info = f"Spreadsheet Analysis for {file_path}:\n"
info += f"Shape: {df.shape[0]} rows, {df.shape[1]} columns\n"
info += f"Columns: {', '.join(df.columns.tolist())}\n\n"
# Show first few rows
info += "First 5 rows:\n"
info += df.head().to_string() + "\n\n"
# Basic statistics for numeric columns
numeric_cols = df.select_dtypes(include=['number']).columns
if len(numeric_cols) > 0:
info += "Numeric column statistics:\n"
info += df[numeric_cols].describe().to_string() + "\n\n"
# Calculate totals if there are numeric columns
if len(numeric_cols) > 0:
info += "Column totals:\n"
for col in numeric_cols:
total = df[col].sum()
info += f"{col}: {total}\n"
return info
except Exception as e:
return f"Error reading spreadsheet: {str(e)}"
def _process_image(self, file_path: str) -> str:
"""Process image files and return basic information."""
try:
with Image.open(file_path) as img:
info = f"Image Analysis for {file_path}:\n"
info += f"Size: {img.size[0]} x {img.size[1]} pixels\n"
info += f"Mode: {img.mode}\n"
info += f"Format: {img.format}\n"
# Note: For GAIA tasks, you might need OCR or more advanced image analysis
info += "\nNote: For text extraction from images, OCR tools would be needed.\n"
return info
except Exception as e:
return f"Error processing image: {str(e)}"
def _process_text(self, file_path: str) -> str:
"""Process text files."""
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
return f"Text file content from {file_path}:\n{content[:2000]}..." if len(content) > 2000 else f"Text file content from {file_path}:\n{content}"
except Exception as e:
return f"Error reading text file: {str(e)}"
class EnhancedWebSearchTool:
"""Enhanced web search tool with better result processing."""
def __init__(self):
self.search_tool = DuckDuckGoSearchRun()
def search_web(self, query: str, max_results: int = 5) -> str:
"""
Perform web search with enhanced result processing.
Args:
query: Search query string
max_results: Maximum number of results to consider
Returns:
Formatted search results
"""
try:
results = self.search_tool.invoke(query)
# Process and format results better
formatted_results = f"Web search results for '{query}':\n\n"
formatted_results += results
return formatted_results
except Exception as e:
return f"Error performing web search: {str(e)}"
class CalculationTool:
"""Tool for performing calculations and data analysis."""
def calculate(self, expression: str) -> str:
"""
Safely evaluate mathematical expressions.
Args:
expression: Mathematical expression to evaluate
Returns:
Result of the calculation
"""
try:
# Only allow safe mathematical operations
allowed_chars = set('0123456789+-*/().% ')
if not all(c in allowed_chars for c in expression.replace(' ', '')):
return f"Invalid characters in expression: {expression}"
result = eval(expression)
return f"Calculation result: {expression} = {result}"
except Exception as e:
return f"Error in calculation: {str(e)}"
def create_gaia_tools() -> List[Tool]:
"""Create all tools needed for GAIA benchmark tasks."""
# Initialize tool classes
wiki_tool = WikipediaSearchTool()
file_tool = FileProcessorTool()
web_tool = EnhancedWebSearchTool()
calc_tool = CalculationTool()
# Create LangChain Tool objects
tools = [
Tool(
name="wikipedia_search",
func=wiki_tool.search_wikipedia,
description="Search Wikipedia for information about any topic. Use this for factual information, historical data, scientific concepts, etc. Input should be a clear search query."
),
Tool(
name="file_processor",
func=file_tool.process_file,
description="Process and analyze files (PDF, Excel, images, text). Input should be the file path. Returns file content, data analysis, or file information."
),
Tool(
name="web_search",
func=web_tool.search_web,
description="Search the web for current information, news, or specific websites. Use this when you need up-to-date information not available in Wikipedia."
),
Tool(
name="calculator",
func=calc_tool.calculate,
description="Perform mathematical calculations. Input should be a mathematical expression using +, -, *, /, (), %. Example: '(100 + 50) * 0.15'"
)
]
return tools
if __name__ == "__main__":
# Test the tools
tools = create_gaia_tools()
# Test Wikipedia search
wiki_result = tools[0].func("Artificial Intelligence")
print("Wikipedia test:")
print(wiki_result[:500] + "...\n")
# Test web search
web_result = tools[2].func("latest AI news 2024")
print("Web search test:")
print(web_result[:500] + "...\n")
# Test calculator
calc_result = tools[3].func("(100 + 50) * 2")
print("Calculator test:")
print(calc_result) |