Spaces:
Sleeping

File size: 4,158 Bytes
dc3cfa3
 
 
 
 
 
 
 
 
 
 
 
4bf6e07
e76c748
 
dc3cfa3
 
4bf6e07
e76c748
dc3cfa3
 
 
 
 
 
 
4bf6e07
 
dc3cfa3
4bf6e07
dc3cfa3
4bf6e07
 
dc3cfa3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a9427c
dc3cfa3
2a9427c
dc3cfa3
 
4bf6e07
dc3cfa3
 
8d1cb85
dc3cfa3
 
 
 
 
 
e76c748
dc3cfa3
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
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}")