File size: 4,026 Bytes
346d40c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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

# 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"

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

# Title
st.title("🚟 Emergency SOS App")

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

# User Info Form
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")
    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!")

# Send SOS Alert via Email
def send_sos_alert():
    st.title("SOS Alert")
    message = "Emergency! Please respond."
    if st.button("Send SOS Alert"):
        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!")
        except Exception as e:
            st.error(f"Failed to send SOS alert: {e}")

# Get Location
def get_location():
    g = geocoder.ip('me')
    return g.latlng

# Send Alert to Hospital
def send_alert_to_hospital(location):
    message = f"Emergency! Send an ambulance to location: {location}"
    st.write("Alert sent to hospital:", message)
    return message

# Auto Ambulance Alert
def auto_ambulance_alert():
    st.title("Ambulance Dispatch Alert")
    if st.button("Send Alert to Hospital"):
        location = get_location()
        if location:
            send_alert_to_hospital(location)
        else:
            st.error("Could not determine location.")

# Send SMS
def send_sms():
    to_number = st.text_input("Enter emergency contact number:")
    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 App
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()