Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
|
3 |
+
import streamlit as st
|
4 |
+
import random
|
5 |
+
import pandas as pd
|
6 |
+
import time
|
7 |
+
import speech_recognition as sr
|
8 |
+
from openai import OpenAI
|
9 |
+
from PyPDF2 import PdfReader
|
10 |
+
import langchain
|
11 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
12 |
+
from langchain.vectorstores import FAISS
|
13 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
14 |
+
from langchain.chains import RetrievalQA
|
15 |
+
from langchain.chat_models import ChatOpenAI
|
16 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
17 |
+
|
18 |
+
|
19 |
+
# Set DeepSeek API Key
|
20 |
+
api_key = "0f33d8a8b7714460bc4b8335b66d217a"
|
21 |
+
base_url = "https://api.aimlapi.com/v1"
|
22 |
+
|
23 |
+
# Initialize OpenAI client
|
24 |
+
api = OpenAI(api_key=api_key, base_url=base_url)
|
25 |
+
|
26 |
+
# Generate 300 Dummy Messages with Severity Levels
|
27 |
+
severities = ["High", "Medium", "Low"]
|
28 |
+
messages = [
|
29 |
+
{"message": f"Disaster Alert {i}", "severity": random.choice(severities)}
|
30 |
+
for i in range(300)
|
31 |
+
]
|
32 |
+
df = pd.DataFrame(messages)
|
33 |
+
|
34 |
+
# Streamlit UI
|
35 |
+
st.set_page_config(page_title="BDRS", layout="wide")
|
36 |
+
|
37 |
+
st.title("π BeaconAi Disaster Response System")
|
38 |
+
st.write("Real-time disaster response with DeepSeek AI-powered chatbot & voice recognition.")
|
39 |
+
|
40 |
+
# Live-updating Disaster Message Dashboard
|
41 |
+
st.subheader("π Social Media Monitoring")
|
42 |
+
chart_placeholder = st.empty()
|
43 |
+
|
44 |
+
# Function to Randomly Pick 5 Messages
|
45 |
+
def get_random_messages():
|
46 |
+
return df.sample(5)
|
47 |
+
|
48 |
+
|
49 |
+
# Placeholder for chart
|
50 |
+
chart_placeholder = st.empty()
|
51 |
+
|
52 |
+
# Define severity categories
|
53 |
+
severities = ["Low", "Medium", "High"]
|
54 |
+
|
55 |
+
# Function to update chart
|
56 |
+
def update_chart():
|
57 |
+
selected_messages = get_random_messages()
|
58 |
+
severity_counts = selected_messages["severity"].value_counts().reindex(severities, fill_value=0)
|
59 |
+
|
60 |
+
# Create DataFrame
|
61 |
+
chart_data = pd.DataFrame({"Severity": severities, "Count": severity_counts.values})
|
62 |
+
|
63 |
+
# Update the chart
|
64 |
+
chart_placeholder.bar_chart(chart_data, x="Severity", y="Count", use_container_width=True)
|
65 |
+
|
66 |
+
# Auto-refresh every second
|
67 |
+
st.button("Refresh Data", on_click=st.rerun)
|
68 |
+
|
69 |
+
update_chart()
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
# PDF Processing for Chatbot Context (Pre-Provided PDF)
|
74 |
+
pdf_path = "/content/Natural Disaster Safety Manual.pdf" # Ensure this file is in the same directory as app.py
|
75 |
+
|
76 |
+
pdf_reader = PdfReader(pdf_path)
|
77 |
+
raw_text = ""
|
78 |
+
for page in pdf_reader.pages:
|
79 |
+
raw_text += page.extract_text() + "\n"
|
80 |
+
|
81 |
+
# Convert to Embeddings for Retrieval
|
82 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
83 |
+
texts = text_splitter.split_text(raw_text)
|
84 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
85 |
+
vector_db = FAISS.from_texts(texts, embeddings)
|
86 |
+
retriever = vector_db.as_retriever()
|
87 |
+
|
88 |
+
# LangGraph-Powered Q&A System
|
89 |
+
chat_model = ChatOpenAI(model="deepseek-chat", api_key=api_key, base_url=base_url)
|
90 |
+
qa = RetrievalQA.from_chain_type(llm=chat_model, chain_type="stuff", retriever=retriever)
|
91 |
+
|
92 |
+
# Chatbot UI
|
93 |
+
st.subheader("π€ AI-Powered Disaster Chatbot")
|
94 |
+
user_query = st.text_input("Ask the chatbot:")
|
95 |
+
|
96 |
+
if user_query:
|
97 |
+
response = qa.run(user_query)
|
98 |
+
st.write("**Chatbot Response:**", response)
|
99 |
+
|
100 |
+
|
101 |
+
# Voice Recognition for Non-English Users
|
102 |
+
st.subheader("ποΈ Voice Recognition (Speech-to-Text)")
|
103 |
+
|
104 |
+
if st.button("Start Recording"):
|
105 |
+
recognizer = sr.Recognizer()
|
106 |
+
with sr.Microphone() as source:
|
107 |
+
st.write("Listening...")
|
108 |
+
audio = recognizer.listen(source)
|
109 |
+
|
110 |
+
try:
|
111 |
+
recognized_text = recognizer.recognize_google(audio)
|
112 |
+
st.write("**Recognized Text:**", recognized_text)
|
113 |
+
except sr.UnknownValueError:
|
114 |
+
st.write("Sorry, could not understand.")
|
115 |
+
except sr.RequestError:
|
116 |
+
st.write("Could not request results. Check your internet connection.")
|
117 |
+
|
118 |
+
# Disaster Guide Dropdown
|
119 |
+
st.subheader("πͺοΈ Disaster Preparedness Guide")
|
120 |
+
|
121 |
+
disaster_options = {
|
122 |
+
"Wildfire": {
|
123 |
+
"steps": [
|
124 |
+
"Evacuate if ordered.",
|
125 |
+
"Keep emergency supplies ready.",
|
126 |
+
"Close all doors and windows."
|
127 |
+
],
|
128 |
+
"video": "https://www.youtube.com/watch?v=OCjl6tp8dnw"
|
129 |
+
},
|
130 |
+
"Earthquake": {
|
131 |
+
"steps": [
|
132 |
+
"Drop, Cover, and Hold On.",
|
133 |
+
"Stay indoors until shaking stops.",
|
134 |
+
"Move away from windows."
|
135 |
+
],
|
136 |
+
"video": "https://www.youtube.com/watch?v=BLEPakj1YTY"
|
137 |
+
},
|
138 |
+
"Flood": {
|
139 |
+
"steps": [
|
140 |
+
"Move to higher ground.",
|
141 |
+
"Avoid walking or driving through floodwaters.",
|
142 |
+
"Stay tuned to emergency alerts."
|
143 |
+
],
|
144 |
+
"video": "https://www.youtube.com/watch?v=43M5mZuzHF8"
|
145 |
+
}
|
146 |
+
}
|
147 |
+
|
148 |
+
selected_disaster = st.selectbox("Select a disaster type:", list(disaster_options.keys()))
|
149 |
+
|
150 |
+
if selected_disaster:
|
151 |
+
st.write("### π Steps to Follow:")
|
152 |
+
for step in disaster_options[selected_disaster]["steps"]:
|
153 |
+
st.write(f"- {step}")
|
154 |
+
|
155 |
+
st.write("πΊ [Watch Video Guide]({})".format(disaster_options[selected_disaster]["video"]))
|
156 |
+
|
157 |
+
st.write("π Stay prepared and stay safe!")
|