File size: 12,276 Bytes
71a6edd
495526b
 
 
 
4b93901
 
 
 
495526b
4b93901
 
 
495526b
 
4b93901
495526b
4b93901
 
495526b
 
 
 
4b93901
 
495526b
4b93901
495526b
 
 
71a6edd
495526b
 
 
51f7c12
495526b
 
 
51f7c12
4b93901
495526b
 
 
e5fb395
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b93901
495526b
 
 
4b93901
 
 
 
495526b
4b93901
495526b
 
 
 
 
 
 
 
 
4b93901
 
 
 
 
 
 
495526b
 
 
4b93901
495526b
4b93901
 
 
 
 
 
 
 
 
 
 
 
 
495526b
 
 
4b93901
495526b
4b93901
495526b
4b93901
51f7c12
4b93901
 
495526b
4b93901
 
 
 
 
 
 
495526b
4b93901
 
 
 
 
 
495526b
 
4b93901
 
495526b
 
4b93901
495526b
 
4b93901
495526b
 
4b93901
 
 
 
495526b
 
4b93901
 
 
495526b
 
4b93901
 
 
495526b
 
4b93901
495526b
 
4b93901
 
 
495526b
 
4b93901
 
 
 
 
495526b
 
 
 
4b93901
495526b
4b93901
495526b
4b93901
 
 
 
 
495526b
4b93901
495526b
4b93901
 
 
 
 
495526b
4b93901
495526b
4b93901
 
 
495526b
4b93901
495526b
 
4b93901
 
 
 
 
495526b
 
4b93901
495526b
 
4b93901
 
 
495526b
 
 
 
4b93901
495526b
4b93901
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
495526b
 
4b93901
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
495526b
 
 
4b93901
495526b
4b93901
 
 
 
495526b
4b93901
 
 
495526b
4b93901
495526b
 
 
4b93901
495526b
4b93901
 
 
 
 
495526b
 
 
4b93901
495526b
4b93901
 
 
 
495526b
 
 
 
 
4b93901
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
---
language:
  - en
  - ne
  - es
  - fr
  - ar
  - hi
  - te
  - zh
  - ja
  - ko
  - pt
license: llama3
library_name: transformers
base_model: meta-llama/Llama-3.1-8B-Instruct
tags:
  - disaster-management
  - emergency-response
  - humanitarian-ai
  - multilingual
  - fine-tuned
  - qlora
  - lora
  - peft
  - llama3
pipeline_tag: text-generation
model-index:
  - name: WorldDisasterLM-8B
    results: []
---

# WorldDisasterLM-8B

> **Open-source AI foundation model for global disaster intelligence, emergency response, and humanitarian aid — supporting 11 languages including Nepali.**

