WebScrapper-AI / README.md
Ramasani's picture
Add HF Space metadata frontmatter to README
cc39b72
|
Raw History Blame Contribute Delete
4.62 kB
---
title: WebScrapper AI
emoji: πŸ”₯
colorFrom: indigo
colorTo: blue
sdk: docker
pinned: false
short_description: This is an ML based web scrapper
---
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
# Web Scraper for Hugging Face Spaces
Efficient web scraper with ML-based content classification, optimized for Hugging Face Docker Spaces with 16GB RAM.
## Features
- **Memory-efficient Chromium**: Single browser instance with page pooling, resource blocking
- **SQLite Storage**: WAL mode for concurrent access, automatic indexing
- **ML Classification**: Scikit-learn RandomForest for content categorization (news, blog, product, etc.)
- **No LLMs/AI Models**: Traditional ML only, minimal dependencies
- **Async FastAPI**: High-performance async API on port 7860
- **Background Jobs**: Queue-based scraping with progress tracking
## Architecture
```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ FastAPI │────▢│ Browser Pool │────▢│ SQLite DB β”‚
β”‚ (port 7860) β”‚ β”‚ (Chromium) β”‚ β”‚ (WAL mode) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ ML Classifier β”‚β—€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ (RandomForest) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```
## Quick Start
### Local Development
```bash
# Build
docker build -t web-scraper .
# Run
docker run -p 7860:7860 web-scraper
```
### Hugging Face Spaces Deployment
1. Create new Space with **Docker** SDK
2. Push this repository to the Space
3. Space automatically builds and deploys
## API Endpoints
| Method | Endpoint | Description |
|--------|----------|-------------|
| GET | `/health` | Health check |
| POST | `/scrape` | Submit URLs for scraping |
| GET | `/scrape/{job_id}` | Get job status/results |
| GET | `/pages` | List scraped pages |
| GET | `/pages/{id}` | Get page details |
| GET | `/stats` | Database statistics |
| POST | `/model/retrain` | Retrain ML model |
| POST | `/model/predict` | Classify HTML content |
| POST | `/db/vacuum` | Optimize database |
## Example Usage
```bash
# Submit scraping job
curl -X POST http://localhost:7860/scrape \
-H "Content-Type: application/json" \
-d '{"urls": ["https://example.com", "https://news.ycombinator.com"]}'
# Check job status
curl http://localhost:7860/scrape/{job_id}
# List pages
curl http://localhost:7860/pages
# Get statistics
curl http://localhost:7860/stats
```
## Configuration (Environment Variables)
| Variable | Default | Description |
|----------|---------|-------------|
| `SCRAPER_DB_PATH` | `/tmp/scraper.db` | SQLite database path |
| `MAX_CONCURRENT_PAGES` | `4` | Max concurrent browser pages |
| `REQUEST_TIMEOUT` | `15000` | Page load timeout (ms) |
| `MAX_CONTENT_SIZE` | `50000` | Max HTML content size (chars) |
| `HEADLESS` | `true` | Run Chromium headless |
| `BLOCK_RESOURCES` | `true` | Block images/CSS/fonts |
| `MODEL_RETRAIN_THRESHOLD` | `100` | Samples before auto-retrain |
| `MODEL_PATH` | `/tmp/scraper_model.joblib` | Model persistence path |
## Memory Optimization
With 16GB RAM, the container uses ~2GB:
- Chromium: ~400MB (single instance, resource blocking)
- Python/Dependencies: ~500MB
- SQLite: ~100-500MB
- ML Model: ~100MB
- Buffer: ~500MB
## Project Structure
```
web-scraper/
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ config.py # Configuration management
β”œβ”€β”€ storage.py # SQLite operations
β”œβ”€β”€ scraper.py # Playwright browser pool
β”œβ”€β”€ model.py # ML classifier (scikit-learn)
β”œβ”€β”€ app.py # FastAPI application
└── data/ # SQLite database (runtime)
```
## ML Model
The classifier uses:
- **Features**: 18 HTML/DOM features (tag counts, link density, DOM depth, readability)
- **Algorithm**: RandomForestClassifier (100 estimators, balanced class weights)
- **Categories**: news, blog, product, documentation, forum, social, landing, ecommerce, article, other
- **Auto-retrain**: Triggers after 100 new labeled samples