Download src/config.py from mkhanumer/GardCom: direct link, hf CLI and curl.
- Browser
- Download file 4.16 kB
-
https://huggingface.co/spaces/mkhanumer/GardCom/resolve/main/src/config.py
- Command line
-
hf download hf://spaces/mkhanumer/GardCom/src/config.py
-
curl -L -o config.py https://huggingface.co/spaces/mkhanumer/GardCom/resolve/main/src/config.py
4.16 kB
| """ | |
| config.py β GardCom AI Model Configuration | |
| ai_model/src/config.py | |
| Exports exactly what app.py and predict.py import β nothing more: | |
| app.py imports: CLASS_MAP_PATH, FLASK_HOST, FLASK_PORT, | |
| MAX_CONTENT_LENGTH, MODEL_PATH, | |
| TOP_K_DEFAULT, TRAINING_SUMMARY_PATH | |
| predict.py imports: CLASS_MAP_PATH, IMAGE_SIZE, MODEL_PATH, | |
| OOD_THRESHOLD, TOP_K_DEFAULT | |
| """ | |
| import os | |
| from pathlib import Path | |
| # ββ Base directory (ai_model/) βββββββββββββββββββββββββββββββββββββββββββββ | |
| # resolve().parent goes: config.py β src/ β ai_model/ | |
| BASE_DIR = Path(__file__).resolve().parent.parent | |
| # ββ Dynamic path finder (matches your working HF config) ββββββββββββββββββ | |
| # Searches multiple locations so the same file works on: | |
| # β’ Local dev (ai_model/saved_models_run2/) | |
| # β’ Hugging Face (may be mounted at a different relative path) | |
| def _find_path(filename: str) -> Path: | |
| candidates = [ | |
| BASE_DIR / "saved_models_run2" / filename, | |
| BASE_DIR / "GardCom" / "saved_models_run2" / filename, | |
| BASE_DIR / "src" / "saved_models_run2" / filename, | |
| ] | |
| for path in candidates: | |
| if path.exists(): | |
| return path | |
| # Return the first candidate as default (even if it doesn't exist yet) | |
| # so error messages show a sensible path instead of None | |
| return candidates[0] | |
| # ββ File paths βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| MODEL_PATH = _find_path("best_model.keras") | |
| CLASS_MAP_PATH = _find_path("class_map.json") | |
| # TRAINING_SUMMARY_PATH: app.py /api/stats serves this file. | |
| # The pipeline writes training_log.csv β not training_summary.json. | |
| # We point to training_log.csv; if neither exists /api/stats returns a | |
| # graceful "not available" message so the build never breaks. | |
| TRAINING_SUMMARY_PATH = _find_path("training_log.csv") | |
| # ββ Image settings βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| IMAGE_SIZE = (224, 224) # EfficientNetB0 native resolution β do not change | |
| # ββ Prediction thresholds ββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # 0.70 was rejecting valid predictions for small-dataset classes | |
| # (Mango ~100 imgs, Sugarcane ~100 imgs). 0.35 matches your working HF config. | |
| OOD_THRESHOLD = float(os.environ.get("OOD_THRESHOLD", 0.35)) | |
| TOP_K_DEFAULT = int(os.environ.get("TOP_K_DEFAULT", 3)) | |
| # ββ Upload size limit ββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| MAX_CONTENT_LENGTH = int(os.environ.get("MAX_CONTENT_LENGTH", 16 * 1024 * 1024)) # 16 MB | |
| # ββ Flask server βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| FLASK_HOST = os.environ.get("FLASK_HOST", "0.0.0.0") | |
| # Hugging Face Spaces REQUIRES port 7860. | |
| # Using os.environ.get("PORT", 7860) means: | |
| # β’ On HF β PORT env var is not set β defaults to 7860 β | |
| # β’ On HF β if HF ever sets PORT it will be respected β | |
| # β’ Locally β set PORT=5000 in your .env to override β | |
| FLASK_PORT = int(os.environ.get("PORT", 7860)) | |
| # ββ Debug print (matches your working HF config) ββββββββββββββββββββββββββ | |
| print("--- CONFIG LOADED ---") | |
| print(f"MODEL_PATH: {MODEL_PATH} (exists: {MODEL_PATH.exists()})") | |
| print(f"CLASS_MAP_PATH: {CLASS_MAP_PATH} (exists: {CLASS_MAP_PATH.exists()})") | |
| print(f"FLASK_PORT: {FLASK_PORT}") | |
| print(f"OOD_THRESHOLD: {OOD_THRESHOLD}") |