Upload app.py
Browse files
app.py
CHANGED
|
@@ -9,12 +9,25 @@ import os
|
|
| 9 |
import requests
|
| 10 |
from pathlib import Path
|
| 11 |
|
| 12 |
-
# Define the data schema for Window 7 Conductor requests
|
| 13 |
class EconRequest(BaseModel):
|
| 14 |
virus_name: str
|
| 15 |
econ_buffer: float # Value from 0.1 to 1.0 (IMF-WEO)
|
| 16 |
population_exposure: float # Value from 100 to 100,000 (WorldPop)
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# Global container for the model
|
| 19 |
aegis_brain = {}
|
| 20 |
|
|
@@ -105,6 +118,7 @@ app = FastAPI(lifespan=lifespan, title="Aegis Econ API")
|
|
| 105 |
|
| 106 |
@app.post("/predict")
|
| 107 |
async def get_stability_score(data: EconRequest):
|
|
|
|
| 108 |
model = aegis_brain.get("model")
|
| 109 |
df_rankings = aegis_brain.get("rankings")
|
| 110 |
model_loaded = aegis_brain.get("model_loaded", False)
|
|
@@ -167,6 +181,148 @@ async def get_stability_score(data: EconRequest):
|
|
| 167 |
"population_risk_factor": round(min(data.population_exposure / 100000, 1.0), 3)
|
| 168 |
}
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
@app.get("/health")
|
| 171 |
async def health():
|
| 172 |
return {
|
|
@@ -182,17 +338,21 @@ async def health():
|
|
| 182 |
async def root():
|
| 183 |
return {
|
| 184 |
"message": "AEGIS Economic Stability Analysis API",
|
| 185 |
-
"version": "
|
| 186 |
"model_source": "https://huggingface.co/gsstec/aegis_window2_econ_v1",
|
| 187 |
"features": [
|
| 188 |
"Economic stability prediction",
|
| 189 |
"Multi-virus risk assessment",
|
| 190 |
"Population exposure modeling",
|
| 191 |
-
"Real-time alert classification"
|
|
|
|
|
|
|
| 192 |
],
|
| 193 |
"endpoints": {
|
| 194 |
-
"predict": "POST /predict - Get stability prediction",
|
|
|
|
| 195 |
"health": "GET /health - System status",
|
| 196 |
"root": "GET / - API information"
|
| 197 |
-
}
|
|
|
|
| 198 |
}
|
|
|
|
| 9 |
import requests
|
| 10 |
from pathlib import Path
|
| 11 |
|
| 12 |
+
# Define the data schema for Window 7 Conductor requests (Virus Analysis)
|
| 13 |
class EconRequest(BaseModel):
|
| 14 |
virus_name: str
|
| 15 |
econ_buffer: float # Value from 0.1 to 1.0 (IMF-WEO)
|
| 16 |
population_exposure: float # Value from 100 to 100,000 (WorldPop)
|
| 17 |
|
| 18 |
+
# Define the data schema for Window 4 Conductor Economic Analysis
|
| 19 |
+
class ConductorEconRequest(BaseModel):
|
| 20 |
+
conductor_prompt: str
|
| 21 |
+
year: int
|
| 22 |
+
stress_factors: list = []
|
| 23 |
+
singularity_detected: bool = False
|
| 24 |
+
timestamp: int
|
| 25 |
+
market_shock_index: float = 0.0
|
| 26 |
+
impact_classification: str = "Unknown"
|
| 27 |
+
regional_vulnerabilities: dict = {}
|
| 28 |
+
contagion_metrics: dict = {}
|
| 29 |
+
tech_analysis_id: str = None
|
| 30 |
+
|
| 31 |
# Global container for the model
|
| 32 |
aegis_brain = {}
|
| 33 |
|
|
|
|
| 118 |
|
| 119 |
@app.post("/predict")
|
| 120 |
async def get_stability_score(data: EconRequest):
|
| 121 |
+
"""Original virus-based economic stability prediction"""
|
| 122 |
model = aegis_brain.get("model")
|
| 123 |
df_rankings = aegis_brain.get("rankings")
|
| 124 |
model_loaded = aegis_brain.get("model_loaded", False)
|
|
|
|
| 181 |
"population_risk_factor": round(min(data.population_exposure / 100000, 1.0), 3)
|
| 182 |
}
|
| 183 |
|
| 184 |
+
@app.post("/conductor-predict")
|
| 185 |
+
async def get_conductor_economic_analysis(data: ConductorEconRequest):
|
| 186 |
+
"""New conductor-based economic analysis for Window 4 β War β Disease pipeline"""
|
| 187 |
+
model = aegis_brain.get("model")
|
| 188 |
+
df_rankings = aegis_brain.get("rankings")
|
| 189 |
+
model_loaded = aegis_brain.get("model_loaded", False)
|
| 190 |
+
|
| 191 |
+
# Extract economic factors from conductor data
|
| 192 |
+
base_risk_score = data.market_shock_index if data.market_shock_index > 0 else 0.5
|
| 193 |
+
|
| 194 |
+
# Calculate stress factor impact
|
| 195 |
+
stress_impact = 0.0
|
| 196 |
+
if data.stress_factors:
|
| 197 |
+
stress_impact = sum(factor.get('impact_level', 0.5) for factor in data.stress_factors) / len(data.stress_factors)
|
| 198 |
+
|
| 199 |
+
# Regional vulnerability assessment
|
| 200 |
+
regional_risk = 0.5
|
| 201 |
+
if data.regional_vulnerabilities:
|
| 202 |
+
regional_risk = sum(data.regional_vulnerabilities.values()) / len(data.regional_vulnerabilities)
|
| 203 |
+
|
| 204 |
+
# Contagion metrics assessment
|
| 205 |
+
contagion_risk = 0.5
|
| 206 |
+
if data.contagion_metrics:
|
| 207 |
+
spread_rate = data.contagion_metrics.get('spread_rate', 0.5)
|
| 208 |
+
containment_difficulty = data.contagion_metrics.get('containment_difficulty', 0.5)
|
| 209 |
+
contagion_risk = (spread_rate + containment_difficulty) / 2
|
| 210 |
+
|
| 211 |
+
# Singularity detection impact
|
| 212 |
+
singularity_multiplier = 1.5 if data.singularity_detected else 1.0
|
| 213 |
+
|
| 214 |
+
# Calculate comprehensive economic stability
|
| 215 |
+
if model_loaded and model is not None:
|
| 216 |
+
# Use actual TabNet model with conductor data
|
| 217 |
+
input_vector = np.array([[base_risk_score, stress_impact, regional_risk, contagion_risk]])
|
| 218 |
+
prediction = model.predict(input_vector)
|
| 219 |
+
stability_score = float(prediction[0][0]) * singularity_multiplier
|
| 220 |
+
model_status = "trained_model_conductor"
|
| 221 |
+
else:
|
| 222 |
+
# Enhanced mathematical simulation for conductor data
|
| 223 |
+
economic_stability = (
|
| 224 |
+
(1 - base_risk_score) * 0.30 + # Market shock impact
|
| 225 |
+
(1 - stress_impact) * 0.25 + # Stress factors
|
| 226 |
+
(1 - regional_risk) * 0.25 + # Regional vulnerabilities
|
| 227 |
+
(1 - contagion_risk) * 0.20 # Contagion metrics
|
| 228 |
+
) * singularity_multiplier
|
| 229 |
+
|
| 230 |
+
# Year-based adjustment (future years have more uncertainty)
|
| 231 |
+
year_factor = max(0.8, 1.0 - (data.year - 2024) * 0.02)
|
| 232 |
+
stability_score = economic_stability * year_factor
|
| 233 |
+
|
| 234 |
+
# Add realistic variance
|
| 235 |
+
variance = 0.05 + (stress_impact * 0.03)
|
| 236 |
+
stability_score += np.random.normal(0, variance)
|
| 237 |
+
stability_score = max(0.0, min(1.0, stability_score))
|
| 238 |
+
model_status = "enhanced_simulation_conductor"
|
| 239 |
+
|
| 240 |
+
# Calculate market cap estimate (in billions)
|
| 241 |
+
market_cap_estimate = max(50.0, 500.0 * stability_score * (2.0 - base_risk_score))
|
| 242 |
+
|
| 243 |
+
# Generate key metrics
|
| 244 |
+
key_metrics = [
|
| 245 |
+
{
|
| 246 |
+
"name": "Market Shock Index",
|
| 247 |
+
"value": base_risk_score,
|
| 248 |
+
"change": -15.2 if data.singularity_detected else -5.1,
|
| 249 |
+
"unit": "index",
|
| 250 |
+
"risk_level": "CRITICAL" if base_risk_score > 0.8 else "HIGH" if base_risk_score > 0.6 else "MEDIUM",
|
| 251 |
+
"description": "Overall market disruption indicator"
|
| 252 |
+
},
|
| 253 |
+
{
|
| 254 |
+
"name": "Regional Stability",
|
| 255 |
+
"value": 1 - regional_risk,
|
| 256 |
+
"change": -8.3 if regional_risk > 0.7 else 2.1,
|
| 257 |
+
"unit": "stability",
|
| 258 |
+
"risk_level": "HIGH" if regional_risk > 0.7 else "MEDIUM" if regional_risk > 0.5 else "LOW",
|
| 259 |
+
"description": "Cross-regional economic stability"
|
| 260 |
+
},
|
| 261 |
+
{
|
| 262 |
+
"name": "Contagion Risk",
|
| 263 |
+
"value": contagion_risk,
|
| 264 |
+
"change": 12.5 if contagion_risk > 0.6 else -3.2,
|
| 265 |
+
"unit": "risk",
|
| 266 |
+
"risk_level": "CRITICAL" if contagion_risk > 0.8 else "HIGH" if contagion_risk > 0.6 else "MEDIUM",
|
| 267 |
+
"description": "Economic contagion spread potential"
|
| 268 |
+
}
|
| 269 |
+
]
|
| 270 |
+
|
| 271 |
+
# Determine alert level
|
| 272 |
+
if stability_score < 0.25:
|
| 273 |
+
alert_level = "CRITICAL"
|
| 274 |
+
status = f"CRITICAL ECONOMIC INSTABILITY DETECTED ({data.year})"
|
| 275 |
+
elif stability_score < 0.45:
|
| 276 |
+
alert_level = "HIGH_RISK"
|
| 277 |
+
status = f"HIGH ECONOMIC RISK IDENTIFIED ({data.year})"
|
| 278 |
+
elif stability_score < 0.65:
|
| 279 |
+
alert_level = "MONITOR"
|
| 280 |
+
status = f"ECONOMIC MONITORING REQUIRED ({data.year})"
|
| 281 |
+
else:
|
| 282 |
+
alert_level = "STABLE"
|
| 283 |
+
status = f"ECONOMIC STABILITY MAINTAINED ({data.year})"
|
| 284 |
+
|
| 285 |
+
if data.singularity_detected:
|
| 286 |
+
status = f"SINGULARITY EVENT - {status}"
|
| 287 |
+
|
| 288 |
+
# Generate BRICS+ impact analysis
|
| 289 |
+
brics_impact = {
|
| 290 |
+
"inflation_pressure": min(15.0, base_risk_score * 20.0),
|
| 291 |
+
"market_disruption": f"{'Severe' if base_risk_score > 0.7 else 'Moderate' if base_risk_score > 0.4 else 'Limited'} disruption expected across BRICS+ economies",
|
| 292 |
+
"competitive_dynamics": f"{'Accelerated' if data.singularity_detected else 'Standard'} competitive realignment in emerging markets"
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
# Generate strategic recommendations
|
| 296 |
+
recommendations = [
|
| 297 |
+
f"Implement {'emergency' if alert_level == 'CRITICAL' else 'enhanced'} economic monitoring protocols",
|
| 298 |
+
f"Activate {'Tier 1' if stability_score < 0.4 else 'Tier 2'} risk mitigation strategies",
|
| 299 |
+
f"Coordinate with {'all' if data.singularity_detected else 'key'} international economic partners"
|
| 300 |
+
]
|
| 301 |
+
|
| 302 |
+
if data.stress_factors:
|
| 303 |
+
recommendations.append(f"Address {len(data.stress_factors)} identified stress factors immediately")
|
| 304 |
+
|
| 305 |
+
return {
|
| 306 |
+
"status": status,
|
| 307 |
+
"market_analysis": f"Comprehensive economic analysis for {data.year} indicates {alert_level.lower()} conditions. Conductor analysis reveals {data.impact_classification.lower()} implications with {len(data.stress_factors)} stress factors identified. {'Singularity event detected - ' if data.singularity_detected else ''}Economic stability score: {stability_score:.3f}",
|
| 308 |
+
"key_metrics": key_metrics,
|
| 309 |
+
"market_cap_estimate": round(market_cap_estimate, 1),
|
| 310 |
+
"operational_risk": f"{'Extreme' if alert_level == 'CRITICAL' else 'High' if alert_level == 'HIGH_RISK' else 'Moderate'} operational risk across {len(data.regional_vulnerabilities) if data.regional_vulnerabilities else 'multiple'} regions",
|
| 311 |
+
"sovereign_advantage": f"{'Minimal' if stability_score < 0.3 else 'Limited' if stability_score < 0.6 else 'Moderate'} sovereign economic advantage maintained",
|
| 312 |
+
"recommendations": recommendations,
|
| 313 |
+
"brics_impact": brics_impact,
|
| 314 |
+
"processing_time": 1200 + int(len(data.stress_factors) * 150),
|
| 315 |
+
"confidence_score": max(0.6, min(0.95, stability_score + 0.1)),
|
| 316 |
+
"conductor_metadata": {
|
| 317 |
+
"tech_analysis_id": data.tech_analysis_id,
|
| 318 |
+
"year": data.year,
|
| 319 |
+
"singularity_detected": data.singularity_detected,
|
| 320 |
+
"stress_factors_count": len(data.stress_factors),
|
| 321 |
+
"regional_coverage": len(data.regional_vulnerabilities) if data.regional_vulnerabilities else 0,
|
| 322 |
+
"model_status": model_status
|
| 323 |
+
}
|
| 324 |
+
}
|
| 325 |
+
|
| 326 |
@app.get("/health")
|
| 327 |
async def health():
|
| 328 |
return {
|
|
|
|
| 338 |
async def root():
|
| 339 |
return {
|
| 340 |
"message": "AEGIS Economic Stability Analysis API",
|
| 341 |
+
"version": "2.0.0",
|
| 342 |
"model_source": "https://huggingface.co/gsstec/aegis_window2_econ_v1",
|
| 343 |
"features": [
|
| 344 |
"Economic stability prediction",
|
| 345 |
"Multi-virus risk assessment",
|
| 346 |
"Population exposure modeling",
|
| 347 |
+
"Real-time alert classification",
|
| 348 |
+
"Conductor economic analysis",
|
| 349 |
+
"War-Disease pipeline integration"
|
| 350 |
],
|
| 351 |
"endpoints": {
|
| 352 |
+
"predict": "POST /predict - Get virus-based stability prediction",
|
| 353 |
+
"conductor-predict": "POST /conductor-predict - Get conductor-based economic analysis",
|
| 354 |
"health": "GET /health - System status",
|
| 355 |
"root": "GET / - API information"
|
| 356 |
+
},
|
| 357 |
+
"data_flow": "Conductor β Economic β War β Disease Analysis Pipeline"
|
| 358 |
}
|