Spaces:
Sleeping

GardCom / src /config.py
Muhammad
Update src/config.py
dc3cfa3 verified
Raw History Blame Contribute Delete
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}")