Spaces:
No application file
No application file
Create bsafe.py
Browse files
bsafe.py
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import os
|
4 |
+
import smtplib
|
5 |
+
from email.mime.multipart import MIMEMultipart
|
6 |
+
from email.mime.text import MIMEText
|
7 |
+
import geocoder
|
8 |
+
from twilio.rest import Client
|
9 |
+
|
10 |
+
# Secrets
|
11 |
+
sender_email = "[email protected]"
|
12 |
+
receiver_email = "[email protected]"
|
13 |
+
sender_password = "your-password"
|
14 |
+
twilio_sid = "your-twilio-sid"
|
15 |
+
twilio_token = "your-twilio-token"
|
16 |
+
twilio_number = "your-twilio-number"
|
17 |
+
|
18 |
+
# Page config
|
19 |
+
st.set_page_config(page_title="Emergency App", layout="centered")
|
20 |
+
|
21 |
+
# Title
|
22 |
+
st.title("🚟 Emergency SOS App")
|
23 |
+
|
24 |
+
# Sidebar
|
25 |
+
st.sidebar.title("Navigation")
|
26 |
+
app_mode = st.sidebar.selectbox("Choose the app mode", ["User Info", "SOS Alert", "Ambulance Alert", "Send SMS"])
|
27 |
+
|
28 |
+
# User Info Form
|
29 |
+
def user_info_form():
|
30 |
+
st.title("User Information Form")
|
31 |
+
name = st.text_input("Name")
|
32 |
+
user_id = st.text_input("User Id")
|
33 |
+
password= st.text_input("password")
|
34 |
+
age = st.number_input("Age", min_value=0, max_value=120)
|
35 |
+
gender = st.radio("Gender", ["Male", "Female", "Other"])
|
36 |
+
phone = st.text_input("Phone Number")
|
37 |
+
email = st.text_input("Email")
|
38 |
+
blood_group = st.selectbox("Blood Group", ["A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"])
|
39 |
+
allergies = st.text_area("Allergies")
|
40 |
+
medical_conditions = st.text_area("Medical Conditions")
|
41 |
+
if st.button("Submit"):
|
42 |
+
user_data = {
|
43 |
+
"Name": name,
|
44 |
+
"User Id": user_id,
|
45 |
+
"Password": password,
|
46 |
+
"Age": age,
|
47 |
+
"Gender": gender,
|
48 |
+
"Phone": phone,
|
49 |
+
"Email": email,
|
50 |
+
"Blood Group": blood_group,
|
51 |
+
"Allergies": allergies,
|
52 |
+
"Medical Conditions": medical_conditions
|
53 |
+
}
|
54 |
+
df = pd.DataFrame([user_data])
|
55 |
+
if os.path.exists("user_data.csv"):
|
56 |
+
df.to_csv("user_data.csv", mode='a', header=False, index=False)
|
57 |
+
else:
|
58 |
+
df.to_csv("user_data.csv", index=False)
|
59 |
+
st.success("User information saved successfully!")
|
60 |
+
|
61 |
+
# Send SOS Alert via Email
|
62 |
+
def send_sos_alert():
|
63 |
+
st.title("SOS Alert")
|
64 |
+
message = "Emergency! Please respond."
|
65 |
+
if st.button("Send SOS Alert"):
|
66 |
+
try:
|
67 |
+
msg = MIMEMultipart()
|
68 |
+
msg['Subject'] = 'SOS Alert'
|
69 |
+
msg['From'] = sender_email
|
70 |
+
msg['To'] = receiver_email
|
71 |
+
msg.attach(MIMEText(message, 'plain'))
|
72 |
+
with smtplib.SMTP('smtp.gmail.com', 587) as server:
|
73 |
+
server.starttls()
|
74 |
+
server.login(sender_email, sender_password)
|
75 |
+
server.send_message(msg)
|
76 |
+
st.success("SOS alert sent successfully!")
|
77 |
+
except Exception as e:
|
78 |
+
st.error(f"Failed to send SOS alert: {e}")
|
79 |
+
|
80 |
+
# Get Location
|
81 |
+
def get_location():
|
82 |
+
g = geocoder.ip('me')
|
83 |
+
return g.latlng
|
84 |
+
|
85 |
+
# Send Alert to Hospital
|
86 |
+
def send_alert_to_hospital(location):
|
87 |
+
message = f"Emergency! Send an ambulance to location: {location}"
|
88 |
+
st.write("Alert sent to hospital:", message)
|
89 |
+
return message
|
90 |
+
|
91 |
+
# Auto Ambulance Alert
|
92 |
+
def auto_ambulance_alert():
|
93 |
+
st.title("Ambulance Dispatch Alert")
|
94 |
+
if st.button("Send Alert to Hospital"):
|
95 |
+
location = get_location()
|
96 |
+
if location:
|
97 |
+
send_alert_to_hospital(location)
|
98 |
+
else:
|
99 |
+
st.error("Could not determine location.")
|
100 |
+
|
101 |
+
# Send SMS
|
102 |
+
def send_sms():
|
103 |
+
to_number = st.text_input("Enter emergency contact number:")
|
104 |
+
message_body = "Emergency! Please respond."
|
105 |
+
if st.button("Send SMS"):
|
106 |
+
try:
|
107 |
+
client = Client(twilio_sid, twilio_token)
|
108 |
+
message_body = client.messages.create(
|
109 |
+
body=message_body,
|
110 |
+
from_=twilio_number,
|
111 |
+
to=to_number
|
112 |
+
)
|
113 |
+
st.success("SMS sent successfully!")
|
114 |
+
except Exception as e:
|
115 |
+
st.error(f"Failed to send SMS: {e}")
|
116 |
+
|
117 |
+
# Main App
|
118 |
+
if app_mode == "User Info":
|
119 |
+
user_info_form()
|
120 |
+
elif app_mode == "SOS Alert":
|
121 |
+
send_sos_alert()
|
122 |
+
elif app_mode == "Ambulance Alert":
|
123 |
+
auto_ambulance_alert()
|
124 |
+
elif app_mode == "Send SMS":
|
125 |
+
send_sms()
|