File size: 3,181 Bytes
58a8218
c2f316d
 
cef2785
62a2c7a
c2f316d
cef2785
 
 
82b4892
62a2c7a
 
 
 
 
 
 
cef2785
 
82b4892
62a2c7a
 
 
 
 
 
cef2785
 
82b4892
62a2c7a
 
 
 
 
 
cef2785
 
82b4892
62a2c7a
 
 
 
 
58a8218
cef2785
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58a8218
0a2766b
c16432c
cef2785
 
62a2c7a
 
 
cef2785
 
62a2c7a
 
0a2766b
 
82b4892
 
62a2c7a
82b4892
 
 
 
62a2c7a
82b4892
 
0a2766b
62a2c7a
 
 
 
 
 
58a8218
0a2766b
 
cef2785
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
import gradio as gr
from transformers import pipeline

# Load model
generator = pipeline("text2text-generation", model="google/flan-t5-base")

# Knowledge base
disaster_knowledge = {
    "earthquake": """
🌍 Earthquake Safety:
1. Drop, cover, and hold on immediately.
2. Stay indoors until shaking stops completely.
3. Stay away from windows, shelves, and heavy objects.
4. If outside, move to an open area away from buildings, trees, and power lines.
5. After shaking stops, check for injuries and help others.
6. Avoid using elevators.
πŸ“ž Helpline: 1078 (National Disaster Helpline, India)
""",
    "flood": """
🌊 Flood Safety:
1. Move to higher ground immediately.
2. Avoid walking or driving through floodwaters – even 6 inches of water can knock you down.
3. Disconnect electrical appliances.
4. Keep emergency supplies (water, food, torch, first aid kit).
5. Follow evacuation orders from authorities.
πŸ“ž Helpline: 1070 (State Emergency Operations Center, India)
""",
    "fire": """
πŸ”₯ Fire Safety:
1. Stay low to avoid smoke inhalation.
2. Use the stairs, not elevators.
3. Call emergency services immediately.
4. Stop, drop, and roll if your clothes catch fire.
5. Exit quickly and leave belongings behind.
πŸ“ž Fire Helpline: 101 (India)
""",
    "cyclone": """
πŸŒͺ️ Cyclone Safety:
1. Stay indoors and away from windows.
2. Keep emergency kits ready (water, food, medicines, torch).
3. Disconnect electrical appliances.
4. Follow official weather updates and evacuation orders.
πŸ“ž Helpline: 108 (Emergency Medical Services, India), 1098 (Childline)
"""
}

# Function to pick the right context
def get_context(user_input: str):
    text = user_input.lower()
    if "earthquake" in text:
        return disaster_knowledge["earthquake"]
    elif "flood" in text:
        return disaster_knowledge["flood"]
    elif "fire" in text:
        return disaster_knowledge["fire"]
    elif "cyclone" in text or "hurricane" in text or "typhoon" in text:
        return disaster_knowledge["cyclone"]
    else:
        return "\n".join(disaster_knowledge.values())  # default: all

# Chat function
def disaster_chat(user_input, history):
    context = get_context(user_input)
    prompt = f"""{context}

User question: {user_input}

Give a detailed, step-by-step safety guide with helpline numbers.
"""
    response = generator(prompt, max_length=400, temperature=0.7)[0]["generated_text"]
    return response

# Welcome message
welcome_msg = (
    "πŸ‘‹ Hello! I am your **Disaster Safety Assistant**.\n\n"
    "Ask me questions like:\n"
    "β€’ What should I do during an earthquake?\n"
    "β€’ How can I stay safe in a flood?\n"
    "β€’ What should I do if there is a fire?\n"
    "β€’ How to prepare for a cyclone?\n\n"
    "I will always give you **step-by-step safety precautions with helpline numbers**. 🌟"
)

# Gradio Chat UI
demo = gr.ChatInterface(
    fn=disaster_chat,
    title="🌍 Disaster Guidance AI Chatbot",
    description="Get detailed step-by-step safety guidance (earthquake, flood, fire, cyclone) including helpline numbers.",
    chatbot=gr.Chatbot(value=[("assistant", welcome_msg)])
)

if __name__ == "__main__":
    demo.launch()