Spaces:
Sleeping
Sleeping
| # main.py | |
| # Application entrypoint — dual-mode: API server or CLI inference on a crop GeoTIFF. | |
| import argparse | |
| import json | |
| import logging | |
| import os | |
| import sys | |
| # Ensure the project root is on sys.path so `src` can be imported. | |
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | |
| from src.logger import setup_logging | |
| setup_logging() | |
| logger = logging.getLogger("boot") | |
| def main(): | |
| parser = argparse.ArgumentParser( | |
| description="GeoGlyph SAM2 — API server and CLI for geoglyph detection.", | |
| ) | |
| # --------------------------------------------------------------------- | |
| # CLI mode | |
| # --------------------------------------------------------------------- | |
| parser.add_argument( | |
| "--cli", | |
| action="store_true", | |
| help="Run a single inference from the command line instead of starting the API server.", | |
| ) | |
| parser.add_argument( | |
| "--crop", | |
| type=str, | |
| help="Path to a small georeferenced GeoTIFF crop. Required for --cli.", | |
| ) | |
| parser.add_argument( | |
| "--output", | |
| type=str, | |
| help="Output GeoPackage path. Required for --cli.", | |
| ) | |
| parser.add_argument( | |
| "--device", | |
| type=str, | |
| default=None, | |
| choices=["cuda", "cpu"], | |
| help="Inference device: cuda | cpu. Auto-detected by default.", | |
| ) | |
| # --------------------------------------------------------------------- | |
| # API mode | |
| # --------------------------------------------------------------------- | |
| parser.add_argument( | |
| "--host", | |
| type=str, | |
| default="0.0.0.0", | |
| help="API server host. Default: 0.0.0.0.", | |
| ) | |
| parser.add_argument( | |
| "--port", | |
| type=int, | |
| default=8000, | |
| help="API server port. Default: 8000.", | |
| ) | |
| args = parser.parse_args() | |
| if args.cli: | |
| if not args.crop or not args.output: | |
| logger.error("--crop and --output are required in CLI mode.") | |
| sys.exit(1) | |
| from src.infer import run_geoglyph_sam2_on_crop | |
| try: | |
| logger.info( | |
| "CLI inference | crop=%s output=%s device=%s", | |
| args.crop, | |
| args.output, | |
| args.device, | |
| ) | |
| result = run_geoglyph_sam2_on_crop( | |
| crop_tif_path=args.crop, | |
| output_gpkg=args.output, | |
| device=args.device, | |
| ) | |
| logger.info( | |
| "CLI completed | n_masks=%d output=%s", | |
| result["n_masks"], | |
| result["output_gpkg"], | |
| ) | |
| print(json.dumps(result, indent=2)) | |
| except Exception as exc: | |
| logger.error("CLI inference failed: %s", exc, exc_info=True) | |
| sys.exit(1) | |
| else: | |
| import uvicorn | |
| logger.info("Starting GeoGlyph SAM2 API on %s:%d", args.host, args.port) | |
| uvicorn.run( | |
| "src.api:app", | |
| host=args.host, | |
| port=args.port, | |
| reload=False, | |
| ) | |
| if __name__ == "__main__": | |
| main() | |
| """ | |
| python main.py --cli ^ | |
| --crop "C:\path\to\sam2_crop.tif" ^ | |
| --output "C:\path\to\sam2_result.gpkg" ^ | |
| --device cpu | |
| python main.py --host 0.0.0.0 --port 8000 | |
| """ |