Spaces:
Runtime error
Runtime error
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() | |