drdeveloper88's picture
Add usage code example (from_pretrained + BitsAndBytes + multilingual) to README
3b9fcc7
Raw
History Blame Contribute Delete
12.5 kB
"""
QA pair generator.
Converts raw DisasterRecord objects into diverse instruction-following samples.
Each record generates 8–10 QA variants to amplify the training corpus.
"""
import random
from worlddisasterlm.data.schemas import DisasterRecord, InstructionSample
RESPONSE_STEPS = {
"earthquake": [
"Drop, Cover, and Hold On immediately.",
"Move away from windows, glass, and heavy furniture.",
"If outdoors, move to open space away from buildings.",
"After shaking stops, check for injuries and hazards.",
"Be prepared for aftershocks.",
"Do not use elevators; evacuate via stairways if building is unsafe.",
"Listen to emergency broadcasts for official guidance.",
],
"flood": [
"Move immediately to higher ground; do not wait for official evacuation order.",
"Avoid walking or driving through flood waters – 6 inches can knock you down.",
"Disconnect electrical appliances; do not touch electrical equipment if wet.",
"Follow evacuation routes designated by local emergency management.",
"Store emergency supplies including water, food, and medication for 72 hours.",
"Monitor official weather and emergency alerts for updates.",
],
"wildfire": [
"Evacuate immediately when ordered – do not wait.",
"Close all windows and doors to reduce smoke penetration.",
"Remove flammable items from around your home before leaving.",
"Wear N95 masks or wet cloth to protect against smoke inhalation.",
"Stay tuned to emergency broadcasts for evacuation route updates.",
],
"hurricane": [
"Secure or bring indoors all outdoor furniture and objects.",
"Board up windows and reinforce garage doors.",
"Prepare emergency kit: water, food, medications, documents.",
"Know your evacuation zone and route.",
"Stay indoors during the storm; the eye of the hurricane is not the all-clear.",
"After the storm, watch for downed power lines and contaminated water.",
],
"epidemic": [
"Report cases to local health authorities immediately.",
"Implement infection control measures: PPE, isolation protocols.",
"Coordinate with WHO, CDC, and national health agencies.",
"Establish clear case definition and surveillance system.",
"Activate contact tracing and quarantine procedures.",
"Communicate clearly with the public to prevent panic.",
],
"pandemic": [
"Follow national health authority guidelines.",
"Implement non-pharmaceutical interventions: masking, distancing.",
"Prioritize healthcare system capacity management.",
"Accelerate vaccine development and equitable distribution.",
"Coordinate international response through WHO frameworks.",
],
"default": [
"Activate emergency response plan immediately.",
"Prioritize life safety: triage injuries, evacuate if necessary.",
"Contact emergency services (fire, police, medical) as appropriate.",
"Coordinate with local emergency management authority.",
"Set up incident command structure.",
"Pre-position supplies: water, food, medical equipment, shelter.",
"Communicate regularly with affected population in plain language.",
"Document all actions for accountability and after-action review.",
],
}
RESOURCE_GUIDANCE = {
"earthquake": "Search and rescue teams, medical triage units, heavy machinery for debris removal, temporary shelter, water purification, emergency food supplies, structural engineers.",
"flood": "Boats and water rescue teams, pumping equipment, water purification, temporary shelters on elevated ground, food and medical supplies, sanitation units.",
"wildfire": "Aerial firefighting assets, ground crews, evacuation transport, respiratory medical support, temporary shelters, animal rescue resources.",
"hurricane": "Pre-positioned food, water and fuel, emergency shelters, power restoration crews, debris removal, search and rescue teams, mental health support.",
"epidemic": "Medical personnel with PPE, testing kits, contact tracing capacity, isolation facilities, treatment medicines, communication system.",
"default": "Emergency medical teams, shelter supplies, clean water and food, communication equipment, transport resources, coordination staff.",
}
def _get_response_steps(event_type: str) -> list[str]:
for key in RESPONSE_STEPS:
if key in event_type.lower():
return RESPONSE_STEPS[key]
return RESPONSE_STEPS["default"]
def _get_resources(event_type: str) -> str:
for key in RESOURCE_GUIDANCE:
if key in event_type.lower():
return RESOURCE_GUIDANCE[key]
return RESOURCE_GUIDANCE["default"]
def generate_qa_pairs(records: list[DisasterRecord]) -> list[InstructionSample]:
samples: list[InstructionSample] = []
for record in records:
steps = _get_response_steps(record.event_type)
steps_text = "\n".join(f"{i + 1}. {s}" for i, s in enumerate(steps))
resources = _get_resources(record.event_type)
context = (
f"Region: {record.region}\n"
f"Hazard type: {record.event_type}\n"
f"Severity: {record.severity}\n"
f"Situation: {record.summary}"
)
# QA 1 – immediate response
samples.append(
InstructionSample(
instruction="What are the immediate emergency response steps for this disaster situation?",
input=context,
output=f"Immediate response priorities for a {record.severity}-severity {record.event_type} event:\n\n{steps_text}",
)
)
# QA 2 – incident summary
samples.append(
InstructionSample(
instruction="Summarize this disaster event for an emergency operations center briefing.",
input=context,
output=(
f"SITUATION REPORT — {record.event_type.upper()} | {record.region}\n\n"
f"Severity: {record.severity.capitalize()}\n"
f"Source: {record.source}\n\n"
f"Summary: {record.summary}\n\n"
f"Status: Active monitoring required. Coordinate with local authorities and relevant UN agencies."
),
)
)
# QA 3 – resource planning
samples.append(
InstructionSample(
instruction="What resources and logistics are needed to respond to this disaster?",
input=context,
output=(
f"Resource requirements for a {record.event_type} event in {record.region} (severity: {record.severity}):\n\n"
f"{resources}\n\n"
f"Logistics priorities: establish forward operating base near the affected area, "
f"pre-position supplies along access routes, coordinate air and ground transport, "
f"and establish communication links with local partners."
),
)
)
# QA 4 – risk assessment
samples.append(
InstructionSample(
instruction="Perform a risk assessment for this disaster scenario.",
input=context,
output=(
f"Risk Assessment: {record.event_type.capitalize()} in {record.region}\n\n"
f"Risk Level: {record.severity.capitalize()}\n"
f"Primary hazard: {record.event_type}\n"
f"Secondary hazards: displacement, water contamination, disease outbreak, infrastructure collapse\n"
f"Vulnerable populations: elderly, children, persons with disabilities, low-income households\n\n"
f"Recommended risk reduction actions:\n"
f"1. Activate early warning dissemination for at-risk zones.\n"
f"2. Pre-position emergency supplies and first responder teams.\n"
f"3. Establish coordination hub with government and NGO partners.\n"
f"4. Issue public guidance in multiple local languages."
),
)
)
# QA 5 – humanitarian aid
samples.append(
InstructionSample(
instruction="What humanitarian aid priorities should be activated for this disaster?",
input=context,
output=(
f"Humanitarian Aid Priorities for {record.event_type} in {record.region}:\n\n"
f"1. Life-saving: Search and rescue, emergency medical care, trauma treatment.\n"
f"2. Basic needs: Emergency shelter, safe water, food assistance, sanitation.\n"
f"3. Protection: Safety monitoring for displaced persons, child protection, GBV prevention.\n"
f"4. Livelihoods: Cash transfers and livelihood support for affected households.\n"
f"5. Recovery: Debris clearance, shelter repair, economic recovery planning.\n\n"
f"Key coordination partners: OCHA, UNHCR, WFP, UNICEF, WHO, local disaster management authority."
),
)
)
# QA 6 – preparedness
samples.append(
InstructionSample(
instruction="How can communities in this region prepare for this type of disaster?",
input=context,
output=(
f"Community Preparedness for {record.event_type} in {record.region}:\n\n"
f"1. Develop and rehearse household emergency plans.\n"
f"2. Build 72-hour emergency supply kits (water, food, medication, documents).\n"
f"3. Know evacuation routes and local shelter locations.\n"
f"4. Participate in community early warning systems.\n"
f"5. Strengthen local infrastructure and building codes.\n"
f"6. Conduct regular drills with schools, workplaces, and community organizations.\n"
f"7. Ensure vulnerable populations have specific support plans."
),
)
)
# QA 7 – public communication
samples.append(
InstructionSample(
instruction="Draft an emergency public communication message for this disaster.",
input=context,
output=(
f"EMERGENCY ALERT — {record.region.upper()}\n\n"
f"A {record.severity}-severity {record.event_type} event has been reported.\n\n"
f"IMMEDIATE ACTIONS REQUIRED:\n"
f"• Follow official evacuation orders immediately.\n"
f"• Move to designated shelters or higher ground.\n"
f"• Call emergency services for life-threatening situations.\n"
f"• Do NOT spread unverified information.\n\n"
f"Stay tuned to official government and emergency management channels for updates.\n"
f"[This message should be verified and issued by the authorised emergency management authority.]"
),
)
)
# QA 8 – recovery planning
samples.append(
InstructionSample(
instruction="What are the key steps in disaster recovery planning after this event?",
input=context,
output=(
f"Recovery Planning Framework: {record.event_type}{record.region}\n\n"
f"Phase 1 (0–72 hours): Life safety, damage assessment, displaced persons registration.\n"
f"Phase 2 (1–4 weeks): Temporary shelter provision, debris clearance, basic services restoration.\n"
f"Phase 3 (1–6 months): Infrastructure repair, economic recovery, psychosocial support.\n"
f"Phase 4 (6+ months): Long-term reconstruction, risk reduction investments, lesson-learned review.\n\n"
f"Key principles: Build Back Better, inclusion of marginalized groups, environmental sustainability, "
f"community ownership of the recovery process."
),
)
)
return samples