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
a5c9e62
 
 
 
 
 
11d4518
a5c9e62
 
7f6ab11
a5c9e62
 
f9a7c9b
 
 
 
 
 
 
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
from langchain.tools import Tool
import requests
import os
from PIL import Image
import io
import base64
from ddgs import DDGS
from typing import Optional
import json
import PyPDF2
import tempfile
import requests
from bs4 import BeautifulSoup
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain.schema import Document
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

def file_download_tool_func(task_id: str) -> str:
    """Downloads a file associated with a GAIA task ID."""
    try:
        api_url = "https://agents-course-unit4-scoring.hf.space"
        file_url = f"{api_url}/files/{task_id}"
        
        response = requests.get(file_url, timeout=30)
        response.raise_for_status()
        
        # Save to temporary file
        with tempfile.NamedTemporaryFile(delete=False, suffix=".tmp") as temp_file:
            temp_file.write(response.content)
            temp_path = temp_file.name
        
        # Try to determine file type and process accordingly
        content_type = response.headers.get('content-type', '').lower()
        
        if 'image' in content_type:
            return f"Image file downloaded to {temp_path}. Use image_analysis_tool to analyze it."
        elif 'pdf' in content_type:
            return process_pdf_file(temp_path)
        elif 'text' in content_type:
            with open(temp_path, 'r', encoding='utf-8') as f:
                content = f.read()
            os.unlink(temp_path)  # Clean up
            return f"Text file content:\n{content}"
        else:
            return f"File downloaded to {temp_path}. Content type: {content_type}"
            
    except Exception as e:
        return f"Failed to download file for task {task_id}: {str(e)}"

def process_pdf_file(file_path: str) -> str:
    """Process a PDF file and extract text content."""
    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 += f"\n--- Page {page_num + 1} ---\n"
                text_content += page.extract_text()
        
        os.unlink(file_path)  # Clean up
        return f"PDF content extracted:\n{text_content}"
    except Exception as e:
        return f"Failed to process PDF: {str(e)}"

file_download_tool = Tool(
    name="file_download",
    func=file_download_tool_func,
    description="Downloads and processes files associated with GAIA task IDs. Can handle images, PDFs, and text files."
)

def image_analysis_tool_func(image_path_or_description: str) -> str:
    """Analyzes images for GAIA questions. For now, returns a placeholder."""
    # This is a simplified version - in a full implementation, you'd use a vision model
    try:
        if os.path.exists(image_path_or_description):
            # Try to open and get basic info about the image
            with Image.open(image_path_or_description) as img:
                width, height = img.size
                mode = img.mode
                format_info = img.format
                
                # Clean up the temporary file
                os.unlink(image_path_or_description)
                
                return f"Image analyzed: {width}x{height} pixels, mode: {mode}, format: {format_info}. Note: This is a basic analysis. For detailed image content analysis, a vision model would be needed."
        else:
            return f"Image analysis requested for: {image_path_or_description}. Note: Full image analysis requires a vision model integration."
    except Exception as e:
        return f"Image analysis failed: {str(e)}"

image_analysis_tool = Tool(
    name="image_analysis",
    func=image_analysis_tool_func,
    description="Analyzes images to extract information. Use this for questions involving visual content."
)

def text_processor_tool_func(text: str, operation: str = "summarize") -> str:
    """Processes text for various operations like summarization, extraction, etc."""
    try:
        if operation == "summarize":
            # Simple summarization - take first and last sentences if long
            sentences = text.split('.')
            if len(sentences) > 5:
                summary = '. '.join(sentences[:2] + sentences[-2:])
                return f"Text summary: {summary}"
            else:
                return f"Text (short enough to not need summarization): {text}"
        
        elif operation == "extract_numbers":
            import re
            numbers = re.findall(r'\d+(?:\.\d+)?', text)
            return f"Numbers found in text: {numbers}"
        
        elif operation == "extract_dates":
            import re
            # Simple date pattern matching
            date_patterns = [
                r'\d{1,2}/\d{1,2}/\d{4}',  # MM/DD/YYYY
                r'\d{4}-\d{1,2}-\d{1,2}',  # YYYY-MM-DD
                r'\b\w+ \d{1,2}, \d{4}\b'  # Month DD, YYYY
            ]
            dates = []
            for pattern in date_patterns:
                dates.extend(re.findall(pattern, text))
            return f"Dates found in text: {dates}"
        
        else:
            return f"Text processing operation '{operation}' not supported. Available: summarize, extract_numbers, extract_dates"
            
    except Exception as e:
        return f"Text processing failed: {str(e)}"

