File size: 17,623 Bytes
0034b95
 
41844a4
0034b95
41844a4
 
0034b95
 
 
 
 
 
 
41844a4
 
0034b95
 
 
41844a4
 
0034b95
 
 
 
41844a4
 
 
0034b95
 
 
 
 
41844a4
 
0034b95
 
 
 
 
41844a4
0034b95
 
 
 
 
 
41844a4
0034b95
 
 
 
 
 
 
41844a4
0034b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41844a4
0034b95
 
 
 
 
 
 
 
 
 
 
 
 
 
41844a4
0034b95
 
 
 
 
 
 
 
 
 
 
 
 
 
41844a4
0034b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41844a4
 
0034b95
 
 
 
 
 
41844a4
0034b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41844a4
 
 
0034b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41844a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0034b95
41844a4
0034b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41844a4
0034b95
41844a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0034b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41844a4
0034b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41844a4
 
 
 
 
0034b95
 
41844a4
0034b95
41844a4
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
"""
Crawl4AI Demo Application
====================================================

This is a modified version of the Crawl4AI demo application specifically designed
for deployment on Hugging Face Spaces.

Features:
---------
- Web interface built with Gradio for interactive use
- Support for multiple crawler types (Basic, LLM, Cosine, JSON/CSS)
- Configurable word count threshold
- Markdown output with metadata
- Sub-page crawling capabilities
- Lazy loading support

Usage:
------
This version is specifically designed for Hugging Face Spaces deployment.
Simply upload this file to your Space and it will automatically run.

Dependencies:
------------
- gradio
- crawl4ai>=0.4.3b0
- python-dotenv>=1.0.0
- pydantic>=2.5.0
"""

import gradio as gr
import asyncio
from typing import Optional, Dict, Any, List, Set
from enum import Enum
from pydantic import BaseModel
from crawl4ai import AsyncWebCrawler, CrawlerRunConfig, CacheMode, BrowserConfig
from crawl4ai.extraction_strategy import JsonCssExtractionStrategy
import urllib.parse

class CrawlerType(str, Enum):
    """Enumeration of supported crawler types."""
    BASIC = "basic"
    LLM = "llm"
    COSINE = "cosine"
    JSON_CSS = "json_css"

class ExtractionType(str, Enum):
    """Enumeration of supported extraction strategies."""
    DEFAULT = "default"
    CSS = "css"
    XPATH = "xpath"
    LLM = "llm"
    COMBINED = "combined"

class CrawlRequest(BaseModel):
    """Request model for crawling operations."""
    url: str
    crawler_type: CrawlerType = CrawlerType.BASIC
    extraction_type: ExtractionType = ExtractionType.DEFAULT
    word_count_threshold: int = 100
    css_selector: Optional[str] = None
    xpath_query: Optional[str] = None
    excluded_tags: Optional[list] = None
    scan_full_page: bool = False
    scroll_delay: float = 0.5
    crawl_subpages: bool = False
    max_depth: int = 1
    exclude_external_links: bool = True
    max_pages: int = 10

def create_extraction_strategy(extraction_type: ExtractionType, css_selector: Optional[str] = None, xpath_query: Optional[str] = None) -> Any:
    """Create an extraction strategy based on the specified type."""
    if extraction_type == ExtractionType.CSS and css_selector:
        schema = {
            "name": "Content",
            "baseSelector": css_selector,
            "fields": [
                {"name": "title", "selector": "h1,h2", "type": "text"},
                {"name": "text", "selector": "p", "type": "text"},
                {"name": "links", "selector": "a", "type": "attribute", "attribute": "href"}
            ]
        }
        return JsonCssExtractionStrategy(schema)
    return None

