Spaces:
Runtime error
Runtime error
Create app1.py
Browse files
app1.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# ----- Configuration -----
|
| 11 |
+
|
| 12 |
+
# Replace with your actual credentials or use Streamlit secrets
|
| 13 |
+
sender_email = "[email protected]"
|
| 14 |
+
receiver_email = "[email protected]"
|
| 15 |
+
sender_password = "your-password"
|
| 16 |
+
|
| 17 |
+
twilio_sid = "your-twilio-sid"
|
| 18 |
+
twilio_token = "your-twilio-token"
|
| 19 |
+
twilio_number = "your-twilio-number"
|
| 20 |
+
|
| 21 |
+
st.set_page_config(page_title="Emergency App", layout="centered")
|
| 22 |
+
|
| 23 |
+
# ----- Sidebar Navigation -----
|
| 24 |
+
st.sidebar.title("Navigation")
|
| 25 |
+
app_mode = st.sidebar.selectbox("Choose the app mode", ["User Info", "SOS Alert", "Ambulance Alert", "Send SMS"])
|
| 26 |
+
|
| 27 |
+
# ----- User Info -----
|
| 28 |
+
def user_info_form():
|
| 29 |
+
st.title("π§ User Information Form")
|
| 30 |
+
name = st.text_input("Name")
|
| 31 |
+
user_id = st.text_input("User ID")
|
| 32 |
+
password = st.text_input("Password", type="password")
|
| 33 |
+
age = st.number_input("Age", min_value=0, max_value=120)
|
| 34 |
+
gender = st.radio("Gender", ["Male", "Female", "Other"])
|
| 35 |
+
phone = st.text_input("Phone Number")
|
| 36 |
+
email = st.text_input("Email")
|
| 37 |
+
blood_group = st.selectbox("Blood Group", ["A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"])
|
| 38 |
+
allergies = st.text_area("Allergies")
|
| 39 |
+
medical_conditions = st.text_area("Medical Conditions")
|
| 40 |
+
|
| 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 |
+
# ----- SOS Email Alert -----
|
| 62 |
+
def send_sos_alert():
|
| 63 |
+
st.title("π¨ SOS Email Alert")
|
| 64 |
+
message = "Emergency! Please respond."
|
| 65 |
+
|
| 66 |
+
if st.button("Send SOS Email"):
|
| 67 |
+
try:
|
| 68 |
+
msg = MIMEMultipart()
|
| 69 |
+
msg['Subject'] = 'SOS Alert'
|
| 70 |
+
msg['From'] = sender_email
|
| 71 |
+
msg['To'] = receiver_email
|
| 72 |
+
msg.attach(MIMEText(message, 'plain'))
|
| 73 |
+
|
| 74 |
+
with smtplib.SMTP('smtp.gmail.com', 587) as server:
|
| 75 |
+
server.starttls()
|
| 76 |
+
server.login(sender_email, sender_password)
|
| 77 |
+
server.send_message(msg)
|
| 78 |
+
|
| 79 |
+
st.success("π§ SOS alert sent successfully via email!")
|
| 80 |
+
except Exception as e:
|
| 81 |
+
st.error(f"β Failed to send SOS alert: {e}")
|
| 82 |
+
|
| 83 |
+
# ----- Location Fetching -----
|
| 84 |
+
def get_location():
|
| 85 |
+
g = geocoder.ip('me')
|
| 86 |
+
return g.latlng if g.ok else None
|
| 87 |
+
|
| 88 |
+
# ----- Ambulance Alert -----
|
| 89 |
+
def send_alert_to_hospital(location):
|
| 90 |
+
message = f"Emergency! Send an ambulance to location: {location}"
|
| 91 |
+
st.success(f"π₯ Alert sent to hospital: {message}")
|
| 92 |
+
return message
|
| 93 |
+
|
| 94 |
+
def auto_ambulance_alert():
|
| 95 |
+
st.title("π Ambulance Dispatch Alert")
|
| 96 |
+
if st.button("Send Ambulance Alert"):
|
| 97 |
+
location = get_location()
|
| 98 |
+
if location:
|
| 99 |
+
send_alert_to_hospital(location)
|
| 100 |
+
else:
|
| 101 |
+
st.error("β Could not determine location.")
|
| 102 |
+
|
| 103 |
+
# ----- SMS Alert via Twilio -----
|
| 104 |
+
def send_sms():
|
| 105 |
+
st.title("π² Send SMS Alert")
|
| 106 |
+
to_number = st.text_input("Enter emergency contact number (with country code):")
|
| 107 |
+
message_body = "Emergency! Please respond."
|
| 108 |
+
|
| 109 |
+
if st.button("Send SMS"):
|
| 110 |
+
try:
|
| 111 |
+
client = Client(twilio_sid, twilio_token)
|
| 112 |
+
message_body= client.messages.create(
|
| 113 |
+
body=message_body,
|
| 114 |
+
from_=twilio_number,
|
| 115 |
+
to=to_number
|
| 116 |
+
)
|
| 117 |
+
st.success("π© SMS sent successfully!")
|
| 118 |
+
except Exception as e:
|
| 119 |
+
st.error(f"β Failed to send SMS: {e}")
|
| 120 |
+
|
| 121 |
+
# ----- Main Logic -----
|
| 122 |
+
if app_mode == "User Info":
|
| 123 |
+
user_info_form()
|
| 124 |
+
elif app_mode == "SOS Alert":
|
| 125 |
+
send_sos_alert()
|
| 126 |
+
elif app_mode == "Ambulance Alert":
|
| 127 |
+
auto_ambulance_alert()
|
| 128 |
+
elif app_mode == "Send SMS":
|
| 129 |
+
send_sms()
|