Spaces:
Sleeping
Sleeping
Readme updated82
Browse files
README.md
CHANGED
|
@@ -1,96 +1,160 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
from src.disaster_grid.rewards import compute_reward
|
| 20 |
-
|
| 21 |
-
# ββ 2. LOAD MODEL & TOKENIZER ββ
|
| 22 |
-
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 23 |
-
model_name = "unsloth/llama-3-8b-Instruct-bnb-4bit",
|
| 24 |
-
max_seq_length = 1024,
|
| 25 |
-
load_in_4bit = True,
|
| 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 |
trainer = GRPOTrainer(
|
| 64 |
-
model
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
train_dataset = dataset,
|
| 68 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
print("π Starting High-Speed GRPO Training...")
|
| 72 |
-
try:
|
| 73 |
-
trainer.train()
|
| 74 |
-
finally:
|
| 75 |
-
# Plotting logic for your final presentation
|
| 76 |
-
history = trainer.state.log_history
|
| 77 |
-
steps = [log["step"] for log in history if "reward" in log]
|
| 78 |
-
rewards = [log["reward"] for log in history if "reward" in log]
|
| 79 |
-
|
| 80 |
-
if steps:
|
| 81 |
-
plt.figure(figsize=(10, 5))
|
| 82 |
-
plt.plot(steps, rewards, marker='o', color='#10b981', linewidth=2)
|
| 83 |
-
plt.title('Agent Learning Curve (GRPO Reward over Time)', color='white')
|
| 84 |
-
plt.xlabel('Steps', color='gray')
|
| 85 |
-
plt.ylabel('Average Reward', color='gray')
|
| 86 |
-
plt.gca().set_facecolor('#1e1e1e')
|
| 87 |
-
plt.gcf().patch.set_facecolor('#1e1e1e')
|
| 88 |
-
plt.savefig('learning_curve.png', bbox_inches='tight', dpi=300)
|
| 89 |
-
plt.show()
|
| 90 |
-
print("β
Graph saved as 'learning_curve.png'")
|
| 91 |
-
|
| 92 |
-
# ββ 7. SAFE SAVING (Avoiding Mergekit Errors) ββ
|
| 93 |
-
# We save as LoRA adapters to avoid needing mergekit
|
| 94 |
-
model.save_pretrained("disaster_agent_lora")
|
| 95 |
-
tokenizer.save_pretrained("disaster_agent_lora")
|
| 96 |
-
print("β
Training complete. Brain saved to 'disaster_agent_lora'.")2399
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: AI-Kinetic Disaster Recovery Grid
|
| 3 |
+
emoji: ποΈ
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: yellow
|
| 6 |
+
sdk: docker
|
| 7 |
+
app_file: app.py
|
| 8 |
+
pinned: true
|
| 9 |
+
license: mit
|
| 10 |
+
short_description: Multi-Agent RL crisis-grid recovery on OpenEnv
|
| 11 |
+
tags:
|
| 12 |
+
- reinforcement-learning
|
| 13 |
+
- multi-agent
|
| 14 |
+
- openenv
|
| 15 |
+
- pytorch
|
| 16 |
+
- llm-agents
|
| 17 |
+
- hackathon
|
| 18 |
+
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# ποΈ AI-Kinetic Disaster Recovery Grid
|
| 21 |
+
|
| 22 |
+
> A Multi-Agent Reinforcement Learning environment built on **OpenEnv** for the **Meta PyTorch Γ Scaler OpenEnv Hackathon**.
|
| 23 |
+
|
| 24 |
+
---
|
| 25 |
+
|
| 26 |
+
## π¨ Problem Statement
|
| 27 |
+
|
| 28 |
+
Modern Large Language Models excel at conversational tasks and static reasoning, but they fail catastrophically at **real-time resource management** and **long-horizon planning** during active crises.
|
| 29 |
+
|
| 30 |
+
When a disaster strikes β a solar flare, cyber-attack, or severe weather event β infrastructure grids suffer cascading failures. In these scenarios:
|
| 31 |
+
|
| 32 |
+
- **Information is sparse.** Communication lines sever, creating partial observability.
|
| 33 |
+
- **Resources are finite.** Repairing one sector may mean abandoning another.
|
| 34 |
+
- **Rewards are delayed.** Actions taken now (gathering tools) may not yield results until much later.
|
| 35 |
+
|
| 36 |
+
Traditional centralized AI models assume perfect information and unlimited bandwidth. Placed in a constrained, decaying environment, they collapse.
|
| 37 |
+
|
| 38 |
+
---
|
| 39 |
+
|
| 40 |
+
## π‘ The Proposed Solution
|
| 41 |
+
|
| 42 |
+
We built the **AI-Kinetic Disaster Recovery Grid** β a Multi-Agent Reinforcement Learning (MARL) environment. Instead of a single "Omni-Agent," we deployed a distributed team of specialized agents acting on a dynamic 5Γ5 city grid.
|
| 43 |
+
|
| 44 |
+
### Agent Roles
|
| 45 |
+
|
| 46 |
+
| Agent | Role |
|
| 47 |
+
|---|---|
|
| 48 |
+
| π οΈ **Scavenger** | Navigates the grid to collect scarce battery packs and repair parts. |
|
| 49 |
+
| βοΈ **Engineer** | Executes complex, multi-step repair sequences. |
|
| 50 |
+
| π‘ **Dispatcher** | Holds the global map and intelligently guides the field agents. |
|
| 51 |
+
|
| 52 |
+
---
|
| 53 |
+
|
| 54 |
+
## βοΈ Workflow
|
| 55 |
+
|
| 56 |
+
### 1. Environment Simulation (OpenEnv)
|
| 57 |
+
|
| 58 |
+
The world is modeled as a 5Γ5 matrix using `openenv-core`. Each sector tracks internal state: **Health**, **Power**, and **Risk**.
|
| 59 |
+
|
| 60 |
+

|
| 61 |
+
*VS Code showing `environment.py` defining the `CityGrid` class and episode dynamics.*
|
| 62 |
+
|
| 63 |
+
### 2. Training Scripts (GRPO + Unsloth)
|
| 64 |
+
|
| 65 |
+
We used **Group Relative Policy Optimization (GRPO)**, accelerated with **Unsloth** to manage memory constraints during training.
|
| 66 |
+
|
| 67 |
+
```python
|
| 68 |
+
from openenv import OpenEnvGrid
|
| 69 |
+
from trl import GRPOTrainer
|
| 70 |
+
|
| 71 |
+
env = OpenEnvGrid(size=5, agents=["scavenger", "engineer", "dispatcher"])
|
| 72 |
|
| 73 |
trainer = GRPOTrainer(
|
| 74 |
+
model="meta-llama/Llama-3-8B",
|
| 75 |
+
args=config,
|
| 76 |
+
train_dataset=env.get_training_data(),
|
|
|
|
| 77 |
)
|
| 78 |
+
trainer.train()
|
| 79 |
+
```
|
| 80 |
+
|
| 81 |
+
---
|
| 82 |
+
|
| 83 |
+
## π§ͺ Validation
|
| 84 |
+
|
| 85 |
+
### Local Validation & Execution
|
| 86 |
+
|
| 87 |
+
Before pushing to the Hugging Face Space, we validate agent logic and entropy sequences via a local CLI built on `openenv-core` for rapid debugging of the 5Γ5 grid state transitions.
|
| 88 |
+
|
| 89 |
+
<video controls src="./assets/validation_video.mp4" width="100%"></video>
|
| 90 |
+
|
| 91 |
+

|
| 92 |
+
*Local terminal simulator showing a completed 50-step sequence tracking City Health and Energy metrics.*
|
| 93 |
+
|
| 94 |
+
### Live Agent Simulation (Operational Theatre)
|
| 95 |
+
|
| 96 |
+
To visualize multi-agent orchestration, we built a web-based UI β the **Operational Theatre** β deployed on Hugging Face Spaces. It allows random anomaly injection while observing LLM-powered agents respond in real time.
|
| 97 |
+
|
| 98 |
+
<video controls src="./assets/operational_theatre.mp4" width="100%"></video>
|
| 99 |
+
*HF Space UI showing the autonomous agent navigating the grid with the Agent Brain Log streaming reasoning.*
|
| 100 |
+
|
| 101 |
+
---
|
| 102 |
+
|
| 103 |
+
## π Real-World Applications
|
| 104 |
+
|
| 105 |
+
- **Autonomous Smart Grid Management** β Dynamically rerouting power around physical failures to prevent cascading blackouts.
|
| 106 |
+
- **Search and Rescue Drone Swarms** β Coordinating ground and air drones in disaster zones with limited battery life.
|
| 107 |
+
- **Self-Healing Server Networks** β Autonomously rerouting global traffic and deploying patches during cyber-attacks.
|
| 108 |
+
|
| 109 |
+
---
|
| 110 |
+
|
| 111 |
+
## βοΈ Advantages & Trade-offs
|
| 112 |
+
|
| 113 |
+
### β
Advantages
|
| 114 |
+
|
| 115 |
+
- **Task Specialization** β Specialized roles converge faster than a single omni-agent.
|
| 116 |
+
- **Fault Tolerance** β No single point of failure; the Dispatcher reroutes around incapacitated agents.
|
| 117 |
+
- **Realistic Observability** β Native "fog of war," forcing agents to communicate to gain a global view.
|
| 118 |
+
|
| 119 |
+
### β οΈ Trade-offs
|
| 120 |
+
|
| 121 |
+
- **Credit Assignment** β Hard to reward early strategic moves whose payoff arrives many steps later.
|
| 122 |
+
- **Non-Stationarity** β Other agents act simultaneously, making convergence non-trivial.
|
| 123 |
+
|
| 124 |
+
---
|
| 125 |
+
|
| 126 |
+
## π Scalability
|
| 127 |
+
|
| 128 |
+
The architecture scales via **Hierarchical Multi-Agent Systems (HMAS)**. In a city-wide deployment, this 5Γ5 grid represents a single *Neighborhood*. Each Neighborhood Dispatcher reports to a higher-level **Zone Commander**, allowing the system to manage thousands of nodes without overwhelming any individual agent's context window.
|
| 129 |
+
|
| 130 |
+
---
|
| 131 |
+
|
| 132 |
+
## π Run It Locally
|
| 133 |
+
|
| 134 |
+
```bash
|
| 135 |
+
git clone https://huggingface.co/spaces/<your-username>/disaster-grid
|
| 136 |
+
cd disaster-grid
|
| 137 |
+
|
| 138 |
+
python -m venv .venv
|
| 139 |
+
source .venv/bin/activate # or .venv\Scripts\Activate.ps1 on Windows
|
| 140 |
+
|
| 141 |
+
pip install -e .
|
| 142 |
+
|
| 143 |
+
# Run the local CLI simulator
|
| 144 |
+
python -m src.disaster_grid.utils
|
| 145 |
+
|
| 146 |
+
# Or launch the operational theatre
|
| 147 |
+
python app.py
|
| 148 |
+
```
|
| 149 |
+
|
| 150 |
+
**Controls in CLI mode:** `W/A/S/D` to move Β· `R` to repair Β· `C` to recharge Β· `Q` to wait Β· `EXIT` to terminate.
|
| 151 |
+
|
| 152 |
+
---
|
| 153 |
+
|
| 154 |
+
## π Conclusion
|
| 155 |
+
|
| 156 |
+
The **AI-Kinetic Disaster Recovery Grid** demonstrates that LLMs can transition from *strategic chatting* to **strategic action**. By implementing multi-agent coordination inside a decaying environment, we are engineering the autonomous emergency responders of the future.
|
| 157 |
+
|
| 158 |
+
---
|
| 159 |
|
| 160 |
+
*Developed by **Sanjith P** and **Varsha D** for the Meta PyTorch Γ Scaler OpenEnv Hackathon.*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|