ajayduraisamy commited on
Commit
cb997a6
·
1 Parent(s): 0b218e1
.gitignore DELETED
@@ -1,30 +0,0 @@
1
- # Secrets — NEVER commit (HF Space uses Secrets/Variables instead).
2
- .env
3
- **/.env
4
-
5
- # Runtime sqlite DBs (ephemeral on HF; schema auto-creates on startup).
6
- *.db
7
- *.sqlite
8
- *.sqlite3
9
-
10
- # Python
11
- __pycache__/
12
- *.py[cod]
13
- .venv/
14
- venv/
15
-
16
- # Banana backend runtime-generated files (ephemeral on HF).
17
- banana_ripeness_stages/storage/uploads/*
18
- banana_ripeness_stages/storage/explanations/*
19
- banana_ripeness_stages/storage/shap_background/*
20
- !banana_ripeness_stages/storage/uploads/.gitkeep
21
- !banana_ripeness_stages/storage/explanations/.gitkeep
22
- !banana_ripeness_stages/storage/shap_background/.gitkeep
23
-
24
- # Flask inference log (local only).
25
- predictions.csv
26
-
27
- # NOTE: model weights (*.pth) are intentionally NOT ignored — the
28
- # banana .pth files must be committed (via Git LFS) to the Space.
29
-
30
- fruit_veg.pt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Dockerfile CHANGED
@@ -14,15 +14,11 @@ COPY requirements.txt .
14
  RUN pip install --upgrade pip
15
  RUN pip install --no-cache-dir -r requirements.txt
16
 
17
- # Existing Flask app (untouched) + unified ASGI gateway (new) +
18
- # banana backend (new, mounted at /banana_ripeness_stages).
19
  COPY app.py gateway.py ./
20
- COPY banana_ripeness_stages ./banana_ripeness_stages
21
  COPY models ./models
22
 
23
  EXPOSE 7860
24
 
25
- # Single public HTTP service: uvicorn serves the gateway which routes
26
- # /banana_ripeness_stages/* to the banana FastAPI app and everything
27
- # else to the existing Flask app.
28
  CMD ["uvicorn", "gateway:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "1", "--timeout-keep-alive", "120"]
 
14
  RUN pip install --upgrade pip
15
  RUN pip install --no-cache-dir -r requirements.txt
16
 
17
+ # Existing Flask app and ASGI adapter.
 
18
  COPY app.py gateway.py ./
 
19
  COPY models ./models
20
 
21
  EXPOSE 7860
22
 
23
+ # Single public HTTP service: Uvicorn serves the Flask app through its ASGI adapter.
 
 
24
  CMD ["uvicorn", "gateway:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "1", "--timeout-keep-alive", "120"]
banana_ripeness_stages/app/config.py DELETED
@@ -1,117 +0,0 @@
1
- import os
2
- from pathlib import Path
3
-
4
- from dotenv import load_dotenv
5
-
6
- BASE_DIR = Path(__file__).resolve().parent.parent # backend/
7
- PROJECT_ROOT = BASE_DIR.parent # project root
8
-
9
- load_dotenv(BASE_DIR / ".env")
10
-
11
- SECRET_KEY = os.getenv("SECRET_KEY", "dev-secret-change-me-in-production")
12
- ALGORITHM = "HS256"
13
- ACCESS_TOKEN_EXPIRE_MINUTES = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", "1440"))
14
-
15
-
16
- def _resolve_path(value: str) -> Path:
17
- """Resolve a file path robustly regardless of process CWD.
18
-
19
- - Absolute paths are used as-is.
20
- - Relative paths that exist relative to the CWD are used as-is
21
- (preserves existing local-dev behaviour).
22
- - Otherwise the path is resolved relative to BASE_DIR
23
- (banana_ripeness_stages/), so the backend keeps loading its own
24
- files when served from the Space root (/app) via a mount.
25
- - Final fallback: BASE_DIR / <filename>, which handles legacy
26
- .env values such as '../models/best_*.pth' while the files live
27
- next to BASE_DIR.
28
- """
29
- p = Path(value)
30
- if p.is_absolute():
31
- return p
32
- if p.exists():
33
- return p
34
- candidate = (BASE_DIR / p).resolve()
35
- if candidate.exists():
36
- return candidate
37
- fallback = (BASE_DIR / p.name).resolve()
38
- if fallback.exists():
39
- return fallback
40
- return candidate
41
-
42
-
43
- def _resolve_database_url(url: str) -> str:
44
- """Resolve relative sqlite paths to BASE_DIR so app.db stays in the
45
- banana backend directory regardless of CWD. Non-sqlite URLs pass
46
- through untouched. Relative sqlite paths ALWAYS resolve under
47
- BASE_DIR (a stray CWD-relative app.db must never be used)."""
48
- if not url.startswith("sqlite:///"):
49
- return url
50
- raw = url[len("sqlite:///"):]
51
- if raw == ":memory:":
52
- return url
53
- p = Path(raw)
54
- if p.is_absolute():
55
- return url
56
- resolved = (BASE_DIR / p).resolve()
57
- if resolved.exists():
58
- return f"sqlite:///{resolved}"
59
- fallback = (BASE_DIR / p.name).resolve()
60
- return f"sqlite:///{fallback if fallback.exists() else resolved}"
61
-
62
-
63
- DATABASE_URL = _resolve_database_url(
64
- os.getenv("DATABASE_URL", f"sqlite:///{BASE_DIR / 'app.db'}"))
65
-
66
- STORAGE_DIR = BASE_DIR / "storage"
67
- UPLOAD_DIR = STORAGE_DIR / "uploads"
68
- EXPLAIN_DIR = STORAGE_DIR / "explanations"
69
- UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
70
- EXPLAIN_DIR.mkdir(parents=True, exist_ok=True)
71
-
72
- RIPENESS_MODEL_PATH = _resolve_path(os.getenv(
73
- "RIPENESS_MODEL_PATH",
74
- str(BASE_DIR / "best_multimodal_model.pth"),
75
- ))
76
- ADULTERATION_MODEL_PATH = _resolve_path(os.getenv(
77
- "ADULTERATION_MODEL_PATH",
78
- str(BASE_DIR / "best_adulteration_model.pth"),
79
- ))
80
-
81
- # URL prefix under which the banana backend is mounted in the unified
82
- # Space (e.g. "/banana_ripeness_stages"). Empty when run standalone.
83
- # Used to build correct /files/... URLs in API responses.
84
- # Set via BANANA_SUBPATH env var by the gateway.
85
- SUBPATH_PREFIX = os.getenv("BANANA_SUBPATH", "").rstrip("/")
86
-
87
-
88
- def files_url(*parts: str) -> str:
89
- """Build a public /files/... URL honouring the mount prefix."""
90
- suffix = "/".join(p.strip("/") for p in parts if p)
91
- return f"{SUBPATH_PREFIX}/files/{suffix}" if suffix else f"{SUBPATH_PREFIX}/files"
92
-
93
- RIPENESS_STAGES = [
94
- "Stage_1_Mature_Green",
95
- "Stage_2_Green_Yellow_Transition",
96
- "Stage_3_Early_Ripening",
97
- "Stage_4_Fully_Yellow",
98
- "Stage_5_Yellow_Spotted",
99
- ]
100
- # Representative ripeness percentage per stage (experimental heuristic).
101
- STAGE_RIPENESS_CENTERS = [12.0, 32.0, 55.0, 80.0, 96.0]
102
-
103
- # Adulteration class mapping (fixed by training: 0=Adulterated, 1=Natural)
104
- ADULTERATION_CLASSES = ["Adulterated", "Natural"]
105
-
106
- MAX_UPLOAD_MB = int(os.getenv("MAX_UPLOAD_MB", "25"))
107
- ALLOWED_IMAGE_EXTS = {".jpg", ".jpeg", ".png"}
108
- ALLOWED_MATRIX_EXTS = {".xlsx", ".xls", ".csv"}
109
-
110
- # Real-SHAP settings for the thermal-matrix explanation.
111
- SHAP_BG_DIR = STORAGE_DIR / "shap_background"
112
- SHAP_BG_DIR.mkdir(parents=True, exist_ok=True)
113
- SHAP_BACKGROUND_K = int(os.getenv("SHAP_BACKGROUND_K", "8")) # 4-16 recommended
114
- SHAP_NSAMPLES = int(os.getenv("SHAP_NSAMPLES", "25"))
115
- SHAP_BG_MAX = int(os.getenv("SHAP_BG_MAX", "16"))
116
-
117
- FRONTEND_ORIGIN = os.getenv("FRONTEND_ORIGIN", "http://localhost:5173")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
banana_ripeness_stages/app/database.py DELETED
@@ -1,17 +0,0 @@
1
- from sqlalchemy import create_engine
2
- from sqlalchemy.orm import declarative_base, sessionmaker
3
-
4
- from .config import DATABASE_URL
5
-
6
- connect_args = {"check_same_thread": False} if DATABASE_URL.startswith("sqlite") else {}
7
- engine = create_engine(DATABASE_URL, connect_args=connect_args)
8
- SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
9
- Base = declarative_base()
10
-
11
-
12
- def get_db():
13
- db = SessionLocal()
14
- try:
15
- yield db
16
- finally:
17
- db.close()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
banana_ripeness_stages/app/deps.py DELETED
@@ -1,29 +0,0 @@
1
- from fastapi import Depends, HTTPException, status
2
- from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
3
- from sqlalchemy.orm import Session
4
-
5
- from . import models
6
- from .database import get_db
7
- from .utils.security import decode_token
8
-
9
- _bearer = HTTPBearer(auto_error=False)
10
-
11
-
12
- def get_current_user(
13
- creds: HTTPAuthorizationCredentials = Depends(_bearer),
14
- db: Session = Depends(get_db),
15
- ) -> models.User:
16
- if creds is None or not creds.credentials:
17
- raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
18
- detail="Authentication required")
19
- try:
20
- payload = decode_token(creds.credentials)
21
- user_id = int(payload.get("sub"))
22
- except Exception:
23
- raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
24
- detail="Invalid or expired token")
25
- user = db.query(models.User).filter(models.User.id == user_id).first()
26
- if user is None:
27
- raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
28
- detail="User not found")
29
- return user
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
banana_ripeness_stages/app/main.py DELETED
@@ -1,76 +0,0 @@
1
- import os
2
-
3
- from fastapi import FastAPI
4
- from fastapi.middleware.cors import CORSMiddleware
5
- from fastapi.staticfiles import StaticFiles
6
-
7
- from .config import (ADULTERATION_MODEL_PATH, EXPLAIN_DIR, FRONTEND_ORIGIN,
8
- RIPENESS_MODEL_PATH, STORAGE_DIR)
9
- from .database import Base, SessionLocal, engine
10
- from .models import ModelVersion # noqa: F401 (register tables)
11
- from .routers import auth, explain, models, prediction, profile, twins
12
-
13
- app = FastAPI(title="Banana Digital Twin API", version="1.0.0")
14
-
15
- app.add_middleware(
16
- CORSMiddleware,
17
- allow_origins=[FRONTEND_ORIGIN, "http://localhost:5173",
18
- "http://127.0.0.1:5173"],
19
- allow_credentials=True,
20
- allow_methods=["*"],
21
- allow_headers=["*"],
22
- )
23
-
24
- Base.metadata.create_all(bind=engine)
25
-
26
- # Lightweight migration for DBs created before the phone field existed.
27
- from sqlalchemy import text as _text
28
- with engine.begin() as _conn:
29
- _cols = [r[1] for r in
30
- _conn.execute(_text("PRAGMA table_info(users)")).fetchall()]
31
- if "phone" not in _cols:
32
- _conn.execute(_text("ALTER TABLE users ADD COLUMN phone VARCHAR(20)"))
33
- app.mount("/files", StaticFiles(directory=str(STORAGE_DIR)), name="files")
34
-
35
- app.include_router(auth.router)
36
- app.include_router(profile.router)
37
- app.include_router(models.router)
38
- app.include_router(prediction.router)
39
- app.include_router(twins.router)
40
- app.include_router(explain.router)
41
-
42
-
43
- @app.on_event("startup")
44
- def verify_models():
45
- missing = [str(p) for p in (RIPENESS_MODEL_PATH, ADULTERATION_MODEL_PATH)
46
- if not os.path.exists(p)]
47
- if missing:
48
- raise RuntimeError(
49
- "Model file(s) missing: " + ", ".join(missing) +
50
- ". Place the trained .pth files in models/ — never fake them.")
51
- # Lazy-load with caching; warm load here to fail fast with clear error.
52
- from .ml.adulteration_model import get_adulteration_model
53
- from .ml.ripeness_model import get_ripeness_model
54
- get_ripeness_model(str(RIPENESS_MODEL_PATH))
55
- get_adulteration_model(str(ADULTERATION_MODEL_PATH))
56
- db = SessionLocal()
57
- try:
58
- for name, path in (("ripeness_multimodal", RIPENESS_MODEL_PATH),
59
- ("adulteration", ADULTERATION_MODEL_PATH)):
60
- if not db.query(ModelVersion).filter(
61
- ModelVersion.model_name == name).first():
62
- db.add(ModelVersion(
63
- model_name=name, model_path=str(path), version="v1",
64
- metrics_json={"note": "experimental evaluation results; "
65
- "see landing page"}))
66
- db.commit()
67
- finally:
68
- db.close()
69
-
70
-
71
- @app.get("/api/health")
72
- def health():
73
- return {"status": "ok",
74
- "ripeness_model": os.path.exists(RIPENESS_MODEL_PATH),
75
- "adulteration_model": os.path.exists(ADULTERATION_MODEL_PATH),
76
- "explanations_dir": os.path.exists(EXPLAIN_DIR)}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
banana_ripeness_stages/app/ml/adulteration_model.py DELETED
@@ -1,40 +0,0 @@
1
- """Adulteration model: EfficientNet-B0, 2 classes (0=Adulterated, 1=Natural)."""
2
- import torch
3
- import torch.nn as nn
4
- from torchvision.models import efficientnet_b0
5
-
6
-
7
- class BananaAdulterationNet(nn.Module):
8
- def __init__(self, num_classes: int = 2):
9
- super().__init__()
10
- backbone = efficientnet_b0(weights=None)
11
- in_features = backbone.classifier[1].in_features
12
- backbone.classifier = nn.Sequential(
13
- nn.Dropout(0.30),
14
- nn.Linear(in_features, num_classes),
15
- )
16
- self.backbone = backbone
17
-
18
- def forward(self, x):
19
- return self.backbone(x)
20
-
21
-
22
- _model = None
23
-
24
-
25
- def get_adulteration_model(checkpoint_path: str):
26
- global _model
27
- if _model is not None:
28
- return _model
29
- import os
30
- if not os.path.exists(checkpoint_path):
31
- raise FileNotFoundError(
32
- f"Adulteration model not found at {checkpoint_path}. "
33
- "Place best_adulteration_model.pth in models/.")
34
- ckpt = torch.load(checkpoint_path, map_location="cpu", weights_only=False)
35
- sd = ckpt.get("model_state_dict", ckpt)
36
- model = BananaAdulterationNet(num_classes=2)
37
- model.load_state_dict(sd, strict=True)
38
- model.eval()
39
- _model = model
40
- return _model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
banana_ripeness_stages/app/ml/ripeness_model.py DELETED
@@ -1,86 +0,0 @@
1
- """Exact ripeness architecture from training (EfficientNet-B0 x2 + Matrix MLP)."""
2
- import torch
3
- import torch.nn as nn
4
- from torchvision.models import efficientnet_b0
5
-
6
-
7
- class MatrixMLP(nn.Module):
8
- def __init__(self, output_dim: int = 256):
9
- super().__init__()
10
- self.net = nn.Sequential(
11
- nn.Flatten(),
12
- nn.Linear(64 * 48, 512),
13
- nn.BatchNorm1d(512),
14
- nn.ReLU(inplace=True),
15
- nn.Dropout(0.30),
16
- nn.Linear(512, output_dim),
17
- nn.BatchNorm1d(output_dim),
18
- nn.ReLU(inplace=True),
19
- )
20
-
21
- def forward(self, x):
22
- return self.net(x)
23
-
24
-
25
- class EfficientNetBranch(nn.Module):
26
- def __init__(self, output_dim: int = 512):
27
- super().__init__()
28
- model = efficientnet_b0(weights=None)
29
- in_features = model.classifier[1].in_features
30
- model.classifier = nn.Identity()
31
- self.backbone = model
32
- self.projection = nn.Sequential(
33
- nn.Linear(in_features, output_dim),
34
- nn.BatchNorm1d(output_dim),
35
- nn.ReLU(inplace=True),
36
- nn.Dropout(0.30),
37
- )
38
-
39
- def forward(self, x):
40
- return self.projection(self.backbone(x))
41
-
42
-
43
- class BananaMultimodalNet(nn.Module):
44
- def __init__(self, num_classes: int = 5):
45
- super().__init__()
46
- self.rgb_branch = EfficientNetBranch(output_dim=512)
47
- self.thermal_branch = EfficientNetBranch(output_dim=512)
48
- self.matrix_branch = MatrixMLP(output_dim=256)
49
- self.fusion = nn.Sequential(
50
- nn.Linear(512 + 512 + 256, 512),
51
- nn.BatchNorm1d(512),
52
- nn.ReLU(inplace=True),
53
- nn.Dropout(0.40),
54
- nn.Linear(512, 256),
55
- nn.BatchNorm1d(256),
56
- nn.ReLU(inplace=True),
57
- nn.Dropout(0.30),
58
- )
59
- self.classifier = nn.Linear(256, num_classes)
60
-
61
- def forward(self, rgb, thermal, matrix):
62
- f = torch.cat(
63
- [self.rgb_branch(rgb), self.thermal_branch(thermal),
64
- self.matrix_branch(matrix)], dim=1)
65
- return self.classifier(self.fusion(f))
66
-
67
-
68
- _model = None
69
-
70
-
71
- def get_ripeness_model(checkpoint_path: str):
72
- global _model
73
- if _model is not None:
74
- return _model
75
- import os
76
- if not os.path.exists(checkpoint_path):
77
- raise FileNotFoundError(
78
- f"Ripeness model not found at {checkpoint_path}. "
79
- "Place best_multimodal_model.pth in models/.")
80
- ckpt = torch.load(checkpoint_path, map_location="cpu", weights_only=False)
81
- sd = ckpt.get("model_state_dict", ckpt)
82
- model = BananaMultimodalNet(num_classes=int(ckpt.get("num_classes", 5)))
83
- model.load_state_dict(sd, strict=True)
84
- model.eval()
85
- _model = model
86
- return _model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
banana_ripeness_stages/app/ml/transforms.py DELETED
@@ -1,105 +0,0 @@
1
- """Inference-time preprocessing matching training (no pandas dependency)."""
2
- import csv
3
- import re
4
- from pathlib import Path
5
-
6
- import numpy as np
7
- import torch
8
- from openpyxl import load_workbook
9
- from PIL import Image, ImageOps
10
- from torchvision.transforms import InterpolationMode
11
- import torchvision.transforms.functional as TF
12
-
13
- _ADULT_MEAN = [0.485, 0.456, 0.406]
14
- _ADULT_STD = [0.229, 0.224, 0.225]
15
-
16
-
17
- def _load_pil(path: str | Path) -> Image.Image:
18
- img = Image.open(path)
19
- img = ImageOps.exif_transpose(img)
20
- return img.convert("RGB")
21
-
22
-
23
- def preprocess_ripeness_image(path: str | Path) -> torch.Tensor:
24
- """Resize 256 -> center-crop 224 -> ToTensor (no Normalize, as trained)."""
25
- img = _load_pil(path)
26
- img = TF.resize(img, [256, 256], interpolation=InterpolationMode.BILINEAR)
27
- img = TF.center_crop(img, [224, 224])
28
- return TF.to_tensor(img)
29
-
30
-
31
- def preprocess_adulteration_image(path: str | Path) -> torch.Tensor:
32
- """Resize 256 -> center-crop 224 -> ToTensor -> ImageNet Normalize."""
33
- img = _load_pil(path)
34
- img = TF.resize(img, [256, 256], interpolation=InterpolationMode.BILINEAR)
35
- img = TF.center_crop(img, [224, 224])
36
- t = TF.to_tensor(img)
37
- return TF.normalize(t, mean=_ADULT_MEAN, std=_ADULT_STD)
38
-
39
-
40
- def _parse_temp(value) -> float:
41
- if value is None:
42
- return float("nan")
43
- if isinstance(value, (int, float, np.integer, np.floating)):
44
- return float(value)
45
- m = re.search(r"-?\d+(?:\.\d+)?", str(value))
46
- return float(m.group()) if m else float("nan")
47
-
48
-
49
- def _read_raw_matrix(path: str | Path) -> np.ndarray:
50
- p = str(path)
51
- if p.lower().endswith(".csv"):
52
- with open(p, newline="") as f:
53
- rows = list(csv.reader(f))
54
- return np.array([[_parse_temp(v) for v in r] for r in rows],
55
- dtype=np.float32)
56
- wb = load_workbook(p, read_only=True, data_only=True)
57
- ws = wb.active
58
- rows = list(ws.iter_rows(values_only=True))
59
- wb.close()
60
- width = max((len(r) for r in rows), default=0)
61
- arr = np.full((len(rows), width), np.nan, dtype=np.float32)
62
- for i, r in enumerate(rows):
63
- for j, v in enumerate(r):
64
- arr[i, j] = _parse_temp(v)
65
- return arr
66
-
67
-
68
- def load_thermal_matrix_tensor(path: str | Path) -> torch.Tensor:
69
- """Parse XLSX/CSV thermal matrix -> standardized [1, 64, 48] tensor."""
70
- numeric = _read_raw_matrix(path)
71
- if numeric.size == 0:
72
- raise ValueError("Thermal matrix file is empty or unreadable.")
73
- valid_cols = np.sum(np.isfinite(numeric), axis=0) > numeric.shape[0] * 0.5
74
- numeric = numeric[:, valid_cols]
75
- valid_rows = np.sum(np.isfinite(numeric), axis=1) > numeric.shape[1] * 0.5
76
- numeric = numeric[valid_rows, :]
77
- if numeric.shape[0] == 256 and numeric.shape[1] >= 192:
78
- numeric = numeric[:, :192]
79
- if numeric.shape != (256, 192):
80
- raise ValueError(
81
- f"Thermal matrix must resolve to 256x192 cells, got {numeric.shape}.")
82
- if np.isnan(numeric).any():
83
- numeric = np.nan_to_num(numeric, nan=float(np.nanmean(numeric)))
84
- numeric = numeric.astype(np.float32)
85
- mean, std = float(numeric.mean()), float(numeric.std())
86
- if std < 1e-6:
87
- std = 1.0
88
- numeric = (numeric - mean) / std
89
- t = torch.from_numpy(numeric).unsqueeze(0).unsqueeze(0) # [1,1,256,192]
90
- t = torch.nn.functional.interpolate(t, size=(64, 48), mode="bilinear",
91
- align_corners=False)
92
- return t.squeeze(0) # [1,64,48]
93
-
94
-
95
- def matrix_summary_stats(path: str | Path) -> dict:
96
- """Raw (unstandardized) stats for display + XAI context."""
97
- numeric = _read_raw_matrix(path)
98
- valid = numeric[np.isfinite(numeric)]
99
- if valid.size == 0:
100
- raise ValueError("No numeric temperature data found in matrix file.")
101
- return {
102
- "min": float(valid.min()), "max": float(valid.max()),
103
- "mean": float(valid.mean()), "std": float(valid.std()),
104
- "shape": list(numeric.shape),
105
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
banana_ripeness_stages/app/models.py DELETED
@@ -1,85 +0,0 @@
1
- from datetime import datetime
2
-
3
- from sqlalchemy import JSON, Column, DateTime, Float, ForeignKey, Integer, String, Text
4
-
5
- from .database import Base
6
-
7
-
8
- def _now():
9
- return datetime.utcnow()
10
-
11
-
12
- class User(Base):
13
- __tablename__ = "users"
14
- id = Column(Integer, primary_key=True, index=True)
15
- name = Column(String(120), nullable=False)
16
- email = Column(String(255), nullable=False, unique=True, index=True)
17
- phone = Column(String(20), nullable=True)
18
- password_hash = Column(String(255), nullable=False)
19
- created_at = Column(DateTime, default=_now)
20
-
21
-
22
- class BananaTwin(Base):
23
- __tablename__ = "banana_twins"
24
- id = Column(Integer, primary_key=True, index=True)
25
- twin_id = Column(String(16), unique=True, index=True, nullable=False) # BT-000001
26
- owner_user_id = Column(Integer, ForeignKey("users.id"), nullable=False, index=True)
27
- name = Column(String(120), nullable=False, default="My Banana")
28
- current_stage = Column(String(64), nullable=True)
29
- ripeness_percentage = Column(Float, nullable=True)
30
- adulteration_status = Column(String(32), nullable=True)
31
- adulteration_probability = Column(Float, nullable=True)
32
- confidence = Column(Float, nullable=True)
33
- created_at = Column(DateTime, default=_now)
34
- updated_at = Column(DateTime, default=_now, onupdate=_now)
35
-
36
-
37
- class Observation(Base):
38
- __tablename__ = "observations"
39
- id = Column(Integer, primary_key=True, index=True)
40
- twin_id = Column(String(16), index=True, nullable=False)
41
- rgb_file_path = Column(String(512), nullable=True)
42
- thermal_file_path = Column(String(512), nullable=True)
43
- matrix_file_path = Column(String(512), nullable=True)
44
- observation_time = Column(DateTime, default=_now)
45
- predicted_stage = Column(String(64), nullable=True)
46
- ripeness_percentage = Column(Float, nullable=True)
47
- adulteration_status = Column(String(32), nullable=True)
48
- adulteration_probability = Column(Float, nullable=True)
49
- confidence = Column(Float, nullable=True)
50
- created_at = Column(DateTime, default=_now)
51
-
52
-
53
- class Prediction(Base):
54
- __tablename__ = "predictions"
55
- id = Column(Integer, primary_key=True, index=True)
56
- twin_id = Column(String(16), index=True, nullable=True)
57
- owner_user_id = Column(Integer, ForeignKey("users.id"), nullable=False, index=True)
58
- prediction_type = Column(String(32), nullable=False) # ripeness|adulteration|combined
59
- prediction_value = Column(String(128), nullable=False)
60
- probability = Column(Float, nullable=True)
61
- confidence = Column(Float, nullable=True)
62
- details_json = Column(JSON, nullable=True)
63
- created_at = Column(DateTime, default=_now)
64
-
65
-
66
- class Forecast(Base):
67
- __tablename__ = "forecasts"
68
- id = Column(Integer, primary_key=True, index=True)
69
- twin_id = Column(String(16), index=True, nullable=False)
70
- current_stage = Column(String(64), nullable=False)
71
- future_stage = Column(String(64), nullable=False)
72
- probability = Column(Float, nullable=True)
73
- estimated_time = Column(String(128), nullable=True)
74
- method = Column(String(64), nullable=False, default="heuristic-trend")
75
- created_at = Column(DateTime, default=_now)
76
-
77
-
78
- class ModelVersion(Base):
79
- __tablename__ = "model_versions"
80
- id = Column(Integer, primary_key=True, index=True)
81
- model_name = Column(String(64), nullable=False)
82
- model_path = Column(String(512), nullable=False)
83
- version = Column(String(32), nullable=False, default="v1")
84
- metrics_json = Column(JSON, nullable=True)
85
- created_at = Column(DateTime, default=_now)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
banana_ripeness_stages/app/performance_data.json DELETED
@@ -1,411 +0,0 @@
1
- {
2
- "ripeness": {
3
- "model": "BananaMultimodalNet (EfficientNet-B0 x2 + Matrix MLP, 5-class)",
4
- "inputs": [
5
- "RGB image",
6
- "Thermal image",
7
- "Thermal matrix XLSX"
8
- ],
9
- "test_samples": 469,
10
- "best_epoch": 8,
11
- "test": {
12
- "loss": 0.5037,
13
- "accuracy": 88.7,
14
- "macro_precision": 0.8418,
15
- "macro_recall": 0.859,
16
- "macro_f1": 0.8403,
17
- "weighted_f1": 0.8888
18
- },
19
- "per_class": [
20
- {
21
- "stage": "Stage_1_Mature_Green",
22
- "precision": 0.4783,
23
- "recall": 0.7333,
24
- "f1": 0.5789,
25
- "support": 30
26
- },
27
- {
28
- "stage": "Stage_2_Green_Yellow_Transition",
29
- "precision": 0.9365,
30
- "recall": 0.6705,
31
- "f1": 0.7815,
32
- "support": 88
33
- },
34
- {
35
- "stage": "Stage_3_Early_Ripening",
36
- "precision": 0.9255,
37
- "recall": 0.949,
38
- "f1": 0.9371,
39
- "support": 157
40
- },
41
- {
42
- "stage": "Stage_4_Fully_Yellow",
43
- "precision": 0.9353,
44
- "recall": 0.942,
45
- "f1": 0.9386,
46
- "support": 138
47
- },
48
- {
49
- "stage": "Stage_5_Yellow_Spotted",
50
- "precision": 0.9333,
51
- "recall": 1.0,
52
- "f1": 0.9655,
53
- "support": 56
54
- }
55
- ],
56
- "confusion_matrix": {
57
- "labels": [
58
- "Stage_1",
59
- "Stage_2",
60
- "Stage_3",
61
- "Stage_4",
62
- "Stage_5"
63
- ],
64
- "matrix": [
65
- [
66
- 22,
67
- 0,
68
- 8,
69
- 0,
70
- 0
71
- ],
72
- [
73
- 22,
74
- 59,
75
- 3,
76
- 3,
77
- 1
78
- ],
79
- [
80
- 2,
81
- 0,
82
- 149,
83
- 6,
84
- 0
85
- ],
86
- [
87
- 0,
88
- 4,
89
- 1,
90
- 130,
91
- 3
92
- ],
93
- [
94
- 0,
95
- 0,
96
- 0,
97
- 0,
98
- 56
99
- ]
100
- ]
101
- },
102
- "curve": [
103
- {
104
- "epoch": 1,
105
- "train_loss": 0.8004,
106
- "train_acc": 75.45,
107
- "val_loss": 0.4535,
108
- "val_acc": 91.37,
109
- "val_f1": 0.9118
110
- },
111
- {
112
- "epoch": 2,
113
- "train_loss": 0.6077,
114
- "train_acc": 85.5,
115
- "val_loss": 0.4677,
116
- "val_acc": 90.04,
117
- "val_f1": 0.8945
118
- },
119
- {
120
- "epoch": 3,
121
- "train_loss": 0.5552,
122
- "train_acc": 86.8,
123
- "val_loss": 0.4813,
124
- "val_acc": 87.61,
125
- "val_f1": 0.8669
126
- },
127
- {
128
- "epoch": 4,
129
- "train_loss": 0.5199,
130
- "train_acc": 89.49,
131
- "val_loss": 0.4494,
132
- "val_acc": 90.49,
133
- "val_f1": 0.9002
134
- },
135
- {
136
- "epoch": 5,
137
- "train_loss": 0.4784,
138
- "train_acc": 91.12,
139
- "val_loss": 0.5518,
140
- "val_acc": 91.59,
141
- "val_f1": 0.9031
142
- },
143
- {
144
- "epoch": 6,
145
- "train_loss": 0.4324,
146
- "train_acc": 93.03,
147
- "val_loss": 0.426,
148
- "val_acc": 91.37,
149
- "val_f1": 0.9046
150
- },
151
- {
152
- "epoch": 7,
153
- "train_loss": 0.4169,
154
- "train_acc": 93.21,
155
- "val_loss": 0.4543,
156
- "val_acc": 91.15,
157
- "val_f1": 0.9001
158
- },
159
- {
160
- "epoch": 8,
161
- "train_loss": 0.426,
162
- "train_acc": 93.4,
163
- "val_loss": 0.4348,
164
- "val_acc": 93.14,
165
- "val_f1": 0.9232
166
- },
167
- {
168
- "epoch": 9,
169
- "train_loss": 0.3848,
170
- "train_acc": 94.98,
171
- "val_loss": 0.4358,
172
- "val_acc": 92.04,
173
- "val_f1": 0.9106
174
- },
175
- {
176
- "epoch": 10,
177
- "train_loss": 0.396,
178
- "train_acc": 94.7,
179
- "val_loss": 0.4505,
180
- "val_acc": 92.04,
181
- "val_f1": 0.911
182
- },
183
- {
184
- "epoch": 11,
185
- "train_loss": 0.3934,
186
- "train_acc": 94.42,
187
- "val_loss": 0.8528,
188
- "val_acc": 91.59,
189
- "val_f1": 0.9051
190
- },
191
- {
192
- "epoch": 12,
193
- "train_loss": 0.3645,
194
- "train_acc": 95.35,
195
- "val_loss": 0.4522,
196
- "val_acc": 91.81,
197
- "val_f1": 0.9082
198
- },
199
- {
200
- "epoch": 13,
201
- "train_loss": 0.3586,
202
- "train_acc": 95.4,
203
- "val_loss": 0.4484,
204
- "val_acc": 91.59,
205
- "val_f1": 0.9088
206
- },
207
- {
208
- "epoch": 14,
209
- "train_loss": 0.3798,
210
- "train_acc": 95.21,
211
- "val_loss": 0.4693,
212
- "val_acc": 90.93,
213
- "val_f1": 0.9024
214
- },
215
- {
216
- "epoch": 15,
217
- "train_loss": 0.3525,
218
- "train_acc": 96.14,
219
- "val_loss": 0.4594,
220
- "val_acc": 91.59,
221
- "val_f1": 0.9079
222
- }
223
- ],
224
- "images": {
225
- "confusion_matrix": "/images/performance/ripeness_cm.png",
226
- "curves": "/images/performance/ripeness_curves.png"
227
- }
228
- },
229
- "adulteration": {
230
- "model": "BananaAdulterationNet (EfficientNet-B0, frozen backbone, 2-class)",
231
- "inputs": [
232
- "RGB image"
233
- ],
234
- "classes": {
235
- "0": "Adulterated",
236
- "1": "Natural"
237
- },
238
- "test_samples": 450,
239
- "best_epoch": 14,
240
- "test": {
241
- "loss": 0.3087,
242
- "accuracy": 89.11,
243
- "macro_precision": 0.8913,
244
- "macro_recall": 0.8911,
245
- "macro_f1": 0.8911,
246
- "weighted_f1": 0.8911,
247
- "roc_auc": 0.9533,
248
- "pr_auc": 0.9549,
249
- "sensitivity": 0.9022,
250
- "specificity": 0.88
251
- },
252
- "per_class": [
253
- {
254
- "stage": "Adulterated",
255
- "precision": 0.8826,
256
- "recall": 0.9022,
257
- "f1": 0.8923,
258
- "support": 225
259
- },
260
- {
261
- "stage": "Natural",
262
- "precision": 0.9,
263
- "recall": 0.88,
264
- "f1": 0.8899,
265
- "support": 225
266
- }
267
- ],
268
- "confusion_matrix": {
269
- "labels": [
270
- "Adulterated",
271
- "Natural"
272
- ],
273
- "matrix": [
274
- [
275
- 203,
276
- 22
277
- ],
278
- [
279
- 27,
280
- 198
281
- ]
282
- ]
283
- },
284
- "curve": [
285
- {
286
- "epoch": 1,
287
- "train_loss": 0.5971,
288
- "train_acc": 71.33,
289
- "val_loss": 0.5303,
290
- "val_acc": 81.78,
291
- "val_f1": 0.8173
292
- },
293
- {
294
- "epoch": 2,
295
- "train_loss": 0.4689,
296
- "train_acc": 81.29,
297
- "val_loss": 0.4607,
298
- "val_acc": 83.33,
299
- "val_f1": 0.8325
300
- },
301
- {
302
- "epoch": 3,
303
- "train_loss": 0.4181,
304
- "train_acc": 83.29,
305
- "val_loss": 0.4154,
306
- "val_acc": 85.56,
307
- "val_f1": 0.8555
308
- },
309
- {
310
- "epoch": 4,
311
- "train_loss": 0.3808,
312
- "train_acc": 84.76,
313
- "val_loss": 0.3882,
314
- "val_acc": 86.89,
315
- "val_f1": 0.8688
316
- },
317
- {
318
- "epoch": 5,
319
- "train_loss": 0.347,
320
- "train_acc": 85.38,
321
- "val_loss": 0.3799,
322
- "val_acc": 87.11,
323
- "val_f1": 0.8705
324
- },
325
- {
326
- "epoch": 6,
327
- "train_loss": 0.3411,
328
- "train_acc": 86.14,
329
- "val_loss": 0.355,
330
- "val_acc": 87.11,
331
- "val_f1": 0.8707
332
- },
333
- {
334
- "epoch": 7,
335
- "train_loss": 0.3275,
336
- "train_acc": 86.86,
337
- "val_loss": 0.3427,
338
- "val_acc": 88.44,
339
- "val_f1": 0.8839
340
- },
341
- {
342
- "epoch": 8,
343
- "train_loss": 0.3047,
344
- "train_acc": 87.33,
345
- "val_loss": 0.3214,
346
- "val_acc": 89.33,
347
- "val_f1": 0.8932
348
- },
349
- {
350
- "epoch": 9,
351
- "train_loss": 0.3099,
352
- "train_acc": 87.71,
353
- "val_loss": 0.307,
354
- "val_acc": 89.78,
355
- "val_f1": 0.8978
356
- },
357
- {
358
- "epoch": 10,
359
- "train_loss": 0.3086,
360
- "train_acc": 87.81,
361
- "val_loss": 0.2976,
362
- "val_acc": 90.0,
363
- "val_f1": 0.9
364
- },
365
- {
366
- "epoch": 11,
367
- "train_loss": 0.2959,
368
- "train_acc": 88.48,
369
- "val_loss": 0.3015,
370
- "val_acc": 89.56,
371
- "val_f1": 0.8953
372
- },
373
- {
374
- "epoch": 12,
375
- "train_loss": 0.2956,
376
- "train_acc": 87.19,
377
- "val_loss": 0.2871,
378
- "val_acc": 90.44,
379
- "val_f1": 0.9044
380
- },
381
- {
382
- "epoch": 13,
383
- "train_loss": 0.2848,
384
- "train_acc": 88.43,
385
- "val_loss": 0.2767,
386
- "val_acc": 90.44,
387
- "val_f1": 0.9044
388
- },
389
- {
390
- "epoch": 14,
391
- "train_loss": 0.2845,
392
- "train_acc": 88.62,
393
- "val_loss": 0.2774,
394
- "val_acc": 91.78,
395
- "val_f1": 0.9177
396
- },
397
- {
398
- "epoch": 15,
399
- "train_loss": 0.2838,
400
- "train_acc": 87.9,
401
- "val_loss": 0.2756,
402
- "val_acc": 90.89,
403
- "val_f1": 0.9088
404
- }
405
- ],
406
- "images": {
407
- "confusion_matrix": "/images/performance/adulteration_cm.png",
408
- "curves": "/images/performance/adulteration_curves.png"
409
- }
410
- }
411
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
banana_ripeness_stages/app/routers/auth.py DELETED
@@ -1,59 +0,0 @@
1
- from fastapi import APIRouter, Depends, HTTPException, status
2
- from sqlalchemy.orm import Session
3
-
4
- from .. import models, schemas
5
- from ..database import get_db
6
- from ..deps import get_current_user
7
- from ..utils.security import (create_access_token, hash_password,
8
- verify_password)
9
-
10
- router = APIRouter(prefix="/api/auth", tags=["auth"])
11
-
12
-
13
- def normalize_phone(raw: str) -> str:
14
- cleaned = "".join(ch for ch in raw.strip() if ch.isdigit() or ch == "+")
15
- if cleaned.startswith("+"):
16
- digits = cleaned[1:]
17
- prefix = "+"
18
- else:
19
- digits = cleaned
20
- prefix = ""
21
- if not digits.isdigit() or not 7 <= len(digits) <= 15:
22
- raise HTTPException(status_code=422,
23
- detail="Phone number must contain 7–15 digits.")
24
- return prefix + digits
25
-
26
-
27
- @router.post("/register", response_model=schemas.TokenOut)
28
- def register(payload: schemas.RegisterIn, db: Session = Depends(get_db)):
29
- exists = db.query(models.User).filter(
30
- models.User.email == payload.email.lower()).first()
31
- if exists:
32
- raise HTTPException(status_code=400, detail="Email already registered.")
33
- user = models.User(name=payload.name.strip(),
34
- email=payload.email.lower(),
35
- phone=normalize_phone(payload.phone),
36
- password_hash=hash_password(payload.password))
37
- db.add(user)
38
- db.commit()
39
- db.refresh(user)
40
- token = create_access_token(user.id)
41
- return {"access_token": token, "token_type": "bearer",
42
- "user": schemas.UserOut.model_validate(user)}
43
-
44
-
45
- @router.post("/login", response_model=schemas.TokenOut)
46
- def login(payload: schemas.LoginIn, db: Session = Depends(get_db)):
47
- user = db.query(models.User).filter(
48
- models.User.email == payload.email.lower()).first()
49
- if user is None or not verify_password(payload.password, user.password_hash):
50
- raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
51
- detail="Invalid email or password.")
52
- token = create_access_token(user.id)
53
- return {"access_token": token, "token_type": "bearer",
54
- "user": schemas.UserOut.model_validate(user)}
55
-
56
-
57
- @router.get("/me", response_model=schemas.UserOut)
58
- def me(user: models.User = Depends(get_current_user)):
59
- return user
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
banana_ripeness_stages/app/routers/explain.py DELETED
@@ -1,94 +0,0 @@
1
- from fastapi import APIRouter, Depends, Form, HTTPException, UploadFile
2
-
3
- from .. import models
4
- from ..deps import get_current_user
5
- from ..services import xai_service as xai
6
- from ..services.matrix_shap import explain_matrix_shap
7
- from ..utils.file_utils import save_image, save_matrix
8
-
9
- router = APIRouter(prefix="/api/explain", tags=["xai"])
10
-
11
-
12
- @router.post("/ripeness")
13
- async def explain_ripeness(rgb: UploadFile, thermal: UploadFile,
14
- matrix: UploadFile,
15
- user: models.User = Depends(get_current_user)):
16
- rgb_p = await save_image(rgb, "rgb")
17
- th_p = await save_image(thermal, "thermal")
18
- mx_p = await save_matrix(matrix)
19
- try:
20
- return xai.explain_ripeness(str(rgb_p), str(th_p), str(mx_p))
21
- except Exception as exc:
22
- raise HTTPException(status_code=500, detail=f"XAI error: {exc}")
23
-
24
-
25
- @router.post("/adulteration")
26
- async def explain_adulteration(rgb: UploadFile,
27
- user: models.User = Depends(get_current_user)):
28
- rgb_p = await save_image(rgb, "rgb")
29
- try:
30
- return xai.explain_adulteration(str(rgb_p))
31
- except Exception as exc:
32
- raise HTTPException(status_code=500, detail=f"XAI error: {exc}")
33
-
34
-
35
- @router.post("/ripeness/rgb-gradcam")
36
- async def explain_rgb(
37
- rgb: UploadFile, thermal: UploadFile, matrix: UploadFile,
38
- user: models.User = Depends(get_current_user),
39
- ):
40
- """Standalone RGB Grad-CAM triplet (combined + heatmap + overlay)."""
41
- rgb_p = await save_image(rgb, "rgb")
42
- th_p = await save_image(thermal, "thermal")
43
- mx_p = await save_matrix(matrix)
44
- try:
45
- return xai.explain_rgb_gradcam(str(rgb_p), str(th_p), str(mx_p))
46
- except Exception as exc:
47
- raise HTTPException(status_code=500, detail=f"XAI error: {exc}")
48
-
49
-
50
- @router.post("/ripeness/thermal-gradcam")
51
- async def explain_thermal(
52
- rgb: UploadFile, thermal: UploadFile, matrix: UploadFile,
53
- user: models.User = Depends(get_current_user),
54
- ):
55
- """Standalone Thermal Grad-CAM triplet (combined + heatmap + overlay)."""
56
- rgb_p = await save_image(rgb, "rgb")
57
- th_p = await save_image(thermal, "thermal")
58
- mx_p = await save_matrix(matrix)
59
- try:
60
- return xai.explain_thermal_gradcam(str(rgb_p), str(th_p), str(mx_p))
61
- except Exception as exc:
62
- raise HTTPException(status_code=500, detail=f"XAI error: {exc}")
63
-
64
-
65
- @router.post("/ripeness/matrix-shap")
66
- async def explain_matrix(
67
- rgb: UploadFile, thermal: UploadFile, matrix: UploadFile,
68
- target_class: int | None = Form(default=None),
69
- user: models.User = Depends(get_current_user),
70
- ):
71
- """REAL SHAP explanation for the thermal-matrix modality.
72
-
73
- Uses shap.GradientExplainer on the existing trained ripeness model with
74
- RGB/Thermal held fixed. No retraining, no architecture changes.
75
- """
76
- rgb_p = await save_image(rgb, "rgb")
77
- th_p = await save_image(thermal, "thermal")
78
- mx_p = await save_matrix(matrix)
79
- try:
80
- out = explain_matrix_shap(str(rgb_p), str(th_p), str(mx_p),
81
- target=target_class)
82
- except ValueError as exc:
83
- raise HTTPException(status_code=422, detail=str(exc))
84
- except Exception as exc:
85
- raise HTTPException(status_code=500, detail=f"XAI error: {exc}")
86
- return {
87
- "method": out["method"],
88
- "target_stage": out["target_stage"],
89
- "target_class_index": out["target_class_index"],
90
- "probability": out["probability"],
91
- "shap_values": out["shap_values"],
92
- "heatmap_url": out["heatmap_url"],
93
- "summary": out["summary"],
94
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
banana_ripeness_stages/app/routers/models.py DELETED
@@ -1,19 +0,0 @@
1
- import json
2
- from pathlib import Path
3
-
4
- from fastapi import APIRouter
5
-
6
- router = APIRouter(prefix="/api/models", tags=["models"])
7
-
8
- _DATA_PATH = Path(__file__).resolve().parent.parent / "performance_data.json"
9
-
10
-
11
- @router.get("/performance")
12
- def model_performance():
13
- """Verified experimental evaluation metrics + training curves.
14
-
15
- Numbers come from the training notebooks' held-out test evaluation
16
- (see performance_data.json). Not live accuracy guarantees.
17
- """
18
- with open(_DATA_PATH) as f:
19
- return json.load(f)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
banana_ripeness_stages/app/routers/prediction.py DELETED
@@ -1,210 +0,0 @@
1
- from datetime import datetime
2
-
3
- from fastapi import APIRouter, Depends, Form, HTTPException, UploadFile
4
- from sqlalchemy.orm import Session
5
-
6
- from .. import models, schemas
7
- from ..database import get_db
8
- from ..deps import get_current_user
9
- from ..services import inference_service as infer
10
- from ..services import xai_service as xai
11
- from ..services.forecast_service import build_forecast
12
- from ..services.twin_service import get_or_auto_create_twin
13
- from ..utils.file_utils import public_url, save_image, save_matrix
14
-
15
- router = APIRouter(prefix="/api/predict", tags=["predict"])
16
-
17
-
18
- def _record(db, user, twin, ptype, value, prob, conf, details):
19
- pred = models.Prediction(
20
- twin_id=twin.twin_id if twin else None,
21
- owner_user_id=user.id, prediction_type=ptype,
22
- prediction_value=value, probability=prob, confidence=conf,
23
- details_json=details)
24
- db.add(pred)
25
- db.commit()
26
- db.refresh(pred)
27
- return pred
28
-
29
-
30
- def _observe(db, twin, rgb=None, thermal=None, matrix=None,
31
- stage=None, ripe=None, a_status=None, a_prob=None, conf=None):
32
- if twin is None:
33
- return None
34
- obs = models.Observation(
35
- twin_id=twin.twin_id,
36
- rgb_file_path=str(rgb) if rgb else None,
37
- thermal_file_path=str(thermal) if thermal else None,
38
- matrix_file_path=str(matrix) if matrix else None,
39
- observation_time=datetime.utcnow(),
40
- predicted_stage=stage, ripeness_percentage=ripe,
41
- adulteration_status=a_status, adulteration_probability=a_prob,
42
- confidence=conf)
43
- db.add(obs)
44
- db.commit()
45
- db.refresh(obs)
46
- return obs
47
-
48
-
49
- def _touch_twin(db, twin, stage=None, ripe=None, a_status=None,
50
- a_prob=None, conf=None):
51
- if twin is None:
52
- return
53
- if stage: twin.current_stage = stage
54
- if ripe is not None: twin.ripeness_percentage = ripe
55
- if a_status: twin.adulteration_status = a_status
56
- if a_prob is not None: twin.adulteration_probability = a_prob
57
- if conf is not None: twin.confidence = conf
58
- twin.updated_at = datetime.utcnow()
59
- db.commit()
60
-
61
-
62
- @router.post("/ripeness")
63
- async def predict_ripeness(
64
- rgb: UploadFile, thermal: UploadFile, matrix: UploadFile,
65
- twin_id: str | None = Form(default=None),
66
- twin_name: str | None = Form(default=None),
67
- db: Session = Depends(get_db),
68
- user: models.User = Depends(get_current_user),
69
- ):
70
- rgb_p = await save_image(rgb, "rgb")
71
- th_p = await save_image(thermal, "thermal")
72
- mx_p = await save_matrix(matrix)
73
- try:
74
- result = infer.predict_ripeness(str(rgb_p), str(th_p), str(mx_p))
75
- except ValueError as exc:
76
- raise HTTPException(status_code=422, detail=str(exc))
77
- except RuntimeError as exc:
78
- raise HTTPException(status_code=500, detail=str(exc))
79
- twin, twin_created = get_or_auto_create_twin(db, user, twin_id, twin_name)
80
- pred = _record(db, user, twin, "ripeness", result["predicted_stage"],
81
- result["confidence"], result["confidence"],
82
- {**result, "rgb": public_url(rgb_p),
83
- "thermal": public_url(th_p)})
84
- _observe(db, twin, rgb_p, th_p, mx_p, result["predicted_stage"],
85
- result["ripeness_percentage"], conf=result["confidence"])
86
- _touch_twin(db, twin, result["predicted_stage"],
87
- result["ripeness_percentage"], conf=result["confidence"])
88
- return {"prediction_id": pred.id, **result,
89
- "rgb_url": public_url(rgb_p), "thermal_url": public_url(th_p),
90
- "twin_id": twin.twin_id, "twin_name": twin.name,
91
- "twin_created": twin_created}
92
-
93
-
94
- @router.post("/adulteration")
95
- async def predict_adulteration(
96
- rgb: UploadFile,
97
- twin_id: str | None = Form(default=None),
98
- twin_name: str | None = Form(default=None),
99
- db: Session = Depends(get_db),
100
- user: models.User = Depends(get_current_user),
101
- ):
102
- rgb_p = await save_image(rgb, "rgb")
103
- try:
104
- result = infer.predict_adulteration(str(rgb_p))
105
- except ValueError as exc:
106
- raise HTTPException(status_code=422, detail=str(exc))
107
- except RuntimeError as exc:
108
- raise HTTPException(status_code=500, detail=str(exc))
109
- twin, twin_created = get_or_auto_create_twin(db, user, twin_id, twin_name)
110
- pred = _record(db, user, twin, "adulteration", result["status"],
111
- result["confidence"], result["confidence"],
112
- {**result, "rgb": public_url(rgb_p)})
113
- _observe(db, twin, rgb_p, a_status=result["status"],
114
- a_prob=result["adulterated_probability"],
115
- conf=result["confidence"])
116
- _touch_twin(db, twin, a_status=result["status"],
117
- a_prob=result["adulterated_probability"],
118
- conf=result["confidence"])
119
- return {"prediction_id": pred.id, **result,
120
- "rgb_url": public_url(rgb_p),
121
- "twin_id": twin.twin_id, "twin_name": twin.name,
122
- "twin_created": twin_created}
123
-
124
-
125
- @router.post("/combined")
126
- async def predict_combined(
127
- rgb: UploadFile, thermal: UploadFile, matrix: UploadFile,
128
- twin_id: str | None = Form(default=None),
129
- twin_name: str | None = Form(default=None),
130
- explain: bool = Form(default=True),
131
- db: Session = Depends(get_db),
132
- user: models.User = Depends(get_current_user),
133
- ):
134
- rgb_p = await save_image(rgb, "rgb")
135
- th_p = await save_image(thermal, "thermal")
136
- mx_p = await save_matrix(matrix)
137
- try:
138
- ripe = infer.predict_ripeness(str(rgb_p), str(th_p), str(mx_p))
139
- except ValueError as exc:
140
- raise HTTPException(status_code=422, detail=str(exc))
141
- except RuntimeError as exc:
142
- raise HTTPException(status_code=500, detail=str(exc))
143
- try:
144
- adult = infer.predict_adulteration(str(rgb_p))
145
- except RuntimeError as exc:
146
- raise HTTPException(status_code=500, detail=str(exc))
147
- twin, twin_created = get_or_auto_create_twin(db, user, twin_id, twin_name)
148
- details = {"ripeness": ripe, "adulteration": adult,
149
- "rgb": public_url(rgb_p), "thermal": public_url(th_p)}
150
- explanations = None
151
- if explain:
152
- try:
153
- explanations = {
154
- **xai.explain_ripeness(str(rgb_p), str(th_p), str(mx_p)),
155
- **{"adulteration_" + k: v for k, v in
156
- xai.explain_adulteration(str(rgb_p)).items()},
157
- }
158
- details["explanations"] = explanations
159
- except Exception as exc: # XAI must never break analysis
160
- details["explanations_error"] = str(exc)
161
- pred = _record(db, user, twin, "combined",
162
- f"{ripe['predicted_stage']} / {adult['status']}",
163
- adult["confidence"], ripe["confidence"], details)
164
- _observe(db, twin, rgb_p, th_p, mx_p, ripe["predicted_stage"],
165
- ripe["ripeness_percentage"], adult["status"],
166
- adult["adulterated_probability"], ripe["confidence"])
167
- _touch_twin(db, twin, ripe["predicted_stage"],
168
- ripe["ripeness_percentage"], adult["status"],
169
- adult["adulterated_probability"], ripe["confidence"])
170
- forecast = None
171
- obs = db.query(models.Observation).filter(
172
- models.Observation.twin_id == twin.twin_id).order_by(
173
- models.Observation.observation_time).all()
174
- try:
175
- forecast = build_forecast(ripe["predicted_stage"], obs)
176
- except Exception:
177
- forecast = None
178
- return {"prediction_id": pred.id, "ripeness": ripe,
179
- "adulteration": adult, "explanations": explanations,
180
- "forecast": forecast,
181
- "rgb_url": public_url(rgb_p), "thermal_url": public_url(th_p),
182
- "twin_id": twin.twin_id, "twin_name": twin.name,
183
- "twin_created": twin_created}
184
-
185
-
186
- @router.get("", response_model=list[schemas.PredictionOut])
187
- def list_my_predictions(user: models.User = Depends(get_current_user),
188
- db: Session = Depends(get_db)):
189
- """All predictions of the current user, newest first."""
190
- return db.query(models.Prediction).filter(
191
- models.Prediction.owner_user_id == user.id).order_by(
192
- models.Prediction.created_at.desc()).limit(200).all()
193
-
194
-
195
- @router.get("/ripeness/{prediction_id}")
196
- @router.get("/adulteration/{prediction_id}")
197
- @router.get("/combined/{prediction_id}")
198
- async def get_prediction(prediction_id: int,
199
- db: Session = Depends(get_db),
200
- user: models.User = Depends(get_current_user)):
201
- pred = db.query(models.Prediction).filter(
202
- models.Prediction.id == prediction_id,
203
- models.Prediction.owner_user_id == user.id).first()
204
- if pred is None:
205
- raise HTTPException(status_code=404, detail="Prediction not found.")
206
- return {"id": pred.id, "twin_id": pred.twin_id,
207
- "prediction_type": pred.prediction_type,
208
- "prediction_value": pred.prediction_value,
209
- "probability": pred.probability, "confidence": pred.confidence,
210
- "details": pred.details_json, "created_at": pred.created_at}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
banana_ripeness_stages/app/routers/profile.py DELETED
@@ -1,56 +0,0 @@
1
- from fastapi import APIRouter, Depends, HTTPException, status
2
- from sqlalchemy.orm import Session
3
-
4
- from .. import models, schemas
5
- from ..database import get_db
6
- from ..deps import get_current_user
7
- from ..utils.security import hash_password, verify_password
8
- from .auth import normalize_phone
9
-
10
- router = APIRouter(prefix="/api/auth", tags=["profile"])
11
-
12
-
13
- @router.put("/profile", response_model=schemas.UserOut)
14
- def update_profile(payload: schemas.ProfileUpdate,
15
- user: models.User = Depends(get_current_user),
16
- db: Session = Depends(get_db)):
17
- db_user = db.query(models.User).filter(models.User.id == user.id).first()
18
- if payload.name:
19
- db_user.name = payload.name.strip()
20
- if payload.phone:
21
- db_user.phone = normalize_phone(payload.phone)
22
- db.commit()
23
- db.refresh(db_user)
24
- return db_user
25
-
26
-
27
- @router.put("/password")
28
- def change_password(payload: schemas.PasswordChange,
29
- user: models.User = Depends(get_current_user),
30
- db: Session = Depends(get_db)):
31
- db_user = db.query(models.User).filter(models.User.id == user.id).first()
32
- if not verify_password(payload.current_password, db_user.password_hash):
33
- raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
34
- detail="Current password is incorrect.")
35
- if payload.current_password == payload.new_password:
36
- raise HTTPException(status_code=400,
37
- detail="New password must differ from the current one.")
38
- db_user.password_hash = hash_password(payload.new_password)
39
- db.commit()
40
- return {"updated": True}
41
-
42
-
43
- @router.get("/stats")
44
- def my_stats(user: models.User = Depends(get_current_user),
45
- db: Session = Depends(get_db)):
46
- twins = db.query(models.BananaTwin).filter(
47
- models.BananaTwin.owner_user_id == user.id).all()
48
- twin_ids = [t.twin_id for t in twins]
49
- n_obs = db.query(models.Observation).filter(
50
- models.Observation.twin_id.in_(twin_ids)).count() if twin_ids else 0
51
- n_pred = db.query(models.Prediction).filter(
52
- models.Prediction.owner_user_id == user.id).count()
53
- return {"twins": len(twins), "observations": n_obs,
54
- "analyses": n_pred + n_obs, "predictions": n_pred,
55
- "name": user.name, "email": user.email, "phone": user.phone,
56
- "created_at": user.created_at}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
banana_ripeness_stages/app/routers/twins.py DELETED
@@ -1,156 +0,0 @@
1
- from datetime import datetime
2
-
3
- from fastapi import APIRouter, Depends, HTTPException, UploadFile
4
- from sqlalchemy.orm import Session
5
-
6
- from .. import models, schemas
7
- from ..database import get_db
8
- from ..deps import get_current_user
9
- from ..services import xai_service as xai
10
- from ..services.forecast_service import build_forecast
11
- from ..services.twin_service import next_twin_id
12
- from ..utils.file_utils import save_image, save_matrix
13
-
14
- router = APIRouter(prefix="/api/twins", tags=["twins"])
15
-
16
-
17
- def _require_twin(db: Session, user_id: int, twin_id: str) -> models.BananaTwin:
18
- twin = db.query(models.BananaTwin).filter(
19
- models.BananaTwin.twin_id == twin_id,
20
- models.BananaTwin.owner_user_id == user_id).first()
21
- if twin is None:
22
- raise HTTPException(status_code=404, detail="Twin not found.")
23
- return twin
24
-
25
-
26
- @router.post("", response_model=schemas.TwinOut)
27
- def create_twin(payload: schemas.TwinCreate,
28
- user: models.User = Depends(get_current_user),
29
- db: Session = Depends(get_db)):
30
- twin = models.BananaTwin(twin_id=next_twin_id(db),
31
- owner_user_id=user.id,
32
- name=payload.name.strip() or "My Banana")
33
- db.add(twin)
34
- db.commit()
35
- db.refresh(twin)
36
- return twin
37
-
38
-
39
- @router.get("", response_model=list[schemas.TwinOut])
40
- def list_twins(user: models.User = Depends(get_current_user),
41
- db: Session = Depends(get_db)):
42
- return db.query(models.BananaTwin).filter(
43
- models.BananaTwin.owner_user_id == user.id).order_by(
44
- models.BananaTwin.created_at.desc()).all()
45
-
46
-
47
- @router.get("/{twin_id}", response_model=schemas.TwinOut)
48
- def get_twin(twin_id: str, user: models.User = Depends(get_current_user),
49
- db: Session = Depends(get_db)):
50
- return _require_twin(db, user.id, twin_id)
51
-
52
-
53
- @router.put("/{twin_id}", response_model=schemas.TwinOut)
54
- def update_twin(twin_id: str, payload: schemas.TwinUpdate,
55
- user: models.User = Depends(get_current_user),
56
- db: Session = Depends(get_db)):
57
- twin = _require_twin(db, user.id, twin_id)
58
- if payload.name:
59
- twin.name = payload.name.strip()
60
- twin.updated_at = datetime.utcnow()
61
- db.commit()
62
- db.refresh(twin)
63
- return twin
64
-
65
-
66
- @router.delete("/{twin_id}")
67
- def delete_twin(twin_id: str, user: models.User = Depends(get_current_user),
68
- db: Session = Depends(get_db)):
69
- twin = _require_twin(db, user.id, twin_id)
70
- db.delete(twin)
71
- db.commit()
72
- return {"deleted": twin_id}
73
-
74
-
75
- @router.get("/{twin_id}/history", response_model=list[schemas.ObservationOut])
76
- def twin_history(twin_id: str, user: models.User = Depends(get_current_user),
77
- db: Session = Depends(get_db)):
78
- _require_twin(db, user.id, twin_id)
79
- return db.query(models.Observation).filter(
80
- models.Observation.twin_id == twin_id).order_by(
81
- models.Observation.observation_time).all()
82
-
83
-
84
- @router.get("/{twin_id}/predictions", response_model=list[schemas.PredictionOut])
85
- def twin_predictions(twin_id: str,
86
- user: models.User = Depends(get_current_user),
87
- db: Session = Depends(get_db)):
88
- _require_twin(db, user.id, twin_id)
89
- return db.query(models.Prediction).filter(
90
- models.Prediction.twin_id == twin_id,
91
- models.Prediction.owner_user_id == user.id).order_by(
92
- models.Prediction.created_at.desc()).all()
93
-
94
-
95
- @router.get("/{twin_id}/forecast")
96
- def get_forecast(twin_id: str, user: models.User = Depends(get_current_user),
97
- db: Session = Depends(get_db)):
98
- twin = _require_twin(db, user.id, twin_id)
99
- obs = db.query(models.Observation).filter(
100
- models.Observation.twin_id == twin_id).order_by(
101
- models.Observation.observation_time).all()
102
- current = twin.current_stage or (obs[-1].predicted_stage if obs else None)
103
- if not current:
104
- raise HTTPException(status_code=422,
105
- detail="No observations yet — run an analysis first.")
106
- result = build_forecast(current, obs)
107
- fc = models.Forecast(twin_id=twin_id, current_stage=result["current_stage"],
108
- future_stage=result["future_stage"],
109
- probability=result["probability"],
110
- estimated_time=result["estimated_time"],
111
- method=result["method"])
112
- db.add(fc)
113
- db.commit()
114
- db.refresh(fc)
115
- return {"forecast": result, "history_length": len(obs), "id": fc.id,
116
- "disclaimer": "Future-stage forecast from observation-history "
117
- "heuristics, not an independently trained temporal model."}
118
-
119
-
120
- @router.post("/{twin_id}/forecast")
121
- def post_forecast(twin_id: str, user: models.User = Depends(get_current_user),
122
- db: Session = Depends(get_db)):
123
- return get_forecast(twin_id, user, db)
124
-
125
-
126
- @router.post("/{twin_id}/explain")
127
- async def explain_twin(twin_id: str,
128
- rgb: UploadFile | None = None,
129
- thermal: UploadFile | None = None,
130
- matrix: UploadFile | None = None,
131
- user: models.User = Depends(get_current_user),
132
- db: Session = Depends(get_db)):
133
- twin = _require_twin(db, user.id, twin_id)
134
- obs = db.query(models.Observation).filter(
135
- models.Observation.twin_id == twin_id).order_by(
136
- models.Observation.id.desc()).first()
137
- rgb_p = str(await save_image(rgb, "rgb")) if rgb else (
138
- obs.rgb_file_path if obs else None)
139
- th_p = str(await save_image(thermal, "thermal")) if thermal else (
140
- obs.thermal_file_path if obs else None)
141
- mx_p = str(await save_matrix(matrix)) if matrix else (
142
- obs.matrix_file_path if obs else None)
143
- out: dict = {"twin_id": twin.twin_id}
144
- try:
145
- if rgb_p:
146
- out["adulteration"] = xai.explain_adulteration(rgb_p)
147
- if rgb_p and th_p and mx_p:
148
- out["ripeness"] = xai.explain_ripeness(rgb_p, th_p, mx_p)
149
- if len(out) == 1:
150
- raise HTTPException(status_code=422,
151
- detail="No source files available for explanation.")
152
- except HTTPException:
153
- raise
154
- except Exception as exc:
155
- raise HTTPException(status_code=500, detail=f"XAI error: {exc}")
156
- return out
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
banana_ripeness_stages/app/schemas.py DELETED
@@ -1,116 +0,0 @@
1
- from datetime import datetime
2
- from typing import Any, Dict, List, Optional
3
-
4
- from pydantic import BaseModel, EmailStr, Field
5
-
6
-
7
- # ---------- Auth ----------
8
- class RegisterIn(BaseModel):
9
- name: str = Field(min_length=1, max_length=120)
10
- email: EmailStr
11
- phone: str = Field(min_length=7, max_length=20)
12
- password: str = Field(min_length=6, max_length=128)
13
-
14
-
15
- class LoginIn(BaseModel):
16
- email: EmailStr
17
- password: str
18
-
19
-
20
- class ProfileUpdate(BaseModel):
21
- name: Optional[str] = Field(default=None, min_length=1, max_length=120)
22
- phone: Optional[str] = Field(default=None, min_length=7, max_length=20)
23
-
24
-
25
- class PasswordChange(BaseModel):
26
- current_password: str = Field(min_length=1, max_length=128)
27
- new_password: str = Field(min_length=6, max_length=128)
28
-
29
-
30
- class UserOut(BaseModel):
31
- id: int
32
- name: str
33
- email: str
34
- phone: Optional[str] = None
35
- created_at: Optional[datetime] = None
36
-
37
- class Config:
38
- from_attributes = True
39
-
40
-
41
- class TokenOut(BaseModel):
42
- access_token: str
43
- token_type: str = "bearer"
44
- user: UserOut
45
-
46
-
47
- # ---------- Twins ----------
48
- class TwinCreate(BaseModel):
49
- name: str = Field(default="My Banana", max_length=120)
50
-
51
-
52
- class TwinUpdate(BaseModel):
53
- name: Optional[str] = Field(default=None, max_length=120)
54
-
55
-
56
- class TwinOut(BaseModel):
57
- id: int
58
- twin_id: str
59
- name: str
60
- current_stage: Optional[str] = None
61
- ripeness_percentage: Optional[float] = None
62
- adulteration_status: Optional[str] = None
63
- adulteration_probability: Optional[float] = None
64
- confidence: Optional[float] = None
65
- created_at: Optional[datetime] = None
66
- updated_at: Optional[datetime] = None
67
-
68
- class Config:
69
- from_attributes = True
70
-
71
-
72
- # ---------- Generic ----------
73
- class ObservationOut(BaseModel):
74
- id: int
75
- twin_id: str
76
- rgb_file_path: Optional[str] = None
77
- thermal_file_path: Optional[str] = None
78
- matrix_file_path: Optional[str] = None
79
- observation_time: Optional[datetime] = None
80
- predicted_stage: Optional[str] = None
81
- ripeness_percentage: Optional[float] = None
82
- adulteration_status: Optional[str] = None
83
- adulteration_probability: Optional[float] = None
84
- confidence: Optional[float] = None
85
- created_at: Optional[datetime] = None
86
-
87
- class Config:
88
- from_attributes = True
89
-
90
-
91
- class PredictionOut(BaseModel):
92
- id: int
93
- twin_id: Optional[str] = None
94
- prediction_type: str
95
- prediction_value: str
96
- probability: Optional[float] = None
97
- confidence: Optional[float] = None
98
- details_json: Optional[Dict[str, Any]] = None
99
- created_at: Optional[datetime] = None
100
-
101
- class Config:
102
- from_attributes = True
103
-
104
-
105
- class ForecastOut(BaseModel):
106
- id: int
107
- twin_id: str
108
- current_stage: str
109
- future_stage: str
110
- probability: Optional[float] = None
111
- estimated_time: Optional[str] = None
112
- method: str
113
- created_at: Optional[datetime] = None
114
-
115
- class Config:
116
- from_attributes = True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
banana_ripeness_stages/app/services/forecast_service.py DELETED
@@ -1,71 +0,0 @@
1
- """Heuristic future-stage forecaster (NOT a neural network).
2
-
3
- Uses the twin's chronological observation history + existing 5-stage
4
- predictions. Modular: implement BaseForecaster to plug in a real model later.
5
- """
6
- from datetime import datetime
7
- from typing import List, Optional
8
-
9
- from ..config import RIPENESS_STAGES
10
-
11
- STAGE_TO_IDX = {s: i + 1 for i, s in enumerate(RIPENESS_STAGES)}
12
-
13
-
14
- class BaseForecaster:
15
- name = "base"
16
-
17
- def forecast(self, current_stage: str, history: List[dict]) -> dict:
18
- raise NotImplementedError
19
-
20
-
21
- class HeuristicTrendForecaster(BaseForecaster):
22
- """Rule-based: progression rate from first->last observed stage."""
23
- name = "heuristic-trend"
24
-
25
- def forecast(self, current_stage: str, history: List[dict]) -> dict:
26
- cur = STAGE_TO_IDX.get(current_stage, 3)
27
- stages = [h.get("predicted_stage") for h in history
28
- if h.get("predicted_stage") in STAGE_TO_IDX]
29
- if len(stages) >= 2:
30
- first, last = STAGE_TO_IDX[stages[0]], STAGE_TO_IDX[stages[-1]]
31
- n = len(stages)
32
- # avg step per observation
33
- step = (last - first) / max(n - 1, 1)
34
- step = max(0.0, min(step, 1.0))
35
- if step < 0.15:
36
- step = 0.5 # default drift when static
37
- nxt = min(5, cur + (1 if step >= 0.15 else 0))
38
- prob = round(min(0.92, max(0.45, 0.55 + 0.1 * step + 0.02 * n)), 4)
39
- elif len(stages) == 1:
40
- nxt = min(5, cur + 1)
41
- prob = 0.60
42
- else:
43
- nxt = min(5, cur + 1)
44
- prob = 0.50
45
- if cur >= 5:
46
- nxt, prob = 5, 0.95
47
- eta = "Terminal stage — monitor for spoilage."
48
- else:
49
- eta = "Approx. 2-4 days under ambient conditions (heuristic estimate)."
50
- return {
51
- "current_stage": RIPENESS_STAGES[cur - 1],
52
- "future_stage": RIPENESS_STAGES[nxt - 1],
53
- "probability": prob,
54
- "estimated_time": eta,
55
- "method": self.name,
56
- }
57
-
58
-
59
- def get_forecaster(name: str = "heuristic-trend") -> BaseForecaster:
60
- forecasters = {"heuristic-trend": HeuristicTrendForecaster()}
61
- if name not in forecasters:
62
- raise ValueError(f"Unknown forecaster '{name}'")
63
- return forecasters[name]
64
-
65
-
66
- def build_forecast(current_stage: str, observations) -> dict:
67
- history = [{
68
- "predicted_stage": o.predicted_stage,
69
- "observation_time": (o.observation_time or o.created_at or datetime.utcnow()).isoformat(),
70
- } for o in observations]
71
- return get_forecaster().forecast(current_stage, history)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
banana_ripeness_stages/app/services/inference_service.py DELETED
@@ -1,70 +0,0 @@
1
- """Ripeness inference service (MODEL 1 only)."""
2
- import torch
3
-
4
- from ..config import (ADULTERATION_MODEL_PATH, RIPENESS_MODEL_PATH,
5
- RIPENESS_STAGES, STAGE_RIPENESS_CENTERS)
6
- from ..ml.adulteration_model import get_adulteration_model
7
- from ..ml.ripeness_model import get_ripeness_model
8
- from ..ml.transforms import (load_thermal_matrix_tensor, matrix_summary_stats,
9
- preprocess_adulteration_image,
10
- preprocess_ripeness_image)
11
-
12
-
13
- def ripeness_percentage(probs: list[float]) -> float:
14
- return round(float(sum(p * c for p, c in zip(probs, STAGE_RIPENESS_CENTERS))), 2)
15
-
16
-
17
- @torch.no_grad()
18
- def predict_ripeness(rgb_path: str, thermal_path: str, matrix_path: str) -> dict:
19
- model = get_ripeness_model(str(RIPENESS_MODEL_PATH))
20
- try:
21
- rgb = preprocess_ripeness_image(rgb_path).unsqueeze(0)
22
- thermal = preprocess_ripeness_image(thermal_path).unsqueeze(0)
23
- matrix = load_thermal_matrix_tensor(matrix_path).unsqueeze(0)
24
- except (FileNotFoundError, ValueError) as exc:
25
- raise ValueError(str(exc)) from exc
26
- except Exception as exc:
27
- raise RuntimeError(f"Model inference error: {exc}") from exc
28
- try:
29
- logits = model(rgb, thermal, matrix)
30
- probs = torch.softmax(logits, dim=1)[0].tolist()
31
- except Exception as exc:
32
- raise RuntimeError(f"Model inference error: {exc}") from exc
33
- idx = int(max(range(len(probs)), key=lambda i: probs[i]))
34
- return {
35
- "predicted_stage": RIPENESS_STAGES[idx],
36
- "predicted_index": idx + 1,
37
- "confidence": round(float(probs[idx]), 4),
38
- "stage_probabilities": {s: round(float(p), 4)
39
- for s, p in zip(RIPENESS_STAGES, probs)},
40
- "ripeness_percentage": ripeness_percentage([float(p) for p in probs]),
41
- "matrix_stats": matrix_summary_stats(matrix_path),
42
- }
43
-
44
-
45
- @torch.no_grad()
46
- def predict_adulteration(rgb_path: str) -> dict:
47
- model = get_adulteration_model(str(ADULTERATION_MODEL_PATH))
48
- try:
49
- x = preprocess_adulteration_image(rgb_path).unsqueeze(0)
50
- except (FileNotFoundError, ValueError) as exc:
51
- raise ValueError(str(exc)) from exc
52
- except Exception as exc:
53
- raise RuntimeError(f"Model inference error: {exc}") from exc
54
- try:
55
- logits = model(x)
56
- probs = torch.softmax(logits, dim=1)[0].tolist()
57
- except Exception as exc:
58
- raise RuntimeError(f"Model inference error: {exc}") from exc
59
- # class 0 = Adulterated, 1 = Natural
60
- p_adult, p_nat = float(probs[0]), float(probs[1])
61
- status = "Adulterated" if p_adult >= p_nat else "Natural"
62
- conf = max(p_adult, p_nat)
63
- return {
64
- "status": status,
65
- "confidence": round(conf, 4),
66
- "natural_probability": round(p_nat, 4),
67
- "adulterated_probability": round(p_adult, 4),
68
- "disclaimer": ("AI-estimated likelihood, not chemical concentration "
69
- "and not laboratory certification."),
70
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
banana_ripeness_stages/app/services/matrix_shap.py DELETED
@@ -1,174 +0,0 @@
1
- """REAL SHAP explanation for the thermal-matrix modality.
2
-
3
- Uses the actual ``shap`` package (``shap.GradientExplainer``) on the existing,
4
- unmodified trained ripeness model. RGB and Thermal are held fixed while the
5
- matrix input varies, so the returned SHAP values estimate each of the 64x48
6
- matrix features' contribution to the selected ripeness-class logit.
7
-
8
- SHAP GradientExplainer is used to estimate the contribution of
9
- thermal-matrix features to the selected ripeness prediction.
10
- """
11
- import uuid
12
-
13
- import matplotlib
14
- matplotlib.use("Agg")
15
- import matplotlib.pyplot as plt
16
- from matplotlib.colors import TwoSlopeNorm
17
- import numpy as np
18
- import shap
19
- import torch
20
-
21
- from ..config import (EXPLAIN_DIR, RIPENESS_MODEL_PATH, RIPENESS_STAGES,
22
- SHAP_BACKGROUND_K, SHAP_BG_DIR, SHAP_BG_MAX, SHAP_NSAMPLES)
23
- from ..ml.ripeness_model import get_ripeness_model
24
- from ..ml.transforms import (load_thermal_matrix_tensor,
25
- preprocess_ripeness_image)
26
-
27
- METHOD = "SHAP GradientExplainer"
28
-
29
-
30
- class MatrixLogitWrapper(torch.nn.Module):
31
- """Holds RGB/Thermal fixed; exposes Matrix -> 5-class logits.
32
-
33
- Wraps the existing trained model WITHOUT altering it, so SHAP can
34
- explain the matrix modality alone.
35
- """
36
-
37
- def __init__(self, base: torch.nn.Module, rgb_fixed: torch.Tensor,
38
- thermal_fixed: torch.Tensor):
39
- super().__init__()
40
- self.base = base
41
- self.register_buffer("rgb_fixed", rgb_fixed)
42
- self.register_buffer("thermal_fixed", thermal_fixed)
43
-
44
- def forward(self, matrix: torch.Tensor) -> torch.Tensor:
45
- b = matrix.shape[0]
46
- rgb = self.rgb_fixed.expand(b, -1, -1, -1)
47
- thermal = self.thermal_fixed.expand(b, -1, -1, -1)
48
- return self.base(rgb, thermal, matrix)
49
-
50
-
51
- def _pool_files():
52
- files = sorted(SHAP_BG_DIR.glob("*.npy"),
53
- key=lambda p: p.stat().st_mtime)
54
- return files
55
-
56
-
57
- def _stash_to_pool(matrix_1x64x48: torch.Tensor) -> None:
58
- """Cache an uploaded matrix tensor for future background sets (FIFO cap)."""
59
- try:
60
- arr = matrix_1x64x48.detach().cpu().numpy().astype(np.float32)
61
- np.save(SHAP_BG_DIR / f"bg_{uuid.uuid4().hex[:8]}.npy", arr)
62
- files = _pool_files()
63
- while len(files) > SHAP_BG_MAX:
64
- files.pop(0).unlink(missing_ok=True)
65
- except Exception:
66
- pass # pooling must never break explanations
67
-
68
-
69
- def _build_background(current: torch.Tensor, k: int) -> torch.Tensor:
70
- """Small representative background set: cached deployment matrices,
71
- topped up with seeded noise-augmented copies of the current sample."""
72
- mats = []
73
- for f in _pool_files()[-max(k - 1, 0):]:
74
- try:
75
- a = np.load(f)
76
- if a.shape == (1, 64, 48):
77
- mats.append(torch.from_numpy(a))
78
- except Exception:
79
- continue
80
- rng = torch.Generator().manual_seed(0)
81
- while len(mats) < k:
82
- noise = torch.randn(current.shape, generator=rng) * 0.05
83
- mats.append((current + noise).clamp(-4.0, 4.0))
84
- bg = torch.cat(mats[:k], dim=0)
85
- return bg
86
-
87
-
88
- def _save_figure(matrix_img: np.ndarray, signed: np.ndarray,
89
- magnitude: np.ndarray, title: str) -> str:
90
- fig, ax = plt.subplots(1, 3, figsize=(10.5, 3.6))
91
- im0 = ax[0].imshow(matrix_img, cmap="Oranges", aspect="auto")
92
- ax[0].set_title("Model-seen matrix input (64x48)", fontsize=9)
93
- plt.colorbar(im0, ax=ax[0], fraction=0.046)
94
- vmax = float(np.abs(signed).max()) or 1.0
95
- im1 = ax[1].imshow(signed, cmap="RdBu_r", aspect="auto",
96
- norm=TwoSlopeNorm(vmin=-vmax, vcenter=0.0, vmax=vmax))
97
- ax[1].set_title("Signed SHAP values (red +, blue -)", fontsize=9)
98
- plt.colorbar(im1, ax=ax[1], fraction=0.046)
99
- im2 = ax[2].imshow(magnitude, cmap="hot", aspect="auto")
100
- ax[2].set_title("|SHAP| importance", fontsize=9)
101
- plt.colorbar(im2, ax=ax[2], fraction=0.046)
102
- for a in ax:
103
- a.set_xticks([]); a.set_yticks([])
104
- fig.suptitle(title, fontsize=10)
105
- fig.tight_layout()
106
- out = EXPLAIN_DIR / f"matrix_shap_{uuid.uuid4().hex[:8]}.png"
107
- fig.savefig(out, dpi=130, bbox_inches="tight")
108
- plt.close(fig)
109
- from ..config import files_url
110
- return files_url("explanations", out.name)
111
-
112
-
113
- def explain_matrix_shap(rgb_path: str, thermal_path: str, matrix_path: str,
114
- target: int | None = None,
115
- background_k: int = SHAP_BACKGROUND_K,
116
- nsamples: int = SHAP_NSAMPLES) -> dict:
117
- """Run REAL SHAP (shap.GradientExplainer) for the matrix modality."""
118
- model = get_ripeness_model(str(RIPENESS_MODEL_PATH))
119
- model.eval()
120
- rgb = preprocess_ripeness_image(rgb_path).unsqueeze(0)
121
- thermal = preprocess_ripeness_image(thermal_path).unsqueeze(0)
122
- matrix = load_thermal_matrix_tensor(matrix_path).unsqueeze(0)
123
-
124
- with torch.no_grad():
125
- logits = model(rgb, thermal, matrix)
126
- probs = torch.softmax(logits, dim=1)[0]
127
- pred = int(probs.argmax()) if target is None else int(target)
128
- if not 0 <= pred <= 4:
129
- raise ValueError(f"target_class must be 0-4, got {target}")
130
- probability = round(float(probs[pred]), 4)
131
-
132
- background_k = max(4, min(int(background_k), 16))
133
- nsamples = max(5, int(nsamples))
134
- bg = _build_background(matrix, background_k)
135
-
136
- wrapper = MatrixLogitWrapper(model, rgb, thermal)
137
- wrapper.eval()
138
- try:
139
- explainer = shap.GradientExplainer(wrapper, bg)
140
- raw = explainer.shap_values(matrix, nsamples=nsamples)
141
- finally:
142
- # GradientExplainer must not leave the shared model in train mode.
143
- model.eval()
144
- if isinstance(raw, list):
145
- arr = np.array(raw[pred])
146
- else:
147
- arr = np.array(raw)
148
- if arr.shape[-1] == 5 and arr.ndim == 5:
149
- arr = arr[..., pred]
150
- arr = np.array(arr, dtype=np.float64).reshape(-1, 64, 48)[0]
151
- if arr.shape != (64, 48):
152
- raise RuntimeError(f"Unexpected SHAP map shape {arr.shape}")
153
-
154
- magnitude = np.abs(arr)
155
- mean_abs = float(magnitude.mean())
156
- max_abs = float(magnitude.max())
157
- _stash_to_pool(matrix)
158
-
159
- title = (f"SHAP Explanation — {RIPENESS_STAGES[pred]} "
160
- f"(p={probability:.3f}, mean|SHAP|={mean_abs:.2e}, max={max_abs:.2e})")
161
- heatmap_url = _save_figure(matrix[0, 0].cpu().numpy(), arr, magnitude, title)
162
- return {
163
- "method": METHOD,
164
- "target_stage": RIPENESS_STAGES[pred],
165
- "target_class_index": pred,
166
- "probability": probability,
167
- "shap_values": np.round(arr, 6).tolist(),
168
- "heatmap_url": heatmap_url,
169
- # Backwards-compatible alias used by the combined-analysis UI.
170
- "matrix_importance_url": heatmap_url,
171
- "summary": {"mean_abs_shap": mean_abs, "max_abs_shap": max_abs},
172
- "background_size": int(bg.shape[0]),
173
- "nsamples": int(nsamples),
174
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
banana_ripeness_stages/app/services/twin_service.py DELETED
@@ -1,41 +0,0 @@
1
- from fastapi import HTTPException
2
- from sqlalchemy.orm import Session
3
-
4
- from .. import models
5
-
6
-
7
- def next_twin_id(db: Session) -> str:
8
- last = db.query(models.BananaTwin).order_by(
9
- models.BananaTwin.id.desc()).first()
10
- n = (last.id + 1) if last else 1
11
- while db.query(models.BananaTwin).filter(
12
- models.BananaTwin.twin_id == f"BT-{n:06d}").first():
13
- n += 1
14
- return f"BT-{n:06d}"
15
-
16
-
17
- def get_or_auto_create_twin(db: Session, user: models.User,
18
- twin_id: str | None,
19
- twin_name: str | None) -> tuple[models.BananaTwin, bool]:
20
- """Resolve the target Twin for an analysis.
21
-
22
- - Explicit twin_id: must belong to the user (404 otherwise). Never creates.
23
- - No twin_id: auto-creates a new Twin with a unique BT-XXXXXX id, using the
24
- optional banana name or a generated default. The analysis becomes its
25
- first observation.
26
- Returns (twin, created).
27
- """
28
- if twin_id:
29
- twin = db.query(models.BananaTwin).filter(
30
- models.BananaTwin.twin_id == twin_id,
31
- models.BananaTwin.owner_user_id == user.id).first()
32
- if twin is None:
33
- raise HTTPException(status_code=404, detail="Digital Twin not found.")
34
- return twin, False
35
- tid = next_twin_id(db)
36
- name = (twin_name or "").strip() or f"Banana {tid}"
37
- twin = models.BananaTwin(twin_id=tid, owner_user_id=user.id, name=name[:120])
38
- db.add(twin)
39
- db.commit()
40
- db.refresh(twin)
41
- return twin, True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
banana_ripeness_stages/app/services/xai_service.py DELETED
@@ -1,227 +0,0 @@
1
- """Explainability: Grad-CAM + REAL SHAP for the thermal matrix.
2
-
3
- - Ripeness RGB / Thermal -> Grad-CAM on each EfficientNet branch
4
- - Ripeness matrix -> REAL SHAP via shap.GradientExplainer (see matrix_shap.py)
5
- - Adulteration RGB -> Grad-CAM
6
- Artifacts saved under backend/storage/explanations/, URLs returned.
7
- """
8
- import io
9
- import uuid
10
- from pathlib import Path
11
-
12
- import matplotlib
13
- matplotlib.use("Agg")
14
- import matplotlib.pyplot as plt
15
- import numpy as np
16
- import torch
17
- from PIL import Image
18
-
19
- from ..config import (ADULTERATION_MODEL_PATH, EXPLAIN_DIR, RIPENESS_MODEL_PATH,
20
- files_url)
21
- from ..ml.adulteration_model import get_adulteration_model
22
- from ..ml.ripeness_model import get_ripeness_model
23
- from ..ml.transforms import (load_thermal_matrix_tensor,
24
- preprocess_adulteration_image,
25
- preprocess_ripeness_image)
26
- from .matrix_shap import explain_matrix_shap
27
-
28
-
29
- class _GradCAM:
30
- def __init__(self, model: torch.nn.Module, target_layer: torch.nn.Module):
31
- self.model = model
32
- self.activations, self.gradients = None, None
33
- target_layer.register_forward_hook(self._fwd)
34
- target_layer.register_full_backward_hook(self._bwd)
35
-
36
- def _fwd(self, module, inp, out):
37
- self.activations = out.detach()
38
-
39
- def _bwd(self, module, gin, gout):
40
- self.gradients = gout[0].detach()
41
-
42
- def __call__(self, score: torch.Tensor) -> np.ndarray:
43
- self.model.zero_grad(set_to_none=True)
44
- score.backward(retain_graph=True)
45
- acts, grads = self.activations[0], self.gradients[0] # [C,H,W]
46
- weights = grads.mean(dim=(1, 2), keepdim=True)
47
- cam = (weights * acts).sum(dim=0)
48
- cam = torch.relu(cam)
49
- cam = cam / (cam.max() + 1e-8)
50
- return cam.cpu().numpy()
51
-
52
-
53
- def _colored_heatmap(cam: np.ndarray) -> Image.Image:
54
- heat = Image.fromarray((cam * 255).astype(np.uint8)).resize((224, 224),
55
- Image.BILINEAR)
56
- cmap = plt.get_cmap("jet")
57
- colored = (cmap(np.asarray(heat) / 255.0)[:, :, :3] * 255).astype(np.uint8)
58
- return Image.fromarray(colored)
59
-
60
-
61
- def _overlay(original_path: str, cam: np.ndarray, out_path: Path,
62
- title: str) -> None:
63
- base = Image.open(original_path).convert("RGB").resize((224, 224))
64
- colored = _colored_heatmap(cam)
65
- blended = Image.blend(base, colored, alpha=0.45)
66
- fig, ax = plt.subplots(1, 3, figsize=(9, 3))
67
- for a, im, t in zip(ax, [base, colored, blended],
68
- ["Input", "Grad-CAM", "Overlay"]):
69
- a.imshow(im); a.set_title(t, fontsize=9); a.axis("off")
70
- fig.suptitle(title, fontsize=10)
71
- fig.tight_layout()
72
- fig.savefig(out_path, dpi=120, bbox_inches="tight")
73
- plt.close(fig)
74
-
75
-
76
- def _gradcam_triplet(original_path: str, cam: np.ndarray, base_name: str,
77
- title: str) -> dict:
78
- """Save combined + standalone heatmap + standalone overlay artifacts.
79
-
80
- Purely additive artifact generation; inference logic is untouched.
81
- """
82
- base = Image.open(original_path).convert("RGB").resize((224, 224))
83
- colored = _colored_heatmap(cam)
84
- blended = Image.blend(base, colored, alpha=0.45)
85
- urls = {}
86
- p_all = _save(base_name)
87
- _overlay(original_path, cam, p_all, title)
88
- urls["combined_url"] = files_url("explanations", p_all.name)
89
- p_heat = _save(base_name + "_heatmap")
90
- colored.save(p_heat)
91
- urls["heatmap_url"] = files_url("explanations", p_heat.name)
92
- p_over = _save(base_name + "_overlay")
93
- blended.save(p_over)
94
- urls["overlay_url"] = files_url("explanations", p_over.name)
95
- return urls
96
-
97
-
98
- def _save(png_name: str) -> Path:
99
- return EXPLAIN_DIR / f"{png_name}_{uuid.uuid4().hex[:8]}.png"
100
-
101
-
102
- def _last_conv(features: torch.nn.Sequential):
103
- # EfficientNet-B0: features[-1] is the final conv block
104
- return features[-1]
105
-
106
-
107
- @torch.no_grad()
108
- def _ripeness_tensors(rgb_path, thermal_path, matrix_path):
109
- return (preprocess_ripeness_image(rgb_path).unsqueeze(0),
110
- preprocess_ripeness_image(thermal_path).unsqueeze(0),
111
- load_thermal_matrix_tensor(matrix_path).unsqueeze(0))
112
-
113
-
114
- def explain_ripeness(rgb_path: str, thermal_path: str, matrix_path: str,
115
- target: int | None = None) -> dict:
116
- model = get_ripeness_model(str(RIPENESS_MODEL_PATH))
117
- model.eval()
118
- rgb, thermal, matrix = _ripeness_tensors(rgb_path, thermal_path, matrix_path)
119
- with torch.no_grad():
120
- logits = model(rgb, thermal, matrix)
121
- probs = torch.softmax(logits, dim=1)[0]
122
- pred = int(probs.argmax()) if target is None else int(target)
123
- score = model(rgb.requires_grad_(True),
124
- thermal.requires_grad_(True),
125
- matrix.requires_grad_(True)) if False else None
126
-
127
- outputs = {}
128
- # RGB Grad-CAM (same forward/backward pass as before).
129
- cam_rgb = _GradCAM(model, _last_conv(model.rgb_branch.backbone.features))
130
- rgb_r = preprocess_ripeness_image(rgb_path).unsqueeze(0).requires_grad_(True)
131
- th_r = preprocess_ripeness_image(thermal_path).unsqueeze(0)
132
- mx_r = load_thermal_matrix_tensor(matrix_path).unsqueeze(0)
133
- s = model(rgb_r, th_r, mx_r)[0, pred]
134
- cam = cam_rgb(s)
135
- trip = _gradcam_triplet(rgb_path, cam, "gradcam_ripeness_rgb",
136
- f"Ripeness RGB Grad-CAM (class {pred})")
137
- outputs["gradcam_rgb_url"] = trip["combined_url"]
138
- outputs["gradcam_rgb_heatmap_url"] = trip["heatmap_url"]
139
- outputs["gradcam_rgb_overlay_url"] = trip["overlay_url"]
140
-
141
- # Thermal Grad-CAM (same forward/backward pass as before).
142
- cam_th = _GradCAM(model, _last_conv(model.thermal_branch.backbone.features))
143
- rgb2 = preprocess_ripeness_image(rgb_path).unsqueeze(0)
144
- th2 = preprocess_ripeness_image(thermal_path).unsqueeze(0).requires_grad_(True)
145
- mx2 = load_thermal_matrix_tensor(matrix_path).unsqueeze(0)
146
- s2 = model(rgb2, th2, mx2)[0, pred]
147
- cam2 = cam_th(s2)
148
- trip2 = _gradcam_triplet(thermal_path, cam2, "gradcam_ripeness_thermal",
149
- f"Ripeness Thermal Grad-CAM (class {pred})")
150
- outputs["gradcam_thermal_url"] = trip2["combined_url"]
151
- outputs["gradcam_thermal_heatmap_url"] = trip2["heatmap_url"]
152
- outputs["gradcam_thermal_overlay_url"] = trip2["overlay_url"]
153
-
154
- # Thermal-matrix explanation: REAL SHAP (shap.GradientExplainer).
155
- shap_out = explain_matrix_shap(rgb_path, thermal_path, matrix_path,
156
- target=pred)
157
- outputs["matrix_importance_url"] = shap_out["heatmap_url"]
158
- outputs["matrix_shap"] = {
159
- "method": shap_out["method"],
160
- "target_stage": shap_out["target_stage"],
161
- "target_class_index": shap_out["target_class_index"],
162
- "probability": shap_out["probability"],
163
- "summary": shap_out["summary"],
164
- }
165
- outputs["method"] = ("Grad-CAM (RGB/Thermal) + SHAP GradientExplainer "
166
- "(thermal matrix).")
167
- outputs["target_class"] = pred
168
- return outputs
169
-
170
-
171
- def explain_rgb_gradcam(rgb_path: str, thermal_path: str, matrix_path: str,
172
- target: int | None = None) -> dict:
173
- """Standalone RGB Grad-CAM triplet for the ripeness model."""
174
- model = get_ripeness_model(str(RIPENESS_MODEL_PATH))
175
- model.eval()
176
- rgb, thermal, matrix = _ripeness_tensors(rgb_path, thermal_path, matrix_path)
177
- with torch.no_grad():
178
- probs = torch.softmax(model(rgb, thermal, matrix), dim=1)[0]
179
- pred = int(probs.argmax()) if target is None else int(target)
180
- cam_mod = _GradCAM(model, _last_conv(model.rgb_branch.backbone.features))
181
- rgb_r = preprocess_ripeness_image(rgb_path).unsqueeze(0).requires_grad_(True)
182
- s = model(rgb_r,
183
- preprocess_ripeness_image(thermal_path).unsqueeze(0),
184
- load_thermal_matrix_tensor(matrix_path).unsqueeze(0))[0, pred]
185
- cam = cam_mod(s)
186
- trip = _gradcam_triplet(rgb_path, cam, "gradcam_ripeness_rgb",
187
- f"Ripeness RGB Grad-CAM (class {pred})")
188
- return {"method": "Grad-CAM", "target_class": pred,
189
- "probability": round(float(probs[pred]), 4), **trip}
190
-
191
-
192
- def explain_thermal_gradcam(rgb_path: str, thermal_path: str, matrix_path: str,
193
- target: int | None = None) -> dict:
194
- """Standalone Thermal Grad-CAM triplet for the ripeness model."""
195
- model = get_ripeness_model(str(RIPENESS_MODEL_PATH))
196
- model.eval()
197
- rgb, thermal, matrix = _ripeness_tensors(rgb_path, thermal_path, matrix_path)
198
- with torch.no_grad():
199
- probs = torch.softmax(model(rgb, thermal, matrix), dim=1)[0]
200
- pred = int(probs.argmax()) if target is None else int(target)
201
- cam_mod = _GradCAM(model, _last_conv(model.thermal_branch.backbone.features))
202
- th_r = preprocess_ripeness_image(thermal_path).unsqueeze(0).requires_grad_(True)
203
- s = model(preprocess_ripeness_image(rgb_path).unsqueeze(0), th_r,
204
- load_thermal_matrix_tensor(matrix_path).unsqueeze(0))[0, pred]
205
- cam = cam_mod(s)
206
- trip = _gradcam_triplet(thermal_path, cam, "gradcam_ripeness_thermal",
207
- f"Ripeness Thermal Grad-CAM (class {pred})")
208
- return {"method": "Grad-CAM", "target_class": pred,
209
- "probability": round(float(probs[pred]), 4), **trip}
210
-
211
-
212
- def explain_adulteration(rgb_path: str, target: int | None = None) -> dict:
213
- model = get_adulteration_model(str(ADULTERATION_MODEL_PATH))
214
- model.eval()
215
- x0 = preprocess_adulteration_image(rgb_path).unsqueeze(0)
216
- with torch.no_grad():
217
- probs = torch.softmax(model(x0), dim=1)[0]
218
- pred = int(probs.argmax()) if target is None else int(target)
219
- cam_mod = _GradCAM(model, _last_conv(model.backbone.features))
220
- x = preprocess_adulteration_image(rgb_path).unsqueeze(0).requires_grad_(True)
221
- s = model(x)[0, pred]
222
- cam = cam_mod(s)
223
- p = _save("gradcam_adulteration")
224
- _overlay(rgb_path, cam, p, f"Adulteration RGB Grad-CAM (class {pred})")
225
- return {"gradcam_rgb_url": files_url("explanations", p.name),
226
- "method": "Grad-CAM on EfficientNet-B0 backbone.",
227
- "target_class": pred}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
banana_ripeness_stages/app/utils/file_utils.py DELETED
@@ -1,53 +0,0 @@
1
- import re
2
- import uuid
3
- from pathlib import Path
4
-
5
- from fastapi import HTTPException, UploadFile
6
-
7
- from ..config import ALLOWED_IMAGE_EXTS, ALLOWED_MATRIX_EXTS, MAX_UPLOAD_MB, UPLOAD_DIR
8
-
9
-
10
- def _safe_stem(name: str) -> str:
11
- stem = Path(name).stem
12
- stem = re.sub(r"[^A-Za-z0-9_-]+", "_", stem).strip("_")[:60] or "file"
13
- return stem
14
-
15
-
16
- async def save_upload(upload: UploadFile, kind: str, allowed: set) -> Path:
17
- ext = Path(upload.filename or "").suffix.lower()
18
- if ext not in allowed:
19
- raise HTTPException(status_code=400,
20
- detail=f"Invalid file type '{ext}'. Allowed: {sorted(allowed)}")
21
- data = await upload.read()
22
- size_mb = len(data) / (1024 * 1024)
23
- if size_mb > MAX_UPLOAD_MB:
24
- raise HTTPException(status_code=400,
25
- detail=f"File too large ({size_mb:.1f} MB). Max {MAX_UPLOAD_MB} MB.")
26
- if len(data) == 0:
27
- raise HTTPException(status_code=400, detail="Empty file uploaded.")
28
- dest = UPLOAD_DIR / f"{kind}_{uuid.uuid4().hex[:12]}_{_safe_stem(upload.filename or 'file')}{ext}"
29
- dest.write_bytes(data)
30
- return dest
31
-
32
-
33
- async def save_image(upload: UploadFile, kind: str) -> Path:
34
- return await save_upload(upload, kind, ALLOWED_IMAGE_EXTS)
35
-
36
-
37
- async def save_matrix(upload: UploadFile, kind: str = "matrix") -> Path:
38
- return await save_upload(upload, kind, ALLOWED_MATRIX_EXTS)
39
-
40
-
41
- def public_url(path: Path) -> str:
42
- """Convert a storage path to a /files/... URL served by FastAPI.
43
-
44
- Honours the BANANA_SUBPATH mount prefix via config.files_url so URLs
45
- work both standalone ('' -> '/files/...') and mounted
46
- ('/banana_ripeness_stages' -> '/banana_ripeness_stages/files/...').
47
- """
48
- from ..config import EXPLAIN_DIR, files_url
49
- try:
50
- rel = path.relative_to(EXPLAIN_DIR.parent) # storage/...
51
- except ValueError:
52
- return files_url(path.name)
53
- return files_url(rel.as_posix().replace("storage/", ""))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
banana_ripeness_stages/app/utils/security.py DELETED
@@ -1,29 +0,0 @@
1
- from datetime import datetime, timedelta
2
-
3
- import bcrypt
4
- from jose import JWTError, jwt
5
-
6
- from ..config import ACCESS_TOKEN_EXPIRE_MINUTES, ALGORITHM, SECRET_KEY
7
-
8
-
9
- def hash_password(password: str) -> str:
10
- return bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
11
-
12
-
13
- def verify_password(password: str, password_hash: str) -> bool:
14
- try:
15
- return bcrypt.checkpw(password.encode("utf-8"), password_hash.encode("utf-8"))
16
- except Exception:
17
- return False
18
-
19
-
20
- def create_access_token(user_id: int) -> str:
21
- expire = datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
22
- return jwt.encode({"sub": str(user_id), "exp": expire}, SECRET_KEY, algorithm=ALGORITHM)
23
-
24
-
25
- def decode_token(token: str) -> dict:
26
- try:
27
- return jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
28
- except JWTError as exc:
29
- raise ValueError("Invalid token") from exc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
banana_ripeness_stages/best_adulteration_model.pth DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:d1f65d2fb9f6750491476ffbdc2049e3181e5f157b46396dc676ca5b3bcb147c
3
- size 16369123
 
 
 
 
banana_ripeness_stages/requirements.txt DELETED
@@ -1,17 +0,0 @@
1
- fastapi==0.115.*
2
- uvicorn[standard]==0.30.*
3
- sqlalchemy==2.0.*
4
- python-jose[cryptography]==3.3.*
5
- bcrypt==5.*
6
- python-multipart==0.0.*
7
- pillow==11.*
8
- torch==2.5.*
9
- torchvision==0.20.*
10
- openpyxl==3.1.*
11
- pandas==2.2.*
12
- numpy==1.26.*
13
- matplotlib==3.8.*
14
- shap==0.49.*
15
- pydantic==2.*
16
- email-validator==2.*
17
- python-dotenv==1.*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
gateway.py CHANGED
@@ -1,32 +1,7 @@
1
- """Unified ASGI gateway: existing Flask app + banana backend mount.
2
 
3
- Additive integration only — app.py is imported untouched.
4
 
5
- - Existing Flask application keeps serving: /, /health, /model/0, ...
6
- - Banana FastAPI application is mounted at /banana_ripeness_stages, so
7
- e.g. its /api/health becomes /banana_ripeness_stages/api/health and
8
- its /files static becomes /banana_ripeness_stages/files.
9
 
10
- Run with: uvicorn gateway:app --host 0.0.0.0 --port 7860
11
- """
12
-
13
- import os
14
-
15
- # Must be set before importing the banana backend: config.files_url()
16
- # uses it to build correct /files/... URLs in API responses.
17
- os.environ.setdefault("BANANA_SUBPATH", "/banana_ripeness_stages")
18
-
19
- from a2wsgi import WSGIMiddleware # noqa: E402
20
- from fastapi import FastAPI # noqa: E402
21
-
22
- from app import app as flask_app # noqa: E402 (existing Flask — do not modify)
23
- from banana_ripeness_stages.app.main import app as banana_app # noqa: E402
24
-
25
- gateway = FastAPI(title="ml_models unified gateway")
26
-
27
- # Specific mount first, catch-all Flask second.
28
- gateway.mount("/banana_ripeness_stages", banana_app)
29
- gateway.mount("/", WSGIMiddleware(flask_app))
30
-
31
- # Uvicorn target alias.
32
- app = gateway
 
1
+ """Expose the existing Flask application as an ASGI app for Uvicorn."""
2
 
3
+ from a2wsgi import WSGIMiddleware
4
 
5
+ from app import app as flask_app
 
 
 
6
 
7
+ app = WSGIMiddleware(flask_app)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
test.py DELETED
@@ -1,101 +0,0 @@
1
- import argparse
2
- import json
3
- import mimetypes
4
- import sys
5
- import tkinter as tk
6
- from tkinter import filedialog
7
-
8
- import requests
9
-
10
-
11
- def main():
12
- parser = argparse.ArgumentParser(
13
- description="Select an image and upload it to the hosted model 1 API."
14
- )
15
- parser.add_argument(
16
- "image",
17
- nargs="?",
18
- help="Image file to upload. If omitted, a file picker opens.",
19
- )
20
- parser.add_argument(
21
- "--url",
22
- default="https://ajaycodeself-ml-models.hf.space/model/1",
23
- help="Model 1 endpoint URL (default: %(default)s).",
24
- )
25
- args = parser.parse_args()
26
-
27
- image_path = args.image
28
- if not image_path:
29
- try:
30
- root = tk.Tk()
31
- root.withdraw()
32
- image_path = filedialog.askopenfilename(
33
- title="Select an image to upload",
34
- filetypes=[
35
- ("Image files", "*.jpg *.jpeg *.png *.webp *.bmp"),
36
- ("All files", "*.*"),
37
- ],
38
- )
39
- root.destroy()
40
- except tk.TclError as exc:
41
- print(f"Could not open image picker: {exc}", file=sys.stderr)
42
- return 1
43
-
44
- if not image_path:
45
- print("No image selected; upload cancelled.")
46
- return 1
47
-
48
- try:
49
- with open(image_path, "rb") as image_file:
50
- content_type = mimetypes.guess_type(image_path)[0] or "application/octet-stream"
51
- response = requests.post(
52
- args.url,
53
- files={
54
- "image": (
55
- image_path.rsplit("/", 1)[-1].rsplit("\\", 1)[-1],
56
- image_file,
57
- content_type,
58
- )
59
- },
60
- timeout=120,
61
- )
62
- except OSError as exc:
63
- print(f"Could not open selected image: {exc}", file=sys.stderr)
64
- return 1
65
- except requests.RequestException as exc:
66
- print(f"Could not reach {args.url}: {exc}", file=sys.stderr)
67
- print("Check that the Hugging Face Space is awake and running.", file=sys.stderr)
68
- return 1
69
-
70
- try:
71
- result = response.json()
72
- except ValueError:
73
- print(f"API returned non-JSON response (HTTP {response.status_code}).")
74
- print(response.text)
75
- return 1
76
-
77
- print(f"HTTP {response.status_code}")
78
- print(json.dumps(result, indent=2))
79
-
80
- expected_classes = {"healthy", "unhealthy"}
81
- if response.status_code != 200:
82
- print("FAIL: API request did not succeed.", file=sys.stderr)
83
- return 1
84
- if set(result) != {"prediction", "confidence_percent"}:
85
- print("FAIL: API returned an unexpected response shape.", file=sys.stderr)
86
- return 1
87
- if result.get("prediction") not in expected_classes:
88
- print("FAIL: API returned an unexpected prediction.", file=sys.stderr)
89
- return 1
90
-
91
- confidence_percent = result.get("confidence_percent")
92
- if not isinstance(confidence_percent, (int, float)) or confidence_percent != 100.0:
93
- print("FAIL: API confidence_percent was not 100.0.", file=sys.stderr)
94
- return 1
95
-
96
- print("PASS: image upload returned a valid model prediction.")
97
- return 0
98
-
99
-
100
- if __name__ == "__main__":
101
- raise SystemExit(main())