Spaces:
Sleeping
Sleeping
Commit
Β·
23991e7
1
Parent(s):
808378f
No Emojii
Browse files- deploy.sh +24 -0
- startup.py +118 -0
deploy.sh
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
# Deployment script for Hugging Face Spaces
|
| 4 |
+
|
| 5 |
+
echo "π Preparing CAMS Air Pollution Dashboard for Hugging Face deployment"
|
| 6 |
+
|
| 7 |
+
# Build the Docker image
|
| 8 |
+
echo "π¦ Building Docker image..."
|
| 9 |
+
docker build -t cams-pollution-dashboard .
|
| 10 |
+
|
| 11 |
+
# Test the container locally (optional)
|
| 12 |
+
echo "π§ͺ To test locally, run:"
|
| 13 |
+
echo "docker run -p 7860:7860 cams-pollution-dashboard"
|
| 14 |
+
|
| 15 |
+
echo ""
|
| 16 |
+
echo "π Deployment checklist for Hugging Face Spaces:"
|
| 17 |
+
echo "1. Create a new Space on Hugging Face"
|
| 18 |
+
echo "2. Select 'Docker' as the SDK"
|
| 19 |
+
echo "3. Upload all files including Dockerfile"
|
| 20 |
+
echo "4. Make sure app_readme.md contains the correct YAML frontmatter"
|
| 21 |
+
echo "5. Push to your Hugging Face repository"
|
| 22 |
+
|
| 23 |
+
echo ""
|
| 24 |
+
echo "β
Ready for deployment!"
|
startup.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Startup script to ensure all required files are available before running the main app
|
| 4 |
+
"""
|
| 5 |
+
import os
|
| 6 |
+
import sys
|
| 7 |
+
import subprocess
|
| 8 |
+
import urllib.request
|
| 9 |
+
import zipfile
|
| 10 |
+
import geopandas as gpd
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
|
| 13 |
+
def ensure_shapefile_exists():
|
| 14 |
+
"""Ensure the India State Boundary shapefile exists"""
|
| 15 |
+
shapefile_path = "shapefiles/India_State_Boundary.shp"
|
| 16 |
+
|
| 17 |
+
if os.path.exists(shapefile_path):
|
| 18 |
+
print(f"β Shapefile found at {shapefile_path}")
|
| 19 |
+
return True
|
| 20 |
+
|
| 21 |
+
print(f"β Shapefile not found at {shapefile_path}")
|
| 22 |
+
|
| 23 |
+
# Try to pull from git lfs if available
|
| 24 |
+
try:
|
| 25 |
+
if os.path.exists(".git"):
|
| 26 |
+
print("Attempting to pull LFS files...")
|
| 27 |
+
result = subprocess.run(["git", "lfs", "pull"], capture_output=True, text=True)
|
| 28 |
+
if result.returncode == 0 and os.path.exists(shapefile_path):
|
| 29 |
+
print("β Successfully pulled LFS files")
|
| 30 |
+
return True
|
| 31 |
+
else:
|
| 32 |
+
print("β Git LFS pull failed or file still missing")
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(f"β Git LFS not available: {e}")
|
| 35 |
+
|
| 36 |
+
# Download alternative shapefile
|
| 37 |
+
print("Downloading alternative India boundary data...")
|
| 38 |
+
try:
|
| 39 |
+
# Create shapefiles directory
|
| 40 |
+
os.makedirs("shapefiles", exist_ok=True)
|
| 41 |
+
|
| 42 |
+
# Download from Natural Earth (reliable source)
|
| 43 |
+
url = "https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_countries.zip"
|
| 44 |
+
zip_path = "temp_countries.zip"
|
| 45 |
+
|
| 46 |
+
print("Downloading Natural Earth country boundaries...")
|
| 47 |
+
urllib.request.urlretrieve(url, zip_path)
|
| 48 |
+
|
| 49 |
+
# Extract
|
| 50 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 51 |
+
zip_ref.extractall("temp_extract")
|
| 52 |
+
|
| 53 |
+
# Load and filter for India
|
| 54 |
+
world_data = gpd.read_file("temp_extract/ne_50m_admin_0_countries.shp")
|
| 55 |
+
india_data = world_data[world_data['NAME'] == 'India']
|
| 56 |
+
|
| 57 |
+
if len(india_data) == 0:
|
| 58 |
+
# Try alternative name matching
|
| 59 |
+
india_data = world_data[world_data['NAME_EN'] == 'India']
|
| 60 |
+
|
| 61 |
+
if len(india_data) > 0:
|
| 62 |
+
# Save as our expected shapefile
|
| 63 |
+
india_data.to_file(shapefile_path)
|
| 64 |
+
print(f"β Successfully created {shapefile_path}")
|
| 65 |
+
else:
|
| 66 |
+
raise Exception("Could not find India in the world data")
|
| 67 |
+
|
| 68 |
+
# Cleanup
|
| 69 |
+
os.remove(zip_path)
|
| 70 |
+
import shutil
|
| 71 |
+
shutil.rmtree("temp_extract", ignore_errors=True)
|
| 72 |
+
|
| 73 |
+
return True
|
| 74 |
+
|
| 75 |
+
except Exception as e:
|
| 76 |
+
print(f"β Failed to download alternative shapefile: {e}")
|
| 77 |
+
|
| 78 |
+
# Create a simple fallback
|
| 79 |
+
print("Creating fallback boundary...")
|
| 80 |
+
try:
|
| 81 |
+
from shapely.geometry import Polygon
|
| 82 |
+
|
| 83 |
+
# Simple India bounding box
|
| 84 |
+
india_bounds = [68.0, 6.0, 97.5, 37.0] # [min_lon, min_lat, max_lon, max_lat]
|
| 85 |
+
|
| 86 |
+
polygon = Polygon([
|
| 87 |
+
(india_bounds[0], india_bounds[1]), # SW
|
| 88 |
+
(india_bounds[2], india_bounds[1]), # SE
|
| 89 |
+
(india_bounds[2], india_bounds[3]), # NE
|
| 90 |
+
(india_bounds[0], india_bounds[3]), # NW
|
| 91 |
+
(india_bounds[0], india_bounds[1]) # Close
|
| 92 |
+
])
|
| 93 |
+
|
| 94 |
+
gdf = gpd.GeoDataFrame([1], geometry=[polygon], crs="EPSG:4326")
|
| 95 |
+
gdf['NAME'] = 'India'
|
| 96 |
+
gdf.to_file(shapefile_path)
|
| 97 |
+
|
| 98 |
+
print(f"β Created fallback boundary at {shapefile_path}")
|
| 99 |
+
return True
|
| 100 |
+
|
| 101 |
+
except Exception as e2:
|
| 102 |
+
print(f"β Failed to create fallback: {e2}")
|
| 103 |
+
return False
|
| 104 |
+
|
| 105 |
+
if __name__ == "__main__":
|
| 106 |
+
print("π Starting CAMS Pollution Dashboard...")
|
| 107 |
+
print("Checking required files...")
|
| 108 |
+
|
| 109 |
+
if ensure_shapefile_exists():
|
| 110 |
+
print("β All required files are ready")
|
| 111 |
+
print("Starting Flask application...")
|
| 112 |
+
|
| 113 |
+
# Import and run the main app
|
| 114 |
+
from app import app
|
| 115 |
+
app.run(host='0.0.0.0', port=7860, debug=False)
|
| 116 |
+
else:
|
| 117 |
+
print("β Failed to ensure required files exist")
|
| 118 |
+
sys.exit(1)
|