gsstec commited on
Commit
1cc4ed0
·
verified ·
1 Parent(s): da9ac69

Deploy AEGIS Economics Stability Analysis App

Browse files
Files changed (4) hide show
  1. Dockerfile +32 -0
  2. README.md +52 -11
  3. app.py +66 -0
  4. requirements.txt +10 -0
Dockerfile ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use a lightweight Python base instead of heavy CUDA
2
+ FROM python:3.12-slim
3
+
4
+ # Set environment variables for stability
5
+ ENV PYTHONUNBUFFERED=1 \
6
+ PYTHONDONTWRITEBYTECODE=1
7
+
8
+ WORKDIR /app
9
+
10
+ # Install system dependencies
11
+ RUN apt-get update && apt-get install -y \
12
+ build-essential \
13
+ curl \
14
+ && rm -rf /var/lib/apt/lists/*
15
+
16
+ # Copy requirements first for better caching
17
+ COPY requirements.txt .
18
+
19
+ # Force install the CPU version of Torch to avoid CUDA bloat
20
+ RUN pip install --no-cache-dir --upgrade pip && \
21
+ pip install --no-cache-dir torch --index-url https://download.pytorch.org/whl/cpu && \
22
+ pip install --no-cache-dir -r requirements.txt
23
+
24
+ # Copy application files and your trained model
25
+ COPY econ.py .
26
+ COPY aegis_window2_econ_v1.zip .
27
+
28
+ # Hugging Face Spaces port
29
+ EXPOSE 7860
30
+
31
+ # Launch using Uvicorn (FastAPI)
32
+ CMD ["uvicorn", "econ:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -1,11 +1,52 @@
1
- ---
2
- title: Econ
3
- emoji: 🏢
4
- colorFrom: purple
5
- colorTo: yellow
6
- sdk: docker
7
- pinned: false
8
- license: mit
9
- ---
10
-
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: AEGIS Economic Stability Analysis
3
+ emoji: 📊
4
+ colorFrom: blue
5
+ colorTo: green
6
+ sdk: docker
7
+ pinned: false
8
+ license: mit
9
+ ---
10
+
11
+ # AEGIS Economic Stability Analysis
12
+
13
+ This application provides economic stability predictions based on viral outbreak scenarios using advanced TabNet regression models.
14
+
15
+ ## Features
16
+
17
+ - **Virus Risk Assessment**: Analyzes economic impact based on virus characteristics
18
+ - **Population Exposure Modeling**: Considers population density and exposure patterns
19
+ - **Economic Buffer Analysis**: Incorporates IMF-WEO economic resilience indicators
20
+ - **Real-time Predictions**: FastAPI-powered REST API for stability scoring
21
+
22
+ ## API Endpoints
23
+
24
+ ### POST /predict
25
+ Predict economic stability score for a given scenario.
26
+
27
+ **Request Body:**
28
+ ```json
29
+ {
30
+ "virus_name": "COVID-19",
31
+ "econ_buffer": 0.7,
32
+ "population_exposure": 50000
33
+ }
34
+ ```
35
+
36
+ **Response:**
37
+ ```json
38
+ {
39
+ "virus": "COVID-19",
40
+ "stability_score": 0.6234,
41
+ "alert_level": "MONITOR"
42
+ }
43
+ ```
44
+
45
+ ### GET /health
46
+ Check system status and hardware configuration.
47
+
48
+ ## Usage
49
+
50
+ The application runs on port 7860 and provides a REST API for economic stability analysis.
51
+
52
+ Built with FastAPI, PyTorch, and TabNet for robust economic modeling.
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+ import pandas as pd
4
+ import numpy as np
5
+ import torch
6
+ from pytorch_tabnet.tab_model import TabNetRegressor
7
+ from contextlib import asynccontextmanager
8
+ import os
9
+
10
+ # Define the data schema for Window 7 Conductor requests
11
+ class EconRequest(BaseModel):
12
+ virus_name: str
13
+ econ_buffer: float # Value from 0.1 to 1.0 (IMF-WEO)
14
+ population_exposure: float # Value from 100 to 100,000 (WorldPop)
15
+
16
+ # Global container for the model
17
+ aegis_brain = {}
18
+
19
+ @asynccontextmanager
20
+ async def lifespan(app: FastAPI):
21
+ # Load model on startup
22
+ model_path = "aegis_window2_econ_v1.zip"
23
+ regressor = TabNetRegressor()
24
+ if os.path.exists(model_path):
25
+ regressor.load_model(model_path)
26
+ aegis_brain["model"] = regressor
27
+ # Load risk data for cross-referencing
28
+ aegis_brain["rankings"] = pd.read_csv("https://huggingface.co/gsstec/aegis_window2_econ_v1/resolve/main/Rankings.csv")
29
+ print("✅ Window 2 Brain & Rankings Loaded.")
30
+ yield
31
+ aegis_brain.clear()
32
+
33
+ app = FastAPI(lifespan=lifespan, title="Aegis Econ API")
34
+
35
+ @app.post("/predict")
36
+ async def get_stability_score(data: EconRequest):
37
+ model = aegis_brain.get("model")
38
+ df_rankings = aegis_brain.get("rankings")
39
+
40
+ if not model or df_rankings is None:
41
+ raise HTTPException(status_code=500, detail="Model not initialized.")
42
+
43
+ # 1. Fetch risk score from Rankings.csv
44
+ virus_row = df_rankings[df_rankings['Virus Name'] == data.virus_name]
45
+ if virus_row.empty:
46
+ raise HTTPException(status_code=404, detail="Virus not found in Risk Drive.")
47
+
48
+ base_score = virus_row.iloc[0]['Original Score']
49
+ risk_impact = base_score * 0.7
50
+
51
+ # 2. Vectorize for TabNet
52
+ input_vector = np.array([[base_score, risk_impact, data.econ_buffer, data.population_exposure]])
53
+
54
+ # 3. Predict Continental Stability
55
+ prediction = model.predict(input_vector)
56
+ stability_score = float(prediction[0][0])
57
+
58
+ return {
59
+ "virus": data.virus_name,
60
+ "stability_score": round(stability_score, 4),
61
+ "alert_level": "CRITICAL" if stability_score < 0.1 else "MONITOR"
62
+ }
63
+
64
+ @app.get("/health")
65
+ async def health():
66
+ return {"status": "operational", "hardware": "T4 GPU Active" if torch.cuda.is_available() else "CPU Mode"}
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ # AEGIS Economic Stability Analysis - FastAPI Requirements
2
+ fastapi==0.104.1
3
+ uvicorn[standard]==0.24.0
4
+ pydantic==2.5.0
5
+ pandas==2.1.4
6
+ numpy==1.24.4
7
+ torch==2.1.1
8
+ pytorch-tabnet==4.1.0
9
+ requests==2.31.0
10
+ python-multipart==0.0.6