Spaces:
No application file
No application file
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() |