Spaces:
Sleeping
Sleeping
Update schemas.py
Browse files- schemas.py +54 -24
schemas.py
CHANGED
|
@@ -1,41 +1,71 @@
|
|
| 1 |
from pydantic import BaseModel
|
| 2 |
-
from typing import Dict, List, Optional, Union
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
class PredictRequest(BaseModel):
|
| 5 |
"""
|
| 6 |
-
Forest segmentation prediction request
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
-
|
| 11 |
-
-
|
| 12 |
-
|
| 13 |
-
Required
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
- Optical bands: [-0.2, 0.6]
|
| 23 |
-
- Indices: [-1, 1]
|
| 24 |
"""
|
|
|
|
| 25 |
model_name: str = "forest_segmentation"
|
| 26 |
model_version: str = "landsat8_v1"
|
| 27 |
-
bands: Dict[str, Union[str, List, int]] # Band data as base64 or array
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
class PredictResponse(BaseModel):
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
forest_percentage: float
|
| 37 |
forest_confidence: float
|
| 38 |
mean_prediction: float
|
|
|
|
|
|
|
| 39 |
classes: Dict[str, int]
|
|
|
|
|
|
|
| 40 |
model_info: Dict[str, Any]
|
|
|
|
|
|
|
| 41 |
debug: Optional[Dict[str, Any]] = None
|
|
|
|
| 1 |
from pydantic import BaseModel
|
| 2 |
+
from typing import Dict, List, Optional, Union, Any
|
| 3 |
+
|
| 4 |
+
# =============================================================================
|
| 5 |
+
# REQUEST SCHEMA
|
| 6 |
+
# =============================================================================
|
| 7 |
|
| 8 |
class PredictRequest(BaseModel):
|
| 9 |
"""
|
| 10 |
+
Forest segmentation prediction request.
|
| 11 |
+
|
| 12 |
+
This schema is intentionally flexible to support:
|
| 13 |
+
- Supabase Edge Functions
|
| 14 |
+
- Hugging Face remote inference
|
| 15 |
+
- Local inference scripts
|
| 16 |
+
|
| 17 |
+
Required:
|
| 18 |
+
- bands: Dict[str, Union[str, List[float]]]
|
| 19 |
+
|
| 20 |
+
Allowed extra fields (sent by Supabase):
|
| 21 |
+
- width, height
|
| 22 |
+
- bbox
|
| 23 |
+
- band_names
|
| 24 |
+
- preprocessing
|
| 25 |
+
- model_name, model_version
|
|
|
|
|
|
|
| 26 |
"""
|
| 27 |
+
|
| 28 |
model_name: str = "forest_segmentation"
|
| 29 |
model_version: str = "landsat8_v1"
|
|
|
|
| 30 |
|
| 31 |
+
# Band data:
|
| 32 |
+
# - base64-encoded float32 (remote calls)
|
| 33 |
+
# - list/array of floats (local calls)
|
| 34 |
+
bands: Dict[str, Union[str, List[float]]]
|
| 35 |
+
|
| 36 |
+
class Config:
|
| 37 |
+
# 🔑 CRITICAL FIX
|
| 38 |
+
# Allows Supabase to send extra metadata without 422 errors
|
| 39 |
+
extra = "allow"
|
| 40 |
|
| 41 |
+
|
| 42 |
+
# =============================================================================
|
| 43 |
+
# RESPONSE SCHEMA
|
| 44 |
+
# =============================================================================
|
| 45 |
|
| 46 |
class PredictResponse(BaseModel):
|
| 47 |
+
"""
|
| 48 |
+
Forest segmentation prediction response.
|
| 49 |
+
|
| 50 |
+
Mask values are CONTINUOUS (0–255) — NOT binary.
|
| 51 |
+
"""
|
| 52 |
+
|
| 53 |
+
# Flattened mask (length = width * height)
|
| 54 |
+
mask: List[int]
|
| 55 |
+
|
| 56 |
+
# Inverted mask (optional utility)
|
| 57 |
+
inverted_mask: List[int]
|
| 58 |
+
|
| 59 |
+
# Statistics
|
| 60 |
forest_percentage: float
|
| 61 |
forest_confidence: float
|
| 62 |
mean_prediction: float
|
| 63 |
+
|
| 64 |
+
# Class mapping
|
| 65 |
classes: Dict[str, int]
|
| 66 |
+
|
| 67 |
+
# Model metadata
|
| 68 |
model_info: Dict[str, Any]
|
| 69 |
+
|
| 70 |
+
# Optional debug block
|
| 71 |
debug: Optional[Dict[str, Any]] = None
|