Spaces:
Running
Running
File size: 4,860 Bytes
5ccd893 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | """
Data Models
Simple data classes and models for the application
"""
from dataclasses import dataclass
from typing import Optional, Dict, Any, List
from datetime import datetime
@dataclass
class Location:
"""Location data model"""
latitude: float
longitude: float
def __post_init__(self):
"""Validate coordinates"""
if not (-90 <= self.latitude <= 90):
raise ValueError(f"Invalid latitude: {self.latitude}")
if not (-180 <= self.longitude <= 180):
raise ValueError(f"Invalid longitude: {self.longitude}")
def to_dict(self) -> Dict[str, float]:
"""Convert to dictionary"""
return {
'latitude': self.latitude,
'longitude': self.longitude
}
@dataclass
class ChatMessage:
"""Chat message data model"""
message: str
context: Optional[Dict[str, Any]] = None
timestamp: Optional[datetime] = None
def __post_init__(self):
"""Set default timestamp"""
if self.timestamp is None:
self.timestamp = datetime.now()
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary"""
return {
'message': self.message,
'context': self.context,
'timestamp': self.timestamp.isoformat() if self.timestamp else None
}
@dataclass
class ChatResponse:
"""Chat response data model"""
response: str
status: str
model: Optional[str] = None
attempt: Optional[int] = None
timestamp: Optional[datetime] = None
def __post_init__(self):
"""Set default timestamp"""
if self.timestamp is None:
self.timestamp = datetime.now()
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary"""
return {
'response': self.response,
'status': self.status,
'model': self.model,
'attempt': self.attempt,
'timestamp': self.timestamp.isoformat() if self.timestamp else None
}
@dataclass
class SatelliteRequest:
"""Satellite data request model"""
location: Location
start_date: str
end_date: str
collection: str = 'COPERNICUS/S2_SR'
cloud_filter: int = 20
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary"""
return {
'location': self.location.to_dict(),
'start_date': self.start_date,
'end_date': self.end_date,
'collection': self.collection,
'cloud_filter': self.cloud_filter
}
@dataclass
class RegionRequest:
"""Region data request model"""
bounds: List[List[float]]
start_date: str
end_date: str
scale: int = 10
def __post_init__(self):
"""Validate bounds"""
if len(self.bounds) < 3:
raise ValueError("Bounds must contain at least 3 coordinate pairs")
for i, coord in enumerate(self.bounds):
if len(coord) != 2:
raise ValueError(f"Invalid coordinate at index {i}")
lon, lat = coord
if not (-180 <= lon <= 180) or not (-90 <= lat <= 90):
raise ValueError(f"Invalid coordinates at index {i}: [{lon}, {lat}]")
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary"""
return {
'bounds': self.bounds,
'start_date': self.start_date,
'end_date': self.end_date,
'scale': self.scale
}
@dataclass
class ServiceStatus:
"""Service status model"""
service_name: str
status: str
initialized: bool
timestamp: Optional[datetime] = None
details: Optional[Dict[str, Any]] = None
def __post_init__(self):
"""Set default timestamp"""
if self.timestamp is None:
self.timestamp = datetime.now()
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary"""
return {
'service_name': self.service_name,
'status': self.status,
'initialized': self.initialized,
'timestamp': self.timestamp.isoformat() if self.timestamp else None,
'details': self.details
}
@dataclass
class ErrorResponse:
"""Error response model"""
error: str
status: str = 'error'
timestamp: Optional[datetime] = None
details: Optional[Dict[str, Any]] = None
def __post_init__(self):
"""Set default timestamp"""
if self.timestamp is None:
self.timestamp = datetime.now()
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary"""
return {
'error': self.error,
'status': self.status,
'timestamp': self.timestamp.isoformat() if self.timestamp else None,
'details': self.details
} |