[![Python 3.11+](https://img.shields.io/badge/python-3.11+-blue.svg)](https://python.org)
[![License](https://img.shields.io/badge/license-Llama3-green.svg)](https://ai.meta.com/llama/license/)
[![HuggingFace](https://img.shields.io/badge/🤗-WorldDisasterLM--8B-yellow)](https://huggingface.co/drdeveloper88/WorldDisasterLM-8B)
[![Tests](https://img.shields.io/badge/tests-9%20passed-brightgreen.svg)]()

---

## How to Use

```python
# pip install transformers accelerate bitsandbytes peft
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
import torch

model_id = "drdeveloper88/WorldDisasterLM-8B"

# Load with 4-bit NF4 quantization (requires ~6 GB VRAM)
bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.bfloat16,
    bnb_4bit_use_double_quant=True,
)

tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    quantization_config=bnb_config,
    device_map="auto",
)

# Ask a disaster question in any of the 11 supported languages
messages = [
    {
        "role": "user",
        "content": "What immediate steps should I take during a major earthquake? I am in Kathmandu, Nepal."
    }
]

input_ids = tokenizer.apply_chat_template(
    messages,
    tokenize=True,
    add_generation_prompt=True,
    return_tensors="pt",
).to(model.device)

gen_tokens = model.generate(
    input_ids,
    max_new_tokens=512,
    do_sample=True,
    temperature=0.7,
    top_p=0.9,
    repetition_penalty=1.1,
)

response = tokenizer.decode(gen_tokens[0][input_ids.shape[-1]:], skip_special_tokens=True)
print(response)
```

### Multilingual example (Nepali)

```python
messages = [{"role": "user", "content": "भूकम्पको बेला के गर्ने? म काठमाडौंमा छु।"}]
# (What to do during an earthquake? I am in Kathmandu.)
```

### CLI inference

```bash
python inference.py --prompt "What to do during a flood?" --language English --region Nepal
python inference.py --prompt "भूकम्पको बेला के गर्ने?" --language Nepali --region Nepal
```

> **Note:** Model weights (`.safetensors`) are generated after training completes.  
> Run `python train.py` on a GPU (A100/V100 recommended) to produce the weights, then push via `scripts/push_to_hub.py`.

---

## What is WorldDisasterLM-8B?

**WorldDisasterLM-8B** is a production-grade, domain-specialized large language model fine-tuned on top of `meta-llama/Llama-3.1-8B-Instruct` using **QLoRA** (4-bit NF4 quantization, LoRA r=16). It is purpose-built to assist:

- 🌍 **Emergency responders** — real-time disaster action guidance
- 🏥 **Humanitarian aid workers** — resource allocation and triage support
- 🏛️ **Government agencies** — risk assessment and crisis intelligence
- 🌐 **Global communities** — multilingual disaster preparedness in 11 languages

Training data is collected **live** from six free public APIs: ReliefWeb, USGS Earthquake, GDACS, NOAA Weather, OpenFEMA, and WHO — with automated QA amplification generating 8 instruction variants per disaster record.

---

## Key Features

| Feature | Detail |
|---|---|
| **Base model** | `meta-llama/Llama-3.1-8B-Instruct` |
| **Fine-tuning** | QLoRA — 4-bit NF4, LoRA r=16, all attn+MLP projectors |
| **Languages** | 11: English, Nepali (नेपाली), Spanish, French, Arabic, Hindi, Telugu, Chinese, Japanese, Korean, Portuguese |
| **Data sources** | ReliefWeb, USGS, GDACS, NOAA, OpenFEMA, WHO |
| **Dataset size** | 88+ live records → 711+ instruction samples per run |
| **API** | FastAPI REST + Server-Sent Events streaming |
| **Frontend** | React 18 + Vite disaster analytics dashboard |
| **Export** | GGUF (llama.cpp) + ONNX formats |
| **HuggingFace** | Auto-push via `scripts/push_to_hub.py` |

---

## Supported Languages

| Language | Code | Script |
|---|---|---|
| English | `en` | Latin |
| **Nepali** | `ne` | **Devanagari (नेपाली)** |
| Spanish | `es` | Latin |
| French | `fr` | Latin |
| Arabic | `ar` | Arabic |
| Hindi | `hi` | Devanagari |
| Telugu | `te` | Telugu |
| Chinese | `zh` | Hanzi |
| Japanese | `ja` | Kanji/Hiragana |
| Korean | `ko` | Hangul |
| Portuguese | `pt` | Latin |

---

## Quick Start

### 1. Clone and install

```bash
git clone https://huggingface.co/drdeveloper88/WorldDisasterLM-8B worlddisasterllm
cd worlddisasterllm
python -m venv .venv

# Windows
.venv\Scripts\activate
# macOS/Linux
source .venv/bin/activate

pip install -r requirements.txt
```

> **Windows note:** If your path has spaces, always set:
> ```powershell
> $env:PYTHONPATH = "C:\path\to\worlddisasterllm"
> ```

### 2. Configure environment

```bash
cp .env.example .env
# Add HF_TOKEN for HuggingFace publishing (optional)
```

### 3. Run the API backend

```bash
uvicorn backend.app.main:app --reload --port 8000
```

API available at: `http://localhost:8000`  
Interactive docs: `http://localhost:8000/docs`

### 4. Run the React dashboard

```bash
cd frontend
npm install
npm run dev
```

Dashboard at: `http://localhost:5173`

### 5. Run the Gradio demo

```bash
python app.py
```

Demo at: `http://localhost:7860`

### 6. Run CLI inference

```bash
# English
python inference.py --prompt "What to do during a flood?" --language English --region Nepal

# Nepali
python inference.py --prompt "भूकम्पको बेला के गर्ने?" --language Nepali --region Nepal
```

---

## Training Pipeline

### Step 1 — Collect live data

```bash
python scripts/collect_data.py
# Collects from: ReliefWeb, USGS, GDACS, NOAA, OpenFEMA, WHO
# Output: data/processed/instruction_dataset.jsonl
```

### Step 2 — Fine-tune with QLoRA

```bash
python train.py --output checkpoints/worlddisasterlm-8b-qlora
# Requires GPU (A100/V100 recommended)
# Uses: 4-bit NF4 quantization + LoRA r=16 + SFTTrainer
```

### Step 3 — Evaluate

```bash
python evaluate.py
```

### Step 4 — Push to HuggingFace

```bash
export HF_TOKEN=hf_xxxx   # or $env:HF_TOKEN on Windows
python scripts/push_to_hub.py \
    --adapter checkpoints/worlddisasterlm-8b-qlora \
    --base-model meta-llama/Llama-3.1-8B-Instruct \
    --repo-id YourUsername/WorldDisasterLM-8B
```

### Step 5 — Export to GGUF (for llama.cpp / Ollama)

```bash
python scripts/export_gguf.py \
    --model-path checkpoints/worlddisasterlm-8b-qlora \
    --output-path artifacts/worlddisasterlm-8b.gguf
```

---

## API Reference

### Health check
```
GET /health
→ { "status": "ok", "model": "WorldDisasterLM-8B" }
```

### Disaster chat
```
POST /v1/chat
{
  "messages": [{"role": "user", "content": "What to do during earthquake?"}],
  "language": "Nepali",
  "region": "Nepal"
}
→ { "answer": "[WorldDisasterLM-8B | नेपाली | Nepal] ...", "confidence": 0.74, ... }
```

### Streaming chat
```
POST /v1/chat/stream    (Server-Sent Events)
```

### Risk scoring
```
POST /v1/risk/score
{ "region": "South Asia", "hazard_type": "flood", "vulnerability_index": 0.74, "exposure_index": 0.81 }
→ { "risk_score": ..., "risk_level": "High", "recommendation": "..." }
```

### Incident classification
```
POST /v1/incidents/classify
{ "text": "Magnitude 7.1 earthquake near coastal city. Hospitals overloaded." }
→ { "incident_type": "earthquake", "severity": "critical", ... }
```

---

## Repository Structure

```
worlddisasterlm/
├── backend/                   FastAPI backend
│   └── app/
│       ├── main.py            API entrypoint
│       ├── services/
│       │   ├── inference_service.py   Core response generation (multilingual)
│       │   └── risk_engine.py         Risk scoring engine
│       ├── guardrails/
│       │   └── safety.py      Prompt safety filters
│       └── models/schemas.py  Pydantic v2 request/response models
├── worlddisasterlm/           Core ML package
│   ├── config.py              SUPPORTED_LANGUAGES, PipelineConfig
│   ├── data/
│   │   ├── collectors/        6 live API data collectors
│   │   ├── etl.py             Extract-Transform-Load pipeline
│   │   ├── qa_generator.py    8-variant QA amplification
│   │   ├── scenario_builder.py  Multilingual disaster scenarios
│   │   └── processors.py      JSONL dataset builder
│   ├── training/
│   │   ├── train_qlora.py     Production QLoRA training (SFTTrainer)
│   │   └── chat_format.py     Llama 3.1 chat template
│   ├── evaluation/
│   │   ├── metrics.py         Task + safety metrics
│   │   └── multilingual_eval.py  11-language coverage scoring
│   └── optimization/
│       ├── export_gguf.py     GGUF export (llama.cpp)
│       └── export_onnx.py     ONNX export
├── frontend/                  React 18 + Vite dashboard
│   └── src/App.jsx            Language selector, chat, analytics, monitoring
├── scripts/
│   ├── collect_data.py        Live data collection orchestrator
│   ├── push_to_hub.py         HuggingFace Hub publisher
│   ├── export_gguf.py         GGUF CLI wrapper
│   └── export_onnx.py         ONNX CLI wrapper
├── tests/
│   ├── test_api.py            FastAPI integration tests (incl. Nepali)
│   ├── test_dataset_builder.py  ETL + Nepali language tests
│   └── test_risk_engine.py    Risk scoring tests
├── inference.py               CLI inference entrypoint
├── train.py                   Training entrypoint
├── evaluate.py                Evaluation entrypoint
├── app.py                     Gradio demo
├── dataset_builder.py         Dataset build entrypoint
├── Dockerfile                 API container
├── docker-compose.yml         Full stack (API + frontend + MLflow)
└── pyproject.toml             Project metadata + pytest config
```

---

## Running Tests

```bash
# Windows
$env:PYTHONPATH = "C:\path\to\worlddisasterllm"
python -m pytest tests/ -v

# macOS/Linux
PYTHONPATH=. pytest tests/ -v
```

Expected output: **9 passed** — including Nepali language tests.

---

## Safety and Responsible AI

- Prompt-level guardrails block unsafe instructions
- Output confidence scoring with human-review flags
- Emergency disclaimers on all life-safety guidance
- Hallucination heuristics with citation anchoring
- Human-in-the-loop escalation for critical incidents

---

## HuggingFace Publishing

The model is published as **`WorldDisasterLM-8B`** on HuggingFace Hub with:
- Merged LoRA adapters (production-ready weights)
- Model card with 11-language tags (`en`, `ne`, `es`, `fr`, `ar`, `hi`, `te`, `zh`, `ja`, `ko`, `pt`)
- GGUF variant for llama.cpp / Ollama compatibility

---

## License

Base model: [Llama 3 Community License](https://ai.meta.com/llama/license/) (Meta).  
This fine-tuning code and dataset pipeline: **Apache 2.0**.

---

## Acknowledgements

- [Meta AI — Llama 3.1](https://ai.meta.com/blog/meta-llama-3/)
- [ReliefWeb API](https://reliefweb.int/help/api)
- [USGS Earthquake Hazards Program](https://earthquake.usgs.gov/fdsnws/event/1/)
- [GDACS Global Disaster Alert](https://www.gdacs.org)
- [NOAA Weather.gov API](https://api.weather.gov)
- [OpenFEMA API](https://www.fema.gov/about/openfema)
- [WHO RSS Feeds](https://www.who.int/rss-feeds)
- [NDRRMA Nepal — राष्ट्रिय विपद् जोखिम न्यूनीकरण तथा व्यवस्थापन प्राधिकरण](https://www.ndrrma.gov.np)