File size: 4,283 Bytes
fc40e05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import streamlit as st
import pandas as pd
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import geocoder
from twilio.rest import Client

# ----- Configuration -----

# Replace with your actual credentials or use Streamlit secrets
sender_email = "[email protected]"
receiver_email = "[email protected]"
sender_password = "your-password"

twilio_sid = "your-twilio-sid"
twilio_token = "your-twilio-token"
twilio_number = "your-twilio-number"

st.set_page_config(page_title="Emergency App", layout="centered")

# ----- Sidebar Navigation -----
st.sidebar.title("Navigation")
app_mode = st.sidebar.selectbox("Choose the app mode", ["User Info", "SOS Alert", "Ambulance Alert", "Send SMS"])

# ----- User Info -----
def user_info_form():
    st.title("πŸ§‘ User Information Form")
    name = st.text_input("Name")
    user_id = st.text_input("User ID")
    password = st.text_input("Password", type="password")
    age = st.number_input("Age", min_value=0, max_value=120)
    gender = st.radio("Gender", ["Male", "Female", "Other"])
    phone = st.text_input("Phone Number")
    email = st.text_input("Email")
    blood_group = st.selectbox("Blood Group", ["A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"])
    allergies = st.text_area("Allergies")
    medical_conditions = st.text_area("Medical Conditions")

    if st.button("Submit"):
        user_data = {
            "Name": name,
            "User ID": user_id,
            "Password": password,
            "Age": age,
            "Gender": gender,
            "Phone": phone,
            "Email": email,
            "Blood Group": blood_group,
            "Allergies": allergies,
            "Medical Conditions": medical_conditions
        }
        df = pd.DataFrame([user_data])
        if os.path.exists("user_data.csv"):
            df.to_csv("user_data.csv", mode='a', header=False, index=False)
        else:
            df.to_csv("user_data.csv", index=False)
        st.success("βœ… User information saved successfully!")

# ----- SOS Email Alert -----
def send_sos_alert():
    st.title("🚨 SOS Email Alert")
    message = "Emergency! Please respond."

    if st.button("Send SOS Email"):
        try:
            msg = MIMEMultipart()
            msg['Subject'] = 'SOS Alert'
            msg['From'] = sender_email
            msg['To'] = receiver_email
            msg.attach(MIMEText(message, 'plain'))

            with smtplib.SMTP('smtp.gmail.com', 587) as server:
                server.starttls()
                server.login(sender_email, sender_password)
                server.send_message(msg)

            st.success("πŸ“§ SOS alert sent successfully via email!")
        except Exception as e:
            st.error(f"❌ Failed to send SOS alert: {e}")

# ----- Location Fetching -----
def get_location():
    g = geocoder.ip('me')
    return g.latlng if g.ok else None

# ----- Ambulance Alert -----
def send_alert_to_hospital(location):
    message = f"Emergency! Send an ambulance to location: {location}"
    st.success(f"πŸ₯ Alert sent to hospital: {message}")
    return message

def auto_ambulance_alert():
    st.title("πŸš‘ Ambulance Dispatch Alert")
    if st.button("Send Ambulance Alert"):
        location = get_location()
        if location:
            send_alert_to_hospital(location)
        else:
            st.error("❌ Could not determine location.")

# ----- SMS Alert via Twilio -----
def send_sms():
    st.title("πŸ“² Send SMS Alert")
    to_number = st.text_input("Enter emergency contact number (with country code):")
    message_body = "Emergency! Please respond."

    if st.button("Send SMS"):
        try:
            client = Client(twilio_sid, twilio_token)
            message_body= client.messages.create(
                body=message_body,
                from_=twilio_number,
                to=to_number
            )
            st.success("πŸ“© SMS sent successfully!")
        except Exception as e:
            st.error(f"❌ Failed to send SMS: {e}")

# ----- Main Logic -----
if app_mode == "User Info":
    user_info_form()
elif app_mode == "SOS Alert":
    send_sos_alert()
elif app_mode == "Ambulance Alert":
    auto_ambulance_alert()
elif app_mode == "Send SMS":
    send_sms()