async def crawl_with_subpages(request: CrawlRequest, base_url: str, current_depth: int = 1, visited: Set[str] = None) -> Dict:
    """Recursively crawl pages including sub-pages up to the specified depth."""
    if visited is None:
        visited = set()
    
    if current_depth > request.max_depth or len(visited) >= request.max_pages:
        return None
    
    # Normalize URL to avoid duplicates
    normalized_url = urllib.parse.urljoin(request.url, '/')
    if normalized_url in visited:
        return None
        
    # Create run configuration for current page
    run_config = CrawlerRunConfig(
        cache_mode=CacheMode.BYPASS,
        verbose=True,
        word_count_threshold=request.word_count_threshold,
        css_selector=request.css_selector,
        excluded_tags=request.excluded_tags or ["nav", "footer", "header"],
        exclude_external_links=request.exclude_external_links,
        wait_for=f"css:{request.css_selector}" if request.css_selector else None,
        wait_for_images=True,
        page_timeout=30000,
        scan_full_page=request.scan_full_page,
        scroll_delay=request.scroll_delay,
        extraction_strategy=create_extraction_strategy(
            request.extraction_type,
            request.css_selector,
            request.xpath_query
        )
    )

    browser_config = BrowserConfig(
        headless=True,
        viewport_width=1920,
        viewport_height=1080
    )

    results = {
        "pages": [],
        "total_links": 0,
        "visited_pages": len(visited)
    }

    try:
        async with AsyncWebCrawler(config=browser_config) as crawler:
            result = await crawler.arun(url=request.url, config=run_config)
            
            if not result.success:
                print(f"Failed to crawl {request.url}: {result.error_message}")
                return None
            
            # Add current page result
            page_result = {
                "url": request.url,
                "markdown": result.markdown_v2 if hasattr(result, 'markdown_v2') else "",
                "extracted_content": result.extracted_content if hasattr(result, 'extracted_content') else None,
                "depth": current_depth
            }
            results["pages"].append(page_result)
            visited.add(normalized_url)

            # Process sub-pages if enabled
            if request.crawl_subpages and hasattr(result, 'links'):
                internal_links = result.links.get("internal", [])
                if internal_links:
                    results["total_links"] += len(internal_links)
                    
                    for link in internal_links:
                        if len(visited) >= request.max_pages:
                            break
                            
                        try:
                            normalized_link = urllib.parse.urljoin(request.url, link)
                            link_domain = urllib.parse.urlparse(normalized_link).netloc
                            
                            if normalized_link in visited or (request.exclude_external_links and link_domain != base_url):
                                continue
                            
                            sub_request = CrawlRequest(
                                **{**request.dict(), "url": normalized_link}
                            )
                            
                            sub_result = await crawl_with_subpages(
                                sub_request,
                                base_url,
                                current_depth + 1,
                                visited
                            )
                            
                            if sub_result:
                                results["pages"].extend(sub_result["pages"])
                                results["total_links"] += sub_result["total_links"]
                                results["visited_pages"] = len(visited)
                        except Exception as e:
                            print(f"Error processing link {link}: {str(e)}")
                            continue

            return results
    except Exception as e:
        print(f"Error crawling {request.url}: {str(e)}")
        return None

async def crawl_url(request: CrawlRequest) -> Dict:
    """Crawl a URL and return the extracted content."""
    try:
        base_url = urllib.parse.urlparse(request.url).netloc
        
        if request.crawl_subpages:
            results = await crawl_with_subpages(request, base_url)
            if not results or not results["pages"]:
                raise Exception(f"Failed to crawl pages starting from {request.url}")
                
            combined_markdown = "\\n\\n---\\n\\n".join(
                f"## Page: {page['url']}\\n{page['markdown']}"
                for page in results["pages"]
            )
            
            return {
                "markdown": combined_markdown,
                "metadata": {
                    "url": request.url,
                    "crawler_type": request.crawler_type.value,
                    "extraction_type": request.extraction_type.value,
                    "word_count_threshold": request.word_count_threshold,
                    "css_selector": request.css_selector,
                    "xpath_query": request.xpath_query,
                    "scan_full_page": request.scan_full_page,
                    "scroll_delay": request.scroll_delay,
                    "total_pages_crawled": results["visited_pages"],
                    "total_links_found": results["total_links"],
                    "max_depth_reached": min(request.max_depth, max(page["depth"] for page in results["pages"]))
                },
                "pages": results["pages"]
            }
        else:
            wait_condition = f"css:{request.css_selector}" if request.css_selector else None
            
            run_config = CrawlerRunConfig(
                cache_mode=CacheMode.BYPASS,
                word_count_threshold=request.word_count_threshold,
                css_selector=request.css_selector,
                excluded_tags=request.excluded_tags or ["nav", "footer", "header"],
                wait_for=wait_condition,
                wait_for_images=True,
                page_timeout=30000,
                scan_full_page=request.scan_full_page,
                scroll_delay=request.scroll_delay,
                extraction_strategy=create_extraction_strategy(
                    request.extraction_type,
                    request.css_selector,
                    request.xpath_query
                )
            )

            browser_config = BrowserConfig(
                headless=True,
                viewport_width=1920,
                viewport_height=1080
            )

            async with AsyncWebCrawler(config=browser_config) as crawler:
                result = await crawler.arun(url=request.url, config=run_config)
                
                if not result.success:
                    raise Exception(result.error_message)
                
                images = result.media.get("images", []) if hasattr(result, 'media') else []
                image_info = "\n### Images Found\n" if images else ""
                for i, img in enumerate(images[:5]):
                    image_info += f"- Image {i+1}: {img.get('src', 'N/A')}\n"
                    if img.get('alt'):
                        image_info += f"  Alt: {img['alt']}\n"
                    if img.get('score'):
                        image_info += f"  Score: {img['score']}\n"
                
                return {
                    "markdown": result.markdown_v2 if hasattr(result, 'markdown_v2') else "",
                    "metadata": {
                        "url": request.url,
                        "crawler_type": request.crawler_type.value,
                        "extraction_type": request.extraction_type.value,
                        "word_count_threshold": request.word_count_threshold,
                        "css_selector": request.css_selector,
                        "xpath_query": request.xpath_query,
                        "scan_full_page": request.scan_full_page,
                        "scroll_delay": request.scroll_delay,
                        "wait_condition": wait_condition
                    },
                    "extracted_content": result.extracted_content if hasattr(result, 'extracted_content') else None,
                    "image_info": image_info
                }
    except Exception as e:
        raise Exception(str(e))

async def gradio_crawl(
    url: str,
    crawler_type: str,
    extraction_type: str,
    word_count_threshold: int,
    css_selector: str,
    xpath_query: str,
    scan_full_page: bool,
    scroll_delay: float,
    crawl_subpages: bool,
    max_depth: int,
    max_pages: int,
    exclude_external_links: bool
) -> tuple[str, str]:
    """Handle crawling requests from the Gradio interface."""
    try:
        request = CrawlRequest(
            url=url,
            crawler_type=CrawlerType(crawler_type.lower()),
            extraction_type=ExtractionType(extraction_type.lower()),
            word_count_threshold=word_count_threshold,
            css_selector=css_selector if css_selector else None,
            xpath_query=xpath_query if xpath_query else None,
            scan_full_page=scan_full_page,
            scroll_delay=scroll_delay,
            crawl_subpages=crawl_subpages,
            max_depth=max_depth,
            max_pages=max_pages,
            exclude_external_links=exclude_external_links
        )
        
        result = await crawl_url(request)
        
        markdown_content = str(result["markdown"]) if result.get("markdown") else ""
        
        metadata_str = f"""### Metadata
- URL: {result['metadata']['url']}
- Crawler Type: {result['metadata']['crawler_type']}
- Extraction Type: {result['metadata']['extraction_type']}
- Word Count Threshold: {result['metadata']['word_count_threshold']}
- CSS Selector: {result['metadata']['css_selector'] or 'None'}
- XPath Query: {result['metadata']['xpath_query'] or 'None'}
- Full Page Scan: {result['metadata']['scan_full_page']}
- Scroll Delay: {result['metadata']['scroll_delay']}s"""

        if crawl_subpages:
            metadata_str += f"""
- Total Pages Crawled: {result['metadata'].get('total_pages_crawled', 0)}
- Total Links Found: {result['metadata'].get('total_links_found', 0)}
- Max Depth Reached: {result['metadata'].get('max_depth_reached', 1)}"""

        if result.get('image_info'):
            metadata_str += f"\n\n{result['image_info']}"

        if result.get("extracted_content"):
            metadata_str += f"\n\n### Extracted Content\n```json\n{result['extracted_content']}\n```"
        
        return markdown_content, metadata_str
    except Exception as e:
        error_msg = f"Error: {str(e)}"
        return error_msg, "Error occurred while crawling"

# Create Gradio interface
demo = gr.Interface(
    fn=gradio_crawl,
    inputs=[
        gr.Textbox(
            label="URL",
            placeholder="Enter URL to crawl",
            info="The webpage URL to extract content from"
        ),
        gr.Dropdown(
            choices=["Basic", "LLM", "Cosine", "JSON/CSS"],
            label="Crawler Type",
            value="Basic",
            info="Select the content extraction strategy"
        ),
        gr.Dropdown(
            choices=["Default", "CSS", "XPath", "LLM", "Combined"],
            label="Extraction Type",
            value="Default",
            info="Choose how to extract content from the page"
        ),
        gr.Slider(
            minimum=50,
            maximum=500,
            value=100,
            step=50,
            label="Word Count Threshold",
            info="Minimum number of words required for content extraction"
        ),
        gr.Textbox(
            label="CSS Selector",
            placeholder="e.g., article.content, main.post",
            info="CSS selector to target specific content (used with CSS extraction type)"
        ),
        gr.Textbox(
            label="XPath Query",
            placeholder="e.g., //article[@class='content']",
            info="XPath query to target specific content (used with XPath extraction type)"
        ),
        gr.Checkbox(
            label="Scan Full Page",
            value=False,
            info="Enable to scroll through the entire page to load lazy content"
        ),
        gr.Slider(
            minimum=0.1,
            maximum=2.0,
            value=0.5,
            step=0.1,
            label="Scroll Delay",
            info="Delay between scroll steps in seconds when scanning full page"
        ),
        gr.Checkbox(
            label="Crawl Sub-pages",
            value=False,
            info="Enable to crawl links found on the page"
        ),
        gr.Slider(
            minimum=1,
            maximum=5,
            value=1,
            step=1,
            label="Max Crawl Depth",
            info="Maximum depth for recursive crawling (1 = only direct links)"
        ),
        gr.Slider(
            minimum=1,
            maximum=50,
            value=10,
            step=5,
            label="Max Pages",
            info="Maximum number of pages to crawl"
        ),
        gr.Checkbox(
            label="Exclude External Links",
            value=True,
            info="Only crawl links within the same domain"
        )
    ],
    outputs=[
        gr.Markdown(label="Generated Markdown"),
        gr.Markdown(label="Metadata & Extraction Results")
    ],
    title="Crawl4AI Demo",
    description="""
    This demo allows you to extract content from web pages using different crawling and extraction strategies.
    
    1. Enter a URL to crawl
    2. Select a crawler type (Basic, LLM, Cosine, JSON/CSS)
    3. Choose an extraction strategy (Default, CSS, XPath, LLM, Combined)
    4. Configure additional options:
       - Word count threshold for content filtering
       - CSS selectors for targeting specific content
       - XPath queries for precise extraction
       - Full page scanning for lazy-loaded content
       - Scroll delay for controlling page scanning speed
       - Sub-page crawling with depth control
       - Maximum number of pages to crawl
       - External link filtering
    
    The extracted content will be displayed in markdown format along with metadata and extraction results.
    When sub-page crawling is enabled, content from all crawled pages will be combined in the output.
    """,
    examples=[
        ["https://example.com", "Basic", "Default", 100, "", "", False, 0.5, False, 1, 10, True],
        ["https://example.com/blog", "Basic", "CSS", 100, "article.post", "", True, 0.5, True, 2, 5, True],
    ]
)

# For Hugging Face Spaces, we launch just the Gradio interface
if __name__ == "__main__":
    demo.launch()