Spaces:
Sleeping
Sleeping
Commit Β·
93e9982
1
Parent(s): a36e07f
Added Training model
Browse files- CivicAI_Training.ipynb +0 -282
- Training.ipynb +0 -0
- build_notebook.py +2 -0
CivicAI_Training.ipynb
DELETED
|
@@ -1,282 +0,0 @@
|
|
| 1 |
-
{
|
| 2 |
-
"cells": [
|
| 3 |
-
{
|
| 4 |
-
"cell_type": "markdown",
|
| 5 |
-
"metadata": {},
|
| 6 |
-
"source": [
|
| 7 |
-
"# \ud83c\udfdb CivicAI Advanced \u2014 Senior ML Engineer Edition\\n",
|
| 8 |
-
"**Real-time Economic Data + GRPO + LoRA + Multi-Country + Live Dashboard**"
|
| 9 |
-
]
|
| 10 |
-
},
|
| 11 |
-
{
|
| 12 |
-
"cell_type": "markdown",
|
| 13 |
-
"metadata": {},
|
| 14 |
-
"source": [
|
| 15 |
-
"### CELL 1: INSTALL DEPENDENCIES\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
|
| 16 |
-
]
|
| 17 |
-
},
|
| 18 |
-
{
|
| 19 |
-
"cell_type": "code",
|
| 20 |
-
"execution_count": null,
|
| 21 |
-
"metadata": {},
|
| 22 |
-
"outputs": [],
|
| 23 |
-
"source": [
|
| 24 |
-
"!pip install -q \\\n \"transformers>=4.38\" \\\n \"accelerate>=0.27\" \\\n \"trl>=0.10\" \\\n \"peft>=0.9\" \\\n \"bitsandbytes>=0.42\" \\\n \"datasets>=2.17\" \\\n \"requests>=2.31\" \\\n \"pandas>=2.0\" \\\n \"fredapi\" \\\n \"world-bank-data\" \\\n \"plotly>=5.18\" \\\n \"rich>=13.0\" \\\n \"tenacity>=8.2\"\n\n# After install: Runtime \u2192 Restart session \u2192 run from Cell 2\\n"
|
| 25 |
-
]
|
| 26 |
-
},
|
| 27 |
-
{
|
| 28 |
-
"cell_type": "markdown",
|
| 29 |
-
"metadata": {},
|
| 30 |
-
"source": [
|
| 31 |
-
"### CELL 2: IMPORTS & SYSTEM SETUP\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
|
| 32 |
-
]
|
| 33 |
-
},
|
| 34 |
-
{
|
| 35 |
-
"cell_type": "code",
|
| 36 |
-
"execution_count": null,
|
| 37 |
-
"metadata": {},
|
| 38 |
-
"outputs": [],
|
| 39 |
-
"source": [
|
| 40 |
-
"import os, re, json, math, time, random, inspect, warnings, logging\nfrom datetime import datetime\nfrom typing import Dict, List, Optional, Tuple\nfrom pathlib import Path\n\nimport numpy as np\nimport pandas as pd\nimport torch\nimport requests\nimport plotly.graph_objects as go\nimport plotly.express as px\nfrom plotly.subplots import make_subplots\nfrom tenacity import retry, stop_after_attempt, wait_exponential\nfrom rich.console import Console\nfrom rich.table import Table\nfrom rich.progress import Progress, SpinnerColumn, TextColumn\nfrom rich import print as rprint\n\nwarnings.filterwarnings(\"ignore\")\nlogging.basicConfig(level=logging.ERROR)\n\nconsole = Console()\n\n# \u2500\u2500 Hardware detection \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\nCUDA_OK = torch.cuda.is_available()\nif CUDA_OK:\n CAP = torch.cuda.get_device_capability()\n USE_BF16 = CAP[0] >= 8\n USE_FP16 = not USE_BF16\n GPU_NAME = torch.cuda.get_device_name(0)\nelse:\n USE_BF16 = USE_FP16 = False\n GPU_NAME = \"CPU\"\n\nDEVICE = \"cuda\" if CUDA_OK else \"cpu\"\n\n# \u2500\u2500 Paths \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\nPath(\"assets\").mkdir(exist_ok=True)\nPath(\"checkpoints\").mkdir(exist_ok=True)\nPath(\"logs\").mkdir(exist_ok=True)\n\nconsole.rule(\"[bold cyan]CivicAI Advanced \u2014 System Ready\")\ntable = Table(show_header=False, box=None)\ntable.add_row(\"[cyan]PyTorch\", torch.__version__)\ntable.add_row(\"[cyan]Device\", GPU_NAME)\ntable.add_row(\"[cyan]BF16/FP16\", f\"bf16={USE_BF16} fp16={USE_FP16}\")\ntable.add_row(\"[cyan]Timestamp\", datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\"))\nconsole.print(table)\\n"
|
| 41 |
-
]
|
| 42 |
-
},
|
| 43 |
-
{
|
| 44 |
-
"cell_type": "markdown",
|
| 45 |
-
"metadata": {},
|
| 46 |
-
"source": [
|
| 47 |
-
"### CELL 3: REAL-TIME DATA FETCHER\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
|
| 48 |
-
]
|
| 49 |
-
},
|
| 50 |
-
{
|
| 51 |
-
"cell_type": "code",
|
| 52 |
-
"execution_count": null,
|
| 53 |
-
"metadata": {},
|
| 54 |
-
"outputs": [],
|
| 55 |
-
"source": [
|
| 56 |
-
"class RealTimeDataFetcher:\n \"\"\"\n Fetches live economic indicators from:\n \u2022 World Bank Open API (no key required)\n \u2022 FRED / St. Louis Fed (free key via fredapi)\n \u2022 BLS (Bureau of Labor Statistics \u2014 no key for basic tier)\n \u2022 REST Countries (social / governance proxies)\n Falls back to realistic historical means if any API is unavailable.\n \"\"\"\n\n WORLD_BANK_BASE = \"https://api.worldbank.org/v2\"\n BLS_BASE = \"https://api.bls.gov/publicAPI/v1/timeseries/data\"\n REST_COUNTRIES = \"https://restcountries.com/v3.1/alpha\"\n\n # World Bank indicator codes\n WB_INDICATORS = {\n \"inflation\" : \"FP.CPI.TOTL.ZG\", # CPI inflation %\n \"unemployment\": \"SL.UEM.TOTL.ZS\", # Unemployment % of labour force\n \"health_exp\" : \"SH.XPD.CHEX.GD.ZS\",# Health expenditure % of GDP\n \"life_expect\" : \"SP.DYN.LE00.IN\", # Life expectancy at birth\n \"gdp_growth\" : \"NY.GDP.MKTP.KD.ZG\", # GDP growth %\n \"homicide\" : \"VC.IHR.PSRC.P5\", # Intentional homicides per 100k\n }\n\n # Country ISO codes supported\n COUNTRIES = {\n \"USA\": {\"iso2\": \"US\", \"iso3\": \"USA\", \"name\": \"United States\"},\n \"IND\": {\"iso2\": \"IN\", \"iso3\": \"IND\", \"name\": \"India\"},\n \"GBR\": {\"iso2\": \"GB\", \"iso3\": \"GBR\", \"name\": \"United Kingdom\"},\n \"DEU\": {\"iso2\": \"DE\", \"iso3\": \"DEU\", \"name\": \"Germany\"},\n \"JPN\": {\"iso2\": \"JP\", \"iso3\": \"JPN\", \"name\": \"Japan\"},\n \"BRA\": {\"iso2\": \"BR\", \"iso3\": \"BRA\", \"name\": \"Brazil\"},\n }\n\n # Realistic fallback values (5-year historical means, 2019-2023)\n FALLBACKS = {\n \"USA\": {\"inflation\":3.8,\"unemployment\":4.8,\"health_exp\":17.2,\"life_expect\":77.5,\"gdp_growth\":2.1,\"homicide\":6.5},\n \"IND\": {\"inflation\":5.5,\"unemployment\":7.2,\"health_exp\":3.3, \"life_expect\":69.4,\"gdp_growth\":5.8,\"homicide\":2.8},\n \"GBR\": {\"inflation\":3.2,\"unemployment\":4.2,\"health_exp\":10.9,\"life_expect\":80.4,\"gdp_growth\":1.4,\"homicide\":1.2},\n \"DEU\": {\"inflation\":2.8,\"unemployment\":3.5,\"health_exp\":12.8,\"life_expect\":80.6,\"gdp_growth\":0.9,\"homicide\":0.9},\n \"JPN\": {\"inflation\":1.2,\"unemployment\":2.8,\"health_exp\":10.9,\"life_expect\":84.3,\"gdp_growth\":0.7,\"homicide\":0.2},\n \"BRA\": {\"inflation\":6.9,\"unemployment\":11.0,\"health_exp\":9.9,\"life_expect\":75.5,\"gdp_growth\":1.2,\"homicide\":22.4},\n }\n\n def __init__(self, cache_ttl_seconds: int = 3600):\n self._cache: Dict[str, Tuple[float, dict]] = {}\n self.cache_ttl = cache_ttl_seconds\n self.session = requests.Session()\n self.session.headers.update({\"User-Agent\": \"CivicAI/2.0\"})\n\n @retry(stop=stop_after_attempt(3), wait=wait_exponential(min=1, max=8))\n def _wb_fetch(self, country_iso2: str, indicator: str) -> Optional[float]:\n \"\"\"Fetch latest non-null value from World Bank API.\"\"\"\n url = (f\"{self.WORLD_BANK_BASE}/country/{country_iso2}/indicator/{indicator}\"\n f\"?format=json&mrv=5&per_page=5\")\n r = self.session.get(url, timeout=10)\n r.raise_for_status()\n data = r.json()\n if len(data) < 2 or not data[1]:\n return None\n for entry in data[1]:\n if entry.get(\"value\") is not None:\n return float(entry[\"value\"])\n return None\n\n def fetch_country(self, country_code: str = \"USA\") -> dict:\n \"\"\"\n Returns normalised economic state for a country.\n Uses cache \u2192 World Bank API \u2192 fallback in that order.\n \"\"\"\n cache_key = f\"{country_code}_{int(time.time() // self.cache_ttl)}\"\n if cache_key in self._cache:\n return self._cache[cache_key]\n\n meta = self.COUNTRIES.get(country_code, self.COUNTRIES[\"USA\"])\n iso2 = meta[\"iso2\"]\n raw = {}\n\n with Progress(SpinnerColumn(), TextColumn(\"[cyan]Fetching {task.description}\"),\n transient=True) as prog:\n t = prog.add_task(f\"live data for {meta['name']}\")\n for key, indicator in self.WB_INDICATORS.items():\n try:\n val = self._wb_fetch(iso2, indicator)\n raw[key] = val if val is not None else self.FALLBACKS[country_code][key]\n except Exception:\n raw[key] = self.FALLBACKS[country_code][key]\n\n # \u2500\u2500 Normalise to [0, 1] for the RL environment \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n state = {\n # Lower inflation = better; 0 %\u21921.0, \u226515 %\u21920.0\n \"inflation\" : max(0.0, min(1.0, 1 - raw[\"inflation\"] / 15.0)),\n # Higher employment = better\n \"employment\" : max(0.0, min(1.0, 1 - raw[\"unemployment\"]/ 25.0)),\n # Higher health expenditure + life expectancy = better\n \"health\" : max(0.0, min(1.0, (raw[\"health_exp\"] / 20.0 +\n raw[\"life_expect\"] / 90.0) / 2)),\n # GDP growth proxy for satisfaction\n \"satisfaction\": max(0.0, min(1.0, (raw[\"gdp_growth\"] + 5) / 15.0)),\n # Lower homicide = better\n \"crime\" : max(0.0, min(1.0, 1 - raw[\"homicide\"] / 50.0)),\n }\n\n # Attach raw for reporting\n state[\"_raw\"] = raw\n state[\"_country\"]= meta[\"name\"]\n state[\"_fetched\"]= datetime.now().isoformat()\n\n self._cache[cache_key] = state\n return state\n\n def fetch_all_countries(self) -> Dict[str, dict]:\n results = {}\n for code in self.COUNTRIES:\n console.log(f\"[dim]\u2192 fetching {code}\")\n results[code] = self.fetch_country(code)\n return results\n\n def to_dataframe(self, all_data: Dict[str, dict]) -> pd.DataFrame:\n rows = []\n for code, state in all_data.items():\n raw = state.get(\"_raw\", {})\n rows.append({\n \"country\" : state.get(\"_country\", code),\n \"code\" : code,\n \"inflation_pct\": raw.get(\"inflation\", 0),\n \"unemployment_pct\": raw.get(\"unemployment\", 0),\n \"health_exp_gdp\": raw.get(\"health_exp\", 0),\n \"life_expect\" : raw.get(\"life_expect\", 0),\n \"gdp_growth\" : raw.get(\"gdp_growth\", 0),\n \"homicide_rate\" : raw.get(\"homicide\", 0),\n # normalised\n \"norm_inflation\" : state[\"inflation\"],\n \"norm_employment\" : state[\"employment\"],\n \"norm_health\" : state[\"health\"],\n \"norm_satisfaction\": state[\"satisfaction\"],\n \"norm_crime\" : state[\"crime\"],\n \"fetched_at\" : state.get(\"_fetched\"),\n })\n return pd.DataFrame(rows)\n\n\n# Instantiate & fetch\nfetcher = RealTimeDataFetcher(cache_ttl_seconds=3600)\nall_data = fetcher.fetch_all_countries()\ndf_world = fetcher.to_dataframe(all_data)\n\nconsole.rule(\"[bold green]Live Data Fetched\")\nconsole.print(df_world[[\"country\",\"inflation_pct\",\"unemployment_pct\",\n \"health_exp_gdp\",\"gdp_growth\"]].to_string(index=False))\\n"
|
| 57 |
-
]
|
| 58 |
-
},
|
| 59 |
-
{
|
| 60 |
-
"cell_type": "markdown",
|
| 61 |
-
"metadata": {},
|
| 62 |
-
"source": [
|
| 63 |
-
"### CELL 4: REAL-DATA DASHBOARD\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
|
| 64 |
-
]
|
| 65 |
-
},
|
| 66 |
-
{
|
| 67 |
-
"cell_type": "code",
|
| 68 |
-
"execution_count": null,
|
| 69 |
-
"metadata": {},
|
| 70 |
-
"outputs": [],
|
| 71 |
-
"source": [
|
| 72 |
-
"def plot_global_dashboard(df: pd.DataFrame) -> None:\n fig = make_subplots(\n rows=2, cols=3,\n subplot_titles=(\n \"Inflation (%)\", \"Unemployment (%)\", \"Health Exp (% GDP)\",\n \"Life Expectancy (yrs)\", \"GDP Growth (%)\", \"Homicide Rate (per 100k)\"\n ),\n )\n cols_raw = [\"inflation_pct\",\"unemployment_pct\",\"health_exp_gdp\",\n \"life_expect\",\"gdp_growth\",\"homicide_rate\"]\n colors = px.colors.qualitative.Bold\n\n for i, col in enumerate(cols_raw):\n r, c = divmod(i, 3)\n fig.add_trace(\n go.Bar(\n x=df[\"country\"], y=df[col],\n marker_color=colors,\n showlegend=False,\n text=df[col].round(1), textposition=\"outside\"\n ),\n row=r+1, col=c+1\n )\n\n fig.update_layout(\n title_text=\"\ud83c\udf0d CivicAI \u2014 Real-Time Global Economic Dashboard\",\n title_font_size=20,\n height=600, template=\"plotly_dark\",\n paper_bgcolor=\"#0d1117\", plot_bgcolor=\"#0d1117\",\n font=dict(color=\"#e6edf3\"),\n )\n fig.show()\n fig.write_html(\"assets/global_dashboard.html\")\n console.log(\"[green]\u2713 Dashboard saved \u2192 assets/global_dashboard.html\")\n\nplot_global_dashboard(df_world)\\n"
|
| 73 |
-
]
|
| 74 |
-
},
|
| 75 |
-
{
|
| 76 |
-
"cell_type": "markdown",
|
| 77 |
-
"metadata": {},
|
| 78 |
-
"source": [
|
| 79 |
-
"### CELL 5: ADVANCED MULTI-COUNTRY ENVIRONMENT\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
|
| 80 |
-
]
|
| 81 |
-
},
|
| 82 |
-
{
|
| 83 |
-
"cell_type": "code",
|
| 84 |
-
"execution_count": null,
|
| 85 |
-
"metadata": {},
|
| 86 |
-
"outputs": [],
|
| 87 |
-
"source": [
|
| 88 |
-
"class AdvancedCivicAIEnv:\n \"\"\"\n Production-grade multi-country civic environment.\n \u2022 Initialises from real World Bank data\n \u2022 Supports 6 countries and 4 policy tasks\n \u2022 Action space: 5-dimensional continuous [0,1]\n \u2022 Observation: 10-dimensional (5 state + 5 delta from last step)\n \u2022 Reward: weighted multi-objective (Pareto-style)\n \u2022 Includes shock events (recession, pandemic proxy, crime spike)\n \"\"\"\n\n TASKS = {\n \"stabilize_economy\" : {\"inflation_weight\":0.4, \"employment_weight\":0.3, \"health_weight\":0.15, \"satisfaction_weight\":0.1, \"crime_weight\":0.05},\n \"improve_health\" : {\"inflation_weight\":0.1, \"employment_weight\":0.2, \"health_weight\":0.5, \"satisfaction_weight\":0.15,\"crime_weight\":0.05},\n \"reduce_crime\" : {\"inflation_weight\":0.1, \"employment_weight\":0.2, \"health_weight\":0.2, \"satisfaction_weight\":0.1, \"crime_weight\":0.4},\n \"maximize_wellbeing\" : {\"inflation_weight\":0.2, \"employment_weight\":0.2, \"health_weight\":0.2, \"satisfaction_weight\":0.2, \"crime_weight\":0.2},\n }\n\n SHOCK_EVENTS = [\n {\"name\":\"recession\", \"prob\":0.02, \"effect\":{\"inflation\":+0.15,\"employment\":-0.12,\"satisfaction\":-0.1}},\n {\"name\":\"pandemic\", \"prob\":0.01, \"effect\":{\"health\":-0.2, \"employment\":-0.1, \"satisfaction\":-0.15}},\n {\"name\":\"crime_spike\",\"prob\":0.02, \"effect\":{\"crime\":-0.15, \"satisfaction\":-0.08}},\n {\"name\":\"boom\", \"prob\":0.02, \"effect\":{\"employment\":+0.1,\"satisfaction\":+0.1,\"inflation\":+0.05}},\n ]\n\n def __init__(self, fetcher: RealTimeDataFetcher, default_country: str = \"USA\"):\n self.fetcher = fetcher\n self.default_country = default_country\n self._prev_state = None\n self.step_count = 0\n self.shock_log = []\n self.state_data = {}\n\n def reset(self, task_id: str = \"stabilize_economy\", country: str = None) -> dict:\n country = country or self.default_country\n self.task_id = task_id\n self.weights = self.TASKS[task_id]\n self.step_count = 0\n self.shock_log = []\n\n # Load real data as starting state\n live = self.fetcher.fetch_country(country)\n self.state_data = {k: live[k] for k in [\"inflation\",\"employment\",\"health\",\"satisfaction\",\"crime\"]}\n\n # Add small noise so each episode is unique\n for k in self.state_data:\n self.state_data[k] = float(np.clip(\n self.state_data[k] + np.random.normal(0, 0.02), 0.0, 1.0\n ))\n\n self._prev_state = dict(self.state_data)\n return self._build_obs()\n\n def _build_obs(self) -> dict:\n \"\"\"10-dim observation: current state + delta from previous step.\"\"\"\n obs = dict(self.state_data)\n obs[\"_task\"] = self.task_id\n obs[\"_step\"] = self.step_count\n if self._prev_state:\n for k in [\"inflation\",\"employment\",\"health\",\"satisfaction\",\"crime\"]:\n obs[f\"d_{k}\"] = self.state_data[k] - self._prev_state[k]\n else:\n for k in [\"inflation\",\"employment\",\"health\",\"satisfaction\",\"crime\"]:\n obs[f\"d_{k}\"] = 0.0\n return obs\n\n def _apply_shocks(self):\n \"\"\"Stochastic external shock events.\"\"\"\n for shock in self.SHOCK_EVENTS:\n if np.random.random() < shock[\"prob\"]:\n self.shock_log.append({\"step\": self.step_count, \"event\": shock[\"name\"]})\n for k, delta in shock[\"effect\"].items():\n if k in self.state_data:\n self.state_data[k] = float(np.clip(self.state_data[k] + delta, 0.0, 1.0))\n console.log(f\"[yellow]\u26a1 Shock event: {shock['name']} at step {self.step_count}\")\n\n def step(self, action: dict) -> Tuple[dict, float, bool, dict]:\n \"\"\"\n action keys: tax, jobs, healthcare, education, infrastructure\n Each in [0, 1] \u2014 represents budget allocation intensity.\n \"\"\"\n self._prev_state = dict(self.state_data)\n\n # Policy effects (with diminishing returns via sqrt)\n tax = action.get(\"tax\", 0.5)\n jobs = action.get(\"jobs\", 0.5)\n healthcare = action.get(\"healthcare\", 0.5)\n education = action.get(\"education\", 0.5)\n infra = action.get(\"infrastructure\",0.5)\n\n self.state_data[\"inflation\"] = np.clip(\n self.state_data[\"inflation\"] - tax * 0.08 + jobs * 0.02, 0.0, 1.0)\n self.state_data[\"employment\"] = np.clip(\n self.state_data[\"employment\"] + jobs * 0.06 + infra * 0.02, 0.0, 1.0)\n self.state_data[\"health\"] = np.clip(\n self.state_data[\"health\"] + healthcare * 0.07 + education * 0.02, 0.0, 1.0)\n self.state_data[\"satisfaction\"] = np.clip(\n self.state_data[\"satisfaction\"] + education * 0.05 + infra * 0.03\n - tax * 0.03, 0.0, 1.0)\n self.state_data[\"crime\"] = np.clip(\n self.state_data[\"crime\"] + education * 0.05 + jobs * 0.03\n - infra * 0.01, 0.0, 1.0)\n\n # Gaussian noise\n for k in self.state_data:\n self.state_data[k] = float(np.clip(\n self.state_data[k] + np.random.normal(0, 0.008), 0.0, 1.0))\n\n self._apply_shocks()\n self.step_count += 1\n\n reward = self._compute_reward()\n done = self.step_count >= 50\n info = {\"shocks\": self.shock_log, \"step\": self.step_count}\n return self._build_obs(), float(reward), done, info\n\n def _compute_reward(self) -> float:\n s = self.state_data\n w = self.weights\n return (\n w[\"inflation_weight\"] * s[\"inflation\"] +\n w[\"employment_weight\"] * s[\"employment\"] +\n w[\"health_weight\"] * s[\"health\"] +\n w[\"satisfaction_weight\"] * s[\"satisfaction\"] +\n w[\"crime_weight\"] * s[\"crime\"]\n )\n\n def state_report(self) -> dict:\n return {k: round(v, 4) for k, v in self.state_data.items()}\n\n\n# Smoke test\nenv_adv = AdvancedCivicAIEnv(fetcher, default_country=\"USA\")\nobs = env_adv.reset(\"stabilize_economy\", \"USA\")\nconsole.rule(\"[bold green]Advanced Environment Ready\")\nconsole.print(f\"Initial state (USA, real data): {env_adv.state_report()}\")\\n"
|
| 89 |
-
]
|
| 90 |
-
},
|
| 91 |
-
{
|
| 92 |
-
"cell_type": "markdown",
|
| 93 |
-
"metadata": {},
|
| 94 |
-
"source": [
|
| 95 |
-
"### CELL 6: PROMPT BUILDER (5-action)\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
|
| 96 |
-
]
|
| 97 |
-
},
|
| 98 |
-
{
|
| 99 |
-
"cell_type": "code",
|
| 100 |
-
"execution_count": null,
|
| 101 |
-
"metadata": {},
|
| 102 |
-
"outputs": [],
|
| 103 |
-
"source": [
|
| 104 |
-
"def build_prompt(obs: dict) -> str:\n task_desc = {\n \"stabilize_economy\" : \"Your priority is economic stability: control inflation and protect employment.\",\n \"improve_health\" : \"Your priority is public health: maximize health outcomes and life expectancy.\",\n \"reduce_crime\" : \"Your priority is public safety: reduce crime through investment and employment.\",\n \"maximize_wellbeing\" : \"Your priority is overall citizen wellbeing across all dimensions.\",\n }.get(obs.get(\"_task\",\"\"), \"Optimize all civic outcomes.\")\n\n return (\n f\"You are a senior policy advisor.\\n",
|
| 105 |
-
"{task_desc}\\n",
|
| 106 |
-
"\\n",
|
| 107 |
-
"\"\n f\"CURRENT STATE (step {obs.get('_step',0)}):\\n",
|
| 108 |
-
"\"\n f\" Inflation score : {obs.get('inflation',0.5):.3f} (\u0394 {obs.get('d_inflation',0):+.3f})\\n",
|
| 109 |
-
"\"\n f\" Employment score : {obs.get('employment',0.5):.3f} (\u0394 {obs.get('d_employment',0):+.3f})\\n",
|
| 110 |
-
"\"\n f\" Health score : {obs.get('health',0.5):.3f} (\u0394 {obs.get('d_health',0):+.3f})\\n",
|
| 111 |
-
"\"\n f\" Satisfaction score: {obs.get('satisfaction',0.5):.3f} (\u0394 {obs.get('d_satisfaction',0):+.3f})\\n",
|
| 112 |
-
"\"\n f\" Crime score : {obs.get('crime',0.5):.3f} (\u0394 {obs.get('d_crime',0):+.3f})\\n",
|
| 113 |
-
"\\n",
|
| 114 |
-
"\"\n \"OUTPUT FORMAT (all values 0.0\u20131.0, no other text):\\n",
|
| 115 |
-
"\"\n \"tax: 0.X, jobs: 0.X, healthcare: 0.X, education: 0.X, infrastructure: 0.X\"\n )\n\n\ndef parse_action(text: str) -> dict:\n \"\"\"5-dimensional action parser with robust regex.\"\"\"\n keys = [\"tax\", \"jobs\", \"healthcare\", \"education\", \"infrastructure\"]\n\n def extract(key: str) -> float:\n m = re.search(rf\"{key}\\s*:\\s*(\\d*\\.?\\d+)\", text)\n if m:\n try:\n return float(np.clip(float(m.group(1)), 0.0, 1.0))\n except ValueError:\n pass\n return 0.5\n\n return {k: extract(k) for k in keys}\\n"
|
| 116 |
-
]
|
| 117 |
-
},
|
| 118 |
-
{
|
| 119 |
-
"cell_type": "markdown",
|
| 120 |
-
"metadata": {},
|
| 121 |
-
"source": [
|
| 122 |
-
"### CELL 7: LOAD MODEL WITH LoRA\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
|
| 123 |
-
]
|
| 124 |
-
},
|
| 125 |
-
{
|
| 126 |
-
"cell_type": "code",
|
| 127 |
-
"execution_count": null,
|
| 128 |
-
"metadata": {},
|
| 129 |
-
"outputs": [],
|
| 130 |
-
"source": [
|
| 131 |
-
"from transformers import AutoTokenizer, AutoModelForCausalLM\nfrom peft import LoraConfig, get_peft_model, TaskType\n\nMODEL_NAME = \"gpt2\" # swap to \"gpt2-medium\" or \"distilgpt2\" as needed\n\ntokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)\ntokenizer.pad_token = tokenizer.eos_token\ntokenizer.padding_side = \"left\"\n\nbase_model = AutoModelForCausalLM.from_pretrained(\n MODEL_NAME,\n torch_dtype = torch.bfloat16 if USE_BF16 else torch.float32,\n)\n\n# \u2500\u2500 Attach LoRA adapters (reduces trainable params by ~90%) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\nlora_cfg = LoraConfig(\n task_type = TaskType.CAUSAL_LM,\n r = 8, # rank\n lora_alpha = 32,\n target_modules = [\"c_attn\"], # GPT-2 attention projection\n lora_dropout = 0.05,\n bias = \"none\",\n)\nmodel = get_peft_model(base_model, lora_cfg)\nmodel.print_trainable_parameters()\nmodel = model.to(DEVICE)\n\nconsole.rule(\"[bold green]Model Ready\")\nconsole.log(f\"[cyan]Model : {MODEL_NAME} + LoRA (r=8)\")\nconsole.log(f\"[cyan]Parameters : {sum(p.numel() for p in model.parameters())/1e6:.1f}M total\")\\n"
|
| 132 |
-
]
|
| 133 |
-
},
|
| 134 |
-
{
|
| 135 |
-
"cell_type": "markdown",
|
| 136 |
-
"metadata": {},
|
| 137 |
-
"source": [
|
| 138 |
-
"### CELL 8: BUILD TRAINING DATASET FROM REAL DATA\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
|
| 139 |
-
]
|
| 140 |
-
},
|
| 141 |
-
{
|
| 142 |
-
"cell_type": "code",
|
| 143 |
-
"execution_count": null,
|
| 144 |
-
"metadata": {},
|
| 145 |
-
"outputs": [],
|
| 146 |
-
"source": [
|
| 147 |
-
"from datasets import Dataset\n\nNUM_SAMPLES = 300\nrecords = []\nenv_tmp = AdvancedCivicAIEnv(fetcher)\ntask_list = list(AdvancedCivicAIEnv.TASKS.keys())\ncountry_list= list(RealTimeDataFetcher.COUNTRIES.keys())\n\nfor i in range(NUM_SAMPLES):\n task = task_list[i % len(task_list)]\n country = country_list[i % len(country_list)]\n obs = env_tmp.reset(task, country)\n records.append({\n \"prompt\" : build_prompt(obs),\n \"task\" : task,\n \"country\" : country,\n })\n\ntrain_dataset = Dataset.from_list(records)\nconsole.log(f\"[green]\u2713 Dataset: {len(train_dataset)} prompts across \"\n f\"{len(task_list)} tasks \u00d7 {len(country_list)} countries\")\nconsole.log(f\" Sample:\\n",
|
| 148 |
-
"{train_dataset[0]['prompt'][:300]}...\")\\n"
|
| 149 |
-
]
|
| 150 |
-
},
|
| 151 |
-
{
|
| 152 |
-
"cell_type": "markdown",
|
| 153 |
-
"metadata": {},
|
| 154 |
-
"source": [
|
| 155 |
-
"### CELL 9: MULTI-OBJECTIVE REWARD FUNCTION\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
|
| 156 |
-
]
|
| 157 |
-
},
|
| 158 |
-
{
|
| 159 |
-
"cell_type": "code",
|
| 160 |
-
"execution_count": null,
|
| 161 |
-
"metadata": {},
|
| 162 |
-
"outputs": [],
|
| 163 |
-
"source": [
|
| 164 |
-
"def civic_reward_advanced(prompts, completions, task=None, country=None, **kwargs) -> List[float]:\n \"\"\"\n Multi-objective GRPO reward function.\n Scores: environment reward + format compliance + consistency bonus.\n \"\"\"\n rewards = []\n env_r = AdvancedCivicAIEnv(fetcher)\n task_list_ = task if isinstance(task, list) else [task] * len(prompts)\n country_ = country if isinstance(country,list) else [country]* len(prompts)\n\n for i, (prompt, completion) in enumerate(zip(prompts, completions)):\n # Extract text\n if isinstance(completion, list) and len(completion) > 0:\n text = completion[0].get(\"content\", \"\")\n else:\n text = str(completion)\n\n action = parse_action(text)\n\n # Environment reward\n t = (task_list_[i] if task_list_[i] else \"maximize_wellbeing\")\n c = (country_[i] if country_[i] else \"USA\")\n env_r.reset(t, c)\n _, env_rew, _, _ = env_r.step(action)\n\n # Format reward: all 5 keys present\n keys_found = sum(\n 1 for k in [\"tax\",\"jobs\",\"healthcare\",\"education\",\"infrastructure\"]\n if re.search(rf\"{k}\\s*:\\s*\\d\", text)\n )\n fmt_bonus = (keys_found / 5.0) * 0.15 # up to +0.15\n\n # Diversity bonus: penalise all-same values (lazy policy)\n vals = list(action.values())\n div_bonus = float(np.std(vals)) * 0.1 # up to ~+0.05\n\n total = float(env_rew) + fmt_bonus + div_bonus\n rewards.append(round(total, 5))\n\n return rewards\\n"
|
| 165 |
-
]
|
| 166 |
-
},
|
| 167 |
-
{
|
| 168 |
-
"cell_type": "markdown",
|
| 169 |
-
"metadata": {},
|
| 170 |
-
"source": [
|
| 171 |
-
"### CELL 10: GRPO CONFIG (VERSION-SAFE)\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
|
| 172 |
-
]
|
| 173 |
-
},
|
| 174 |
-
{
|
| 175 |
-
"cell_type": "code",
|
| 176 |
-
"execution_count": null,
|
| 177 |
-
"metadata": {},
|
| 178 |
-
"outputs": [],
|
| 179 |
-
"source": [
|
| 180 |
-
"from trl import GRPOConfig, GRPOTrainer\n\nvalid_params = set(inspect.signature(GRPOConfig.__init__).parameters)\n\nall_kwargs = {\n \"output_dir\" : \"checkpoints/civicai-grpo\",\n \"num_train_epochs\" : 3,\n \"per_device_train_batch_size\" : 2,\n \"num_generations\" : 2,\n \"max_prompt_length\" : 300,\n \"max_completion_length\" : 80,\n \"learning_rate\" : 5e-6,\n \"logging_steps\" : 5,\n \"save_strategy\" : \"epoch\",\n \"save_total_limit\" : 2,\n \"report_to\" : \"none\",\n \"remove_unused_columns\" : False,\n \"bf16\" : USE_BF16,\n \"fp16\" : USE_FP16,\n \"gradient_accumulation_steps\" : 4,\n \"max_grad_norm\" : 0.3,\n \"warmup_ratio\" : 0.05,\n \"lr_scheduler_type\" : \"cosine\",\n \"dataloader_num_workers\" : 0,\n}\n\nsafe_kwargs = {k: v for k, v in all_kwargs.items() if k in valid_params}\nskipped = set(all_kwargs) - set(safe_kwargs)\nif skipped:\n console.log(f\"[yellow]Skipped unsupported GRPOConfig args: {skipped}\")\n\ngrpo_config = GRPOConfig(**safe_kwargs)\n\ntrainer = GRPOTrainer(\n model = model,\n args = grpo_config,\n reward_funcs = civic_reward_advanced,\n train_dataset = train_dataset,\n processing_class = tokenizer,\n)\nconsole.log(\"[green]\u2713 GRPOTrainer initialised with LoRA + multi-objective reward\")\\n"
|
| 181 |
-
]
|
| 182 |
-
},
|
| 183 |
-
{
|
| 184 |
-
"cell_type": "markdown",
|
| 185 |
-
"metadata": {},
|
| 186 |
-
"source": [
|
| 187 |
-
"### CELL 11: TRAINING\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
|
| 188 |
-
]
|
| 189 |
-
},
|
| 190 |
-
{
|
| 191 |
-
"cell_type": "code",
|
| 192 |
-
"execution_count": null,
|
| 193 |
-
"metadata": {},
|
| 194 |
-
"outputs": [],
|
| 195 |
-
"source": [
|
| 196 |
-
"console.rule(\"[bold cyan]Starting GRPO Training\")\nstart_time = time.time()\ntrainer.train()\nelapsed = time.time() - start_time\nconsole.rule(f\"[bold green]Training Complete \u2014 {elapsed/60:.1f} min\")\\n"
|
| 197 |
-
]
|
| 198 |
-
},
|
| 199 |
-
{
|
| 200 |
-
"cell_type": "markdown",
|
| 201 |
-
"metadata": {},
|
| 202 |
-
"source": [
|
| 203 |
-
"### CELL 12: EXTRACT & PLOT TRAINING METRICS\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
|
| 204 |
-
]
|
| 205 |
-
},
|
| 206 |
-
{
|
| 207 |
-
"cell_type": "code",
|
| 208 |
-
"execution_count": null,
|
| 209 |
-
"metadata": {},
|
| 210 |
-
"outputs": [],
|
| 211 |
-
"source": [
|
| 212 |
-
"logs = trainer.state.log_history\ndf_logs = pd.DataFrame(logs).dropna(subset=[\"loss\"] if \"loss\" in pd.DataFrame(logs).columns else [])\n\nreward_entries = [e for e in logs if \"reward\" in e]\nrewards_logged = [e[\"reward\"] for e in reward_entries]\nsteps_logged = [e.get(\"step\", i) for i, e in enumerate(reward_entries)]\n\nfig = make_subplots(rows=1, cols=2,\n subplot_titles=(\"Reward Curve\", \"Reward Distribution\"))\n\n# Reward over steps\nfig.add_trace(go.Scatter(\n x=steps_logged, y=rewards_logged,\n mode=\"lines\", name=\"Reward\", line=dict(color=\"#00d4ff\", width=2)\n), row=1, col=1)\n\n# Smoothed\nif len(rewards_logged) > 5:\n smooth = np.convolve(rewards_logged, np.ones(5)/5, mode=\"valid\")\n fig.add_trace(go.Scatter(\n x=steps_logged[4:], y=smooth,\n mode=\"lines\", name=\"Smoothed\",\n line=dict(color=\"#ff6b6b\", width=2, dash=\"dash\")\n ), row=1, col=1)\n\n# Histogram\nfig.add_trace(go.Histogram(\n x=rewards_logged, nbinsx=20,\n marker_color=\"#00d4ff\", opacity=0.75, name=\"Distribution\"\n), row=1, col=2)\n\nfig.update_layout(\n title=\"CivicAI GRPO Training Metrics\",\n template=\"plotly_dark\", height=420,\n paper_bgcolor=\"#0d1117\", font=dict(color=\"#e6edf3\"),\n)\nfig.show()\nfig.write_html(\"assets/training_metrics.html\")\n\nif rewards_logged:\n console.print(f\"[cyan]Start reward : {rewards_logged[0]:.4f}\")\n console.print(f\"[cyan]Final reward : {rewards_logged[-1]:.4f}\")\n console.print(f\"[green]Improvement : {rewards_logged[-1]-rewards_logged[0]:+.4f}\")\\n"
|
| 213 |
-
]
|
| 214 |
-
},
|
| 215 |
-
{
|
| 216 |
-
"cell_type": "markdown",
|
| 217 |
-
"metadata": {},
|
| 218 |
-
"source": [
|
| 219 |
-
"### CELL 13: MULTI-COUNTRY POLICY EVALUATION\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
|
| 220 |
-
]
|
| 221 |
-
},
|
| 222 |
-
{
|
| 223 |
-
"cell_type": "code",
|
| 224 |
-
"execution_count": null,
|
| 225 |
-
"metadata": {},
|
| 226 |
-
"outputs": [],
|
| 227 |
-
"source": [
|
| 228 |
-
"def evaluate_trained_policy(\n model, tokenizer, fetcher,\n countries: List[str] = None,\n tasks: List[str] = None,\n episodes: int = 5,\n) -> pd.DataFrame:\n \"\"\"Evaluate trained policy on all countries \u00d7 all tasks.\"\"\"\n countries = countries or list(RealTimeDataFetcher.COUNTRIES.keys())\n tasks = tasks or list(AdvancedCivicAIEnv.TASKS.keys())\n model.eval()\n results = []\n\n for country in countries:\n for task in tasks:\n ep_rewards = []\n env_eval = AdvancedCivicAIEnv(fetcher, default_country=country)\n\n for _ in range(episodes):\n obs = env_eval.reset(task, country)\n ep_reward = 0.0\n for _ in range(20):\n prompt = build_prompt(obs)\n inputs = tokenizer(prompt, return_tensors=\"pt\",\n truncation=True, max_length=300).to(DEVICE)\n with torch.no_grad():\n out = model.generate(\n **inputs, max_new_tokens=60,\n do_sample=False,\n pad_token_id=tokenizer.eos_token_id,\n )\n gen_tokens = out[0][inputs[\"input_ids\"].shape[1]:]\n text = tokenizer.decode(gen_tokens, skip_special_tokens=True)\n action = parse_action(text)\n obs, r, done, _ = env_eval.step(action)\n ep_reward += r\n if done: break\n ep_rewards.append(ep_reward / 20)\n\n results.append({\n \"country\" : RealTimeDataFetcher.COUNTRIES[country][\"name\"],\n \"task\" : task,\n \"mean_r\" : round(float(np.mean(ep_rewards)), 4),\n \"std_r\" : round(float(np.std(ep_rewards)), 4),\n \"max_r\" : round(float(np.max(ep_rewards)), 4),\n })\n console.log(f\"[dim]{country} / {task} \u2192 {results[-1]['mean_r']:.4f}\")\n\n return pd.DataFrame(results)\n\n\ndef baseline_score(fetcher, episodes=5):\n \"\"\"Fixed 0.5 policy baseline.\"\"\"\n env_b, total = AdvancedCivicAIEnv(fetcher, \"USA\"), []\n for _ in range(episodes):\n obs = env_b.reset(\"maximize_wellbeing\", \"USA\")\n r = 0.0\n for _ in range(20):\n obs, rew, done, _ = env_b.step(\n {k: 0.5 for k in [\"tax\",\"jobs\",\"healthcare\",\"education\",\"infrastructure\"]}\n )\n r += rew\n total.append(r / 20)\n return float(np.mean(total))\n\n\nconsole.rule(\"[bold cyan]Evaluating Policy Across Countries & Tasks\")\ndf_eval = evaluate_trained_policy(model, tokenizer, fetcher, episodes=3)\nbaseline = baseline_score(fetcher)\n\nconsole.print(df_eval.to_string(index=False))\nconsole.print(f\"\\n",
|
| 229 |
-
"[bold]Baseline (fixed 0.5) : {baseline:.4f}\")\nconsole.print(f\"[bold green]Best trained score : {df_eval['mean_r'].max():.4f}\")\\n"
|
| 230 |
-
]
|
| 231 |
-
},
|
| 232 |
-
{
|
| 233 |
-
"cell_type": "markdown",
|
| 234 |
-
"metadata": {},
|
| 235 |
-
"source": [
|
| 236 |
-
"### CELL 14: EVALUATION HEATMAP\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
|
| 237 |
-
]
|
| 238 |
-
},
|
| 239 |
-
{
|
| 240 |
-
"cell_type": "code",
|
| 241 |
-
"execution_count": null,
|
| 242 |
-
"metadata": {},
|
| 243 |
-
"outputs": [],
|
| 244 |
-
"source": [
|
| 245 |
-
"pivot = df_eval.pivot(index=\"country\", columns=\"task\", values=\"mean_r\")\n\nfig_heat = go.Figure(go.Heatmap(\n z = pivot.values,\n x = pivot.columns.tolist(),\n y = pivot.index.tolist(),\n colorscale = \"RdYlGn\",\n text = np.round(pivot.values, 3),\n texttemplate=\"%{text}\",\n showscale = True,\n zmin=0.4, zmax=1.0,\n))\nfig_heat.add_shape(\n type=\"line\", x0=-0.5, x1=len(pivot.columns)-0.5,\n y0=-0.5, y1=len(pivot.index)-0.5,\n line=dict(color=\"white\", width=0)\n)\nfig_heat.update_layout(\n title = \"Policy Performance Heatmap \u2014 Country \u00d7 Task (GRPO Trained)\",\n template=\"plotly_dark\", height=400,\n paper_bgcolor=\"#0d1117\", font=dict(color=\"#e6edf3\"),\n xaxis_title=\"Task\", yaxis_title=\"Country\",\n)\nfig_heat.show()\nfig_heat.write_html(\"assets/eval_heatmap.html\")\\n"
|
| 246 |
-
]
|
| 247 |
-
},
|
| 248 |
-
{
|
| 249 |
-
"cell_type": "markdown",
|
| 250 |
-
"metadata": {},
|
| 251 |
-
"source": [
|
| 252 |
-
"### CELL 15: SAVE EVERYTHING\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
|
| 253 |
-
]
|
| 254 |
-
},
|
| 255 |
-
{
|
| 256 |
-
"cell_type": "code",
|
| 257 |
-
"execution_count": null,
|
| 258 |
-
"metadata": {},
|
| 259 |
-
"outputs": [],
|
| 260 |
-
"source": [
|
| 261 |
-
"# Save LoRA adapter only (lightweight)\nmodel.save_pretrained(\"checkpoints/civicai-lora\")\ntokenizer.save_pretrained(\"checkpoints/civicai-lora\")\n\n# Save results JSON\nresults_json = {\n \"run_timestamp\" : datetime.now().isoformat(),\n \"model\" : MODEL_NAME,\n \"lora_rank\" : 8,\n \"training_epochs\": 3,\n \"num_countries\" : len(RealTimeDataFetcher.COUNTRIES),\n \"num_tasks\" : len(AdvancedCivicAIEnv.TASKS),\n \"data_source\" : \"World Bank Open API (live)\",\n \"baseline_reward\": round(baseline, 4),\n \"best_reward\" : round(float(df_eval[\"mean_r\"].max()), 4),\n \"improvement\" : round(float(df_eval[\"mean_r\"].max()) - baseline, 4),\n \"reward_history\" : rewards_logged,\n \"eval_by_country_task\": df_eval.to_dict(orient=\"records\"),\n \"real_data_snapshot\" : df_world[[\"country\",\"inflation_pct\",\"unemployment_pct\",\n \"health_exp_gdp\",\"gdp_growth\"]].to_dict(orient=\"records\"),\n}\n\nwith open(\"assets/training_results.json\", \"w\") as f:\n json.dump(results_json, f, indent=2)\n\nconsole.rule(\"[bold green]All Done\")\nconsole.print(f\"[green]\u2713 LoRA checkpoint \u2192 checkpoints/civicai-lora/\")\nconsole.print(f\"[green]\u2713 Results JSON \u2192 assets/training_results.json\")\nconsole.print(f\"[green]\u2713 Dashboard HTML \u2192 assets/global_dashboard.html\")\nconsole.print(f\"[green]\u2713 Training metrics \u2192 assets/training_metrics.html\")\nconsole.print(f\"[green]\u2713 Eval heatmap \u2192 assets/eval_heatmap.html\")\nconsole.print(f\"\\n",
|
| 262 |
-
"[bold cyan]Baseline : {baseline:.4f}\")\nconsole.print(f\"[bold green]Best score: {df_eval['mean_r'].max():.4f}\")\nconsole.print(f\"[bold green]Delta : {df_eval['mean_r'].max() - baseline:+.4f}\")\\n"
|
| 263 |
-
]
|
| 264 |
-
}
|
| 265 |
-
],
|
| 266 |
-
"metadata": {
|
| 267 |
-
"colab": {
|
| 268 |
-
"name": "CivicAI_Training.ipynb"
|
| 269 |
-
},
|
| 270 |
-
"kernelspec": {
|
| 271 |
-
"display_name": "Python 3",
|
| 272 |
-
"language": "python",
|
| 273 |
-
"name": "python3"
|
| 274 |
-
},
|
| 275 |
-
"language_info": {
|
| 276 |
-
"name": "python",
|
| 277 |
-
"version": "3.10"
|
| 278 |
-
}
|
| 279 |
-
},
|
| 280 |
-
"nbformat": 4,
|
| 281 |
-
"nbformat_minor": 4
|
| 282 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Training.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
build_notebook.py
CHANGED
|
@@ -64,6 +64,8 @@ else:
|
|
| 64 |
|
| 65 |
DEVICE = "cuda" if CUDA_OK else "cpu"
|
| 66 |
|
|
|
|
|
|
|
| 67 |
# ββ Paths βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 68 |
Path("assets").mkdir(exist_ok=True)
|
| 69 |
Path("checkpoints").mkdir(exist_ok=True)
|
|
|
|
| 64 |
|
| 65 |
DEVICE = "cuda" if CUDA_OK else "cpu"
|
| 66 |
|
| 67 |
+
|
| 68 |
+
|
| 69 |
# ββ Paths βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 70 |
Path("assets").mkdir(exist_ok=True)
|
| 71 |
Path("checkpoints").mkdir(exist_ok=True)
|