happynickamazon commited on
Commit
5c0caf7
·
verified ·
1 Parent(s): 88767fe

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +29 -26
app.py CHANGED
@@ -147,23 +147,24 @@ class Asset(Base):
147
  predictions = relationship("HealthPrediction", back_populates="asset", cascade="all, delete-orphan")
148
 
149
  def to_dict(self) -> Dict:
 
150
  return {
151
  "id": self.id,
152
  "owner_id": self.owner_id,
153
- "projectName": self.project_name,
154
  "assetId": self.asset_id,
155
- "location": self.location,
156
- "capacity": self.capacity,
157
- "bmsProvider": self.bms_provider,
158
  "category": json.loads(self.category) if self.category else [],
159
- "projectStage": self.project_stage,
160
- "healthScore": self.health_score,
161
- "remainingLife": self.remaining_life,
162
- "degradationRate": self.degradation_rate,
163
- "confidence": self.confidence,
164
- "stateOfCharge": self.state_of_charge,
165
- "voltage": self.voltage,
166
- "temperature": self.temperature,
167
  "isOnline": bool(self.is_online),
168
  "nftHash": self.nft_hash,
169
  "nftMintDate": self.nft_mint_date.isoformat() if self.nft_mint_date else None,
@@ -190,21 +191,21 @@ class HealthPrediction(Base):
190
  asset = relationship("Asset", back_populates="predictions")
191
 
192
  def to_dict(self) -> Dict:
193
- """Convert to dictionary for API response"""
194
  return {
195
  "id": self.id,
196
  "asset_id": self.asset_id,
197
  "prediction_date": self.prediction_date.isoformat() if self.prediction_date else None,
198
- "cycle_index": self.cycle_index,
199
- "health_score": self.health_score,
200
- "current_soh": self.current_soh,
201
- "soh_lower_95": self.soh_lower_95,
202
- "soh_upper_95": self.soh_upper_95,
203
- "remaining_life_years": self.remaining_life_years,
204
- "remaining_life_cycles": self.remaining_life_cycles,
205
- "degradation_rate": self.degradation_rate,
206
- "confidence": self.confidence,
207
- "data_quality": self.data_quality,
208
  "shap_top_features": json.loads(self.shap_top_features) if self.shap_top_features else None,
209
  }
210
 
@@ -247,6 +248,7 @@ def perform_seeding(db: Session):
247
  import random
248
 
249
  owner = "demo-user-1"
 
250
 
251
  # 1. NASA Heritage Fleet
252
  nasa_assets = [
@@ -269,7 +271,9 @@ def perform_seeding(db: Session):
269
  all_assets = nasa_assets + stanford_fleet
270
 
271
  for a in all_assets:
272
- uid = str(uuid.uuid4())
 
 
273
  new_asset = Asset(
274
  id=uid, owner_id=owner, project_name=a["name"],
275
  asset_id=a["id"], location=f"Lab Bay {random.randint(1, 20)}", capacity=100.0,
@@ -283,7 +287,6 @@ def perform_seeding(db: Session):
283
  # Create 10 historical data points for realistic charts
284
  for i in range(1, 11):
285
  cycle = i * 20
286
- # Create a realistic "descending" health curve
287
  h_point = a["health"] + (11-i) * random.uniform(0.3, 0.6)
288
  db.add(HealthPrediction(
289
  asset_id=uid, cycle_index=cycle, health_score=round(h_point, 1),
@@ -291,7 +294,7 @@ def perform_seeding(db: Session):
291
  ))
292
 
293
  db.commit()
294
- print(f"✅ Enterprise Seed Complete: {len(all_assets)} assets created for {owner}")
295
 
296
  # --- 5. ROUTES ---
297
  @app.on_event("startup")
 
147
  predictions = relationship("HealthPrediction", back_populates="asset", cascade="all, delete-orphan")
148
 
149
  def to_dict(self) -> Dict:
150
+ """Convert to dictionary for API response with safe defaults"""
151
  return {
152
  "id": self.id,
153
  "owner_id": self.owner_id,
154
+ "projectName": self.project_name or "Unknown Project",
155
  "assetId": self.asset_id,
156
+ "location": self.location or "Global",
157
+ "capacity": float(self.capacity or 100.0),
158
+ "bmsProvider": self.bms_provider or "Standard",
159
  "category": json.loads(self.category) if self.category else [],
160
+ "projectStage": self.project_stage or "Monitoring",
161
+ "healthScore": float(self.health_score or 0.0),
162
+ "remainingLife": float(self.remaining_life or 0.0),
163
+ "degradationRate": float(self.degradation_rate or 0.0),
164
+ "confidence": float(self.confidence or 0.0),
165
+ "stateOfCharge": float(self.state_of_charge or 0.0),
166
+ "voltage": float(self.voltage or 0.0),
167
+ "temperature": float(self.temperature or 0.0),
168
  "isOnline": bool(self.is_online),
169
  "nftHash": self.nft_hash,
170
  "nftMintDate": self.nft_mint_date.isoformat() if self.nft_mint_date else None,
 
191
  asset = relationship("Asset", back_populates="predictions")
192
 
193
  def to_dict(self) -> Dict:
194
+ """Convert to dictionary for API response with safe defaults"""
195
  return {
196
  "id": self.id,
197
  "asset_id": self.asset_id,
198
  "prediction_date": self.prediction_date.isoformat() if self.prediction_date else None,
199
+ "cycle_index": int(self.cycle_index or 0),
200
+ "health_score": float(self.health_score or 0.0),
201
+ "current_soh": float(self.current_soh or 0.0),
202
+ "soh_lower_95": float(self.soh_lower_95 or 0.0),
203
+ "soh_upper_95": float(self.soh_upper_95 or 0.0),
204
+ "remaining_life_years": float(self.remaining_life_years or 0.0),
205
+ "remaining_life_cycles": float(self.remaining_life_cycles or 0.0),
206
+ "degradation_rate": float(self.degradation_rate or 0.0),
207
+ "confidence": float(self.confidence or 0.0),
208
+ "data_quality": float(self.data_quality or 0.0),
209
  "shap_top_features": json.loads(self.shap_top_features) if self.shap_top_features else None,
210
  }
211
 
 
248
  import random
249
 
250
  owner = "demo-user-1"
251
+ namespace = uuid.NAMESPACE_DNS
252
 
253
  # 1. NASA Heritage Fleet
254
  nasa_assets = [
 
271
  all_assets = nasa_assets + stanford_fleet
272
 
273
  for a in all_assets:
274
+ # Use deterministic UUID based on asset_id so links don't break on restart
275
+ uid = str(uuid.uuid5(namespace, a["id"]))
276
+
277
  new_asset = Asset(
278
  id=uid, owner_id=owner, project_name=a["name"],
279
  asset_id=a["id"], location=f"Lab Bay {random.randint(1, 20)}", capacity=100.0,
 
287
  # Create 10 historical data points for realistic charts
288
  for i in range(1, 11):
289
  cycle = i * 20
 
290
  h_point = a["health"] + (11-i) * random.uniform(0.3, 0.6)
291
  db.add(HealthPrediction(
292
  asset_id=uid, cycle_index=cycle, health_score=round(h_point, 1),
 
294
  ))
295
 
296
  db.commit()
297
+ print(f"✅ Enterprise Seed Complete: {len(all_assets)} assets created with stable IDs.")
298
 
299
  # --- 5. ROUTES ---
300
  @app.on_event("startup")