Spaces:
Running
title: KYS School Scraper
emoji: π«
colorFrom: indigo
colorTo: gray
sdk: docker
pinned: false
π« KYS School Scraper & Data Processing Pipeline
An automated, end-to-end data pipeline to harvest school data from the KYS UDISE+ portal, process complex district boundary changes, and compile a finalized master dataset seamlessly on HuggingFace.
Pipeline Flow: Scrape β Auto-push Raw β Map Districts β Build Mapped Master
β¨ Features & Capabilities
- Targeted Scraping: Operates strictly at the District Level, capturing only 6 targeted school categories (3, 5, 6, 7, 10, 11).
- Intelligent Retries: Automatically detects CAPTCHA failures and retries only missing data. The terminal UI wipes clean automatically before each operation.
- UDISE Geo-Decoding: For non-actual state scrapes (e.g., KVS, NVS, NAVY, IAF), it parses the UDISE code to determine the real State, District, and Block.
- Automated UDISE-Tracking Math Engine: When building the master sheet, an intelligent math engine automatically resolves renamed districts and blocks!
- It cross-references newly scraped UDISE codes against a cloud-stored Scholarship Application Baseline (
baseline_master.parquet). - If a district was simply renamed (e.g., EAST DISTRICT -> GANGTOK), the math engine statistically tracks the UDISE codes, proves the rename, and automatically generates a mapping rule in the cloud (
manual_district_mapping.parquet). - For complex fractured districts, it drills down to the Block level (
manual_block_mapping.parquet) to perfectly map schools to their old districts.
- It cross-references newly scraped UDISE codes against a cloud-stored Scholarship Application Baseline (
- HuggingFace Cloud Architecture: Syncs directly to a HuggingFace dataset organized into 3 folders:
district_reference: Source of truth for Scholarship Application district tracking (Dataset 1).mapping_rules: Automated + manual district & block mapping rules (Dataset 2) & the SF Baseline Master.scraped_data: Holds bothrawper-state files and the finalmappedmaster sheet.
- Smart District Flagging (Hybrid System): Because the math engine automatically handles all simple renames behind the scenes, the UI only flags genuinely brand-new or highly ambiguous districts for your manual review in the Master Sheet tab!
π Prerequisites & Setup
- Google Chrome installed.
- Tesseract OCR (Windows):
- Download from UB-Mannheim (e.g.,
tesseract-ocr-w64-setup-5.5.0.20241111.exe). - Run the installer and add the path (usually
C:\Program Files\Tesseract-OCR) to your System Environment Variables underPath.
- Download from UB-Mannheim (e.g.,
- Python 3.10+.
- Installation:
python -m venv venv .\venv\Scripts\activate pip install -r requirements.txt playwright install chromium - Environment Setup (Required for HuggingFace Dataset Sync):
- Rename
.env.exampleto.envin the root folder. - Add your HuggingFace token and repository path:
HF_TOKEN=hf_your_actual_token_here HF_REPO=Apf-AI4Good/kys-school-data
- Rename
π The Web Application (Gradio)
The absolute easiest way to use the pipeline is via the interactive browser UI. Chromium launches minimized in the background so it never interrupts your workflow!
python app.py
The app opens automatically at http://127.0.0.1:7861 and features 4 main tabs.
π Tab 1: Scraper
The core scraping workflow, all on one screen:
- βΆ Start Scraping
- Navigates the UDISE+ portal, finds all districts for the selected state, and collects data for the target categories.
- β» Fix Missing Data (Retries)
- Scans for CAPTCHA failures and re-scrapes only the failed records. No successful data is ever lost.
- β
Automatic Push
- Once a state finishes scraping without missing data, it is automatically pushed to the cloud in your HuggingFace dataset.
Repeat Steps 1β3 for every state you want to scrape. Then proceed to the other tabs.
π Tab 2: Master Sheet
Combines all raw state data from HuggingFace into a single mapped master Excel file, and manages flagged districts.
- State Coverage Table: Connects to your HuggingFace dataset and displays which states are ready to build. You can delete raw data directly from here (which strictly auto-cleans any pending districts for that state!).
- Review New Districts: Automatically detects newly scraped districts not in your Scholarship Application database. You can instantly map them to older Scholarship Application districts or rename them before building.
- Build Master Sheet: Automatically:
- Pulls all raw complete state parquets.
- Applies geo-decoding for KVS/NVS/NAVY schools.
- Applies district back-mapping based on your rules.
- Tags schools with their
Status(e.g.,presentornew districts found). - Saves the final mapped file to
scraped_data/mapped/on HuggingFace and provides an Excel download.
πΊοΈ Tab 3: Mapping Manager
A full database management dashboard to maintain your Scholarship Application mappings.
- Scholarship Application District Reference: View and manage your master district list.
- Click-to-Edit Rows: Click any row in the table to instantly populate a quick-edit dropdown for updating its Status.
- Bulk Excel Import/Export: Expand the accordion to download the table, make bulk edits in Excel, and drag-and-drop it back to seamlessly sync with the cloud.
- Smart CRUD Tools: Add, Rename, and Delete districts. If you rename a district here, it perfectly cascades and updates your manual mapping rules automatically!
π₯ Tab 4: Download History
- Easily browse, refresh, and download any previously built Master Sheets directly from your HuggingFace cloud storage without having to rebuild them!
π οΈ Admin Scripts
If you need to make global, architectural changes to the baseline data outside of the UI, use the provided admin scripts:
admin_rename_sf_district.py
If an old Scholarship Application district name is permanently outdated and you want to rename it everywhere (becoming the new Baseline truth):
python admin_rename_sf_district.py --state "SIKKIM" --old "EAST DISTRICT (GANGTOK)" --new "GANGTOK"
This script updates Dataset 1, rewrites the Baseline Master, and instantly re-runs the automated math engine for that state to guarantee your mapping rules stay mathematically synchronized!
π» Running via Terminal (Headless Mode)
# Step 1: Main scrape
python -m pytest tests/test_scrape_districts_by_category.py -v -s --state "GOA"
# Step 2: Retry failures
python -m pytest tests/test_retry_districts_by_category.py -v -s --state "GOA"
# Step 3: Export raw Excel
python export_to_excel.py --state "GOA"
(Filter categories: $env:KYS_TARGET_CATEGORIES="3,5" ; python ...)
π Hosting on Hugging Face Spaces (Docker)
- Create a Docker Space on Hugging Face (Blank Template).
- Upload all
.pyfiles,requirements.txt,Dockerfile,README.md, and thetests/andpages/folders. Do NOT upload.env,.git, orvenv/. - In your Space Settings > Variables and secrets, add
HF_TOKENandHF_REPO. - The Dockerfile automatically installs Chromium and Tesseract OCR, and launches your Gradio app publicly!
π Project Structure & Output Files
| File | Purpose |
|---|---|
output/session_cookies.json |
Browser session cookies (auto-refreshed) |
output/goa_district_id_map.json |
District dropdown mapping IDs for the portal |
output/goa_schools_by_category.json |
Raw scraped JSON arrays & API responses |
output_excel/goa_Schools.xlsx |
Raw Excel Export (before mapping) |
HF Dataset: district_reference/ |
Dataset 1: The Scholarship Application reference mapping |
HF Dataset: mapping_rules/ |
Dataset 2: District and Block mapping rules |
HF Dataset: scraped_data/raw/{state}.parquet |
Per-state raw scraped data |
HF Dataset: scraped_data/mapped/ |
Final combined mapped master output |