GHarshasri commited on
Commit
7433a0c
Β·
verified Β·
1 Parent(s): 6bd1e01

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +127 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,129 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
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()