text_processor_tool = Tool(
    name="text_processor",
    func=text_processor_tool_func,
    description="Processes text for various operations like summarization, number extraction, date extraction. Specify operation as second parameter."
)

def enhanced_web_retrieval_tool_func(query: str, backend: str = "bing") -> str:
    """Enhanced web search with cascading fallback: Wikipedia first, then general web search."""
    try:
        print(f"πŸ” Enhanced web retrieval for: {query}")
        
        # Step 1: Try Wikipedia search first
        print("πŸ“š Searching Wikipedia...")
        wikipedia_results = get_wikipedia_search_urls(query, backend)
        
        if has_sufficient_results(wikipedia_results):
            print(f"βœ… Found {len(wikipedia_results)} Wikipedia results")
            documents = fetch_and_process_results(wikipedia_results, "Wikipedia")
            if documents:
                return search_documents_with_vector_store(documents, query, "Wikipedia")
        
        # Step 2: Fallback to general web search
        print("🌐 Wikipedia results insufficient, searching general web...")
        web_results = get_general_web_search_urls(query, backend)
        
        if web_results:
            print(f"βœ… Found {len(web_results)} general web results")
            documents = fetch_and_process_results(web_results, "General Web")
            if documents:
                return search_documents_with_vector_store(documents, query, "General Web")
        
        return "No sufficient results found in Wikipedia or general web search."
        
    except Exception as e:
        return f"Enhanced web retrieval failed: {str(e)}"

def get_wikipedia_search_urls(query: str, backend: str = "auto") -> list:
    """Get search results from English Wikipedia using DDGS."""
    try:
        with DDGS() as ddgs:
            # Create Wikipedia-specific search queries
            wikipedia_queries = [
                f"{query} site:en.wikipedia.org"
            ]
            
            search_results = []
            seen_urls = set()
            
            for wiki_query in wikipedia_queries:
                try:
                    results = list(ddgs.text(
                        wiki_query, 
                        max_results=8, 
                        region="us-en", 
                        backend=backend, 
                        safesearch="moderate"
                    ))
                    
                    for result in results:
                        url = result.get('href', '')
                        
                        # Only include Wikipedia URLs and avoid duplicates
                        if 'en.wikipedia.org' in url and url not in seen_urls:
                            search_results.append({
                                'url': url,
                                'title': result.get('title', 'No title'),
                                'snippet': result.get('body', 'No content')
                            })
                            seen_urls.add(url)
                            
                            # Limit to 6 unique Wikipedia pages
                            if len(search_results) >= 6:
                                break
                    
                    if len(search_results) >= 6:
                        break
                        
                except Exception as e:
                    print(f"Wikipedia search attempt failed: {e}")
                    continue
            
            return search_results
            
    except Exception as e:
        print(f"Wikipedia search URL retrieval failed: {e}")
        return []

def get_general_web_search_urls(query: str, backend: str = "auto") -> list:
    """Get search results from general web using DDGS."""
    try:
        with DDGS() as ddgs:
            search_results = []
            seen_urls = set()
            
            try:
                # General web search without site restriction
                results = list(ddgs.text(
                    query, 
                    max_results=8, 
                    region="us-en", 
                    backend=backend, 
                    safesearch="moderate"
                ))
                
                for result in results:
                    url = result.get('href', '')
                    
                    # Avoid duplicates and filter out low-quality sources
                    if url not in seen_urls and is_quality_source(url):
                        search_results.append({
                            'url': url,
                            'title': result.get('title', 'No title'),
                            'snippet': result.get('body', 'No content')
                        })
                        seen_urls.add(url)
                        
                        # Limit to 6 unique web pages
                        if len(search_results) >= 6:
                            break
                            
            except Exception as e:
                print(f"General web search attempt failed: {e}")
            
            return search_results
            
    except Exception as e:
        print(f"General web search URL retrieval failed: {e}")
        return []

def is_quality_source(url: str) -> bool:
    """Filter out low-quality or problematic sources."""
    low_quality_domains = [
        'pinterest.com', 'instagram.com', 'facebook.com', 'twitter.com',
        'tiktok.com', 'youtube.com', 'reddit.com'
    ]
    
    for domain in low_quality_domains:
        if domain in url.lower():
            return False
    
    return True

def has_sufficient_results(results: list) -> bool:
    """Check if search results are sufficient to proceed."""
    if not results:
        return False
    
    # Check for minimum number of results
    if len(results) < 2:
        return False
    
    # Check if results have meaningful content
    meaningful_results = 0
    for result in results:
        snippet = result.get('snippet', '')
        title = result.get('title', '')
        
        # Consider result meaningful if it has substantial content
        if len(snippet) > 50 or len(title) > 10:
            meaningful_results += 1
    
    return meaningful_results >= 2

def fetch_and_process_results(results: list, source_type: str) -> list:
    """Fetch and process webpage content from search results."""
    documents = []
    
    for result in results[:4]:  # Process top 4 results
        url = result.get('url', '')
        title = result.get('title', 'No title')
        
        print(f"πŸ“„ Fetching content from: {title}")
        content = fetch_webpage_content(url)
        
        if content and len(content.strip()) > 100:  # Ensure meaningful content
            doc = Document(
                page_content=content,
                metadata={
                    "source": url, 
                    "title": title,
                    "source_type": source_type
                }
            )
            documents.append(doc)
    
    return documents

def fetch_webpage_content(url: str) -> str:
    """Fetch and extract clean text content from a webpage."""
    try:
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
        }
        
        response = requests.get(url, headers=headers, timeout=10)
        response.raise_for_status()
        
        # Parse HTML and extract text
        soup = BeautifulSoup(response.content, 'html.parser')
        
        # Remove script and style elements
        for script in soup(["script", "style"]):
            script.decompose()
        
        # Get text content
        text = soup.get_text()
        
        # Clean up text
        lines = (line.strip() for line in text.splitlines())
        chunks = (phrase.strip() for line in lines for phrase in line.split("  "))
        text = ' '.join(chunk for chunk in chunks if chunk)
        
        return text[:30000]
        
    except Exception as e:
        print(f"Failed to fetch content from {url}: {e}")
        return ""

def search_documents_with_vector_store(documents: list, query: str, source_type: str = "Web") -> str:
    """Create vector store and search for relevant information."""
    try:
        # Split documents into chunks
        text_splitter = RecursiveCharacterTextSplitter(
            chunk_size=1000,
            chunk_overlap=200,
            length_function=len,
        )
        
        splits = text_splitter.split_documents(documents)
        
        if not splits:
            return "No content to process after splitting."
        
        # Create embeddings and vector store
        embeddings = OpenAIEmbeddings()
        vectorstore = FAISS.from_documents(splits, embeddings)
        
        # Search for relevant chunks with the original query
        relevant_docs = vectorstore.similarity_search(query, k=5)
        
        # Format results with source type indication
        results = []
        results.append(f"πŸ” Search Results from {source_type}:\n")
        
        for i, doc in enumerate(relevant_docs, 1):
            source = doc.metadata.get('source', 'Unknown source')
            title = doc.metadata.get('title', 'No title')
            source_type_meta = doc.metadata.get('source_type', source_type)
            content = doc.page_content[:2000]  # Increased content length
            
            results.append(f"Result {i} ({source_type_meta}) - {title}:\n{content}\nSource: {source}\n")
        
        return "\n---\n".join(results)
        
    except Exception as e:
        return f"Vector search failed: {str(e)}"

web_search_tool = Tool(
    name="enhanced_web_retrieval",
    func=enhanced_web_retrieval_tool_func,
    description="Enhanced cascading web search with vector retrieval. First searches Wikipedia for reliable factual information, then falls back to general web search if insufficient results are found. Supports multiple search backends (auto, html, lite, bing) and uses semantic search to find relevant information. Ideal for comprehensive research on any topic."
)

# List of all tools for easy import
agent_tools = [
    web_search_tool,
    file_download_tool,
    image_analysis_tool,
    text_processor_tool
]