KeshavRa commited on
Commit
5fedc81
·
verified ·
1 Parent(s): 1a6cb8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -88
app.py CHANGED
@@ -87,39 +87,6 @@ def haversine(lat1, lon1, lat2, lon2):
87
  distance = R * c
88
  return distance
89
 
90
- # # Function to handle form submission
91
- # @st.experimental_dialog("Fill out the form")
92
- # def form_dialog():
93
- # city = st.selectbox("City", ['San Francisco', 'Oakland', 'Berkeley'])
94
- # zipcode = st.selectbox("Zipcode")
95
-
96
- # sex = st.radio("Sex", ["Male", "Female", "Other"])
97
- # lgbtq = st.radio("Do you identify as LGBTQ+ (some shelters serve this community specifically)", ["No", "Yes"])
98
- # domestic_violence = st.radio("Have you experienced domestic violence (some shelters serve these individuals specifically", ["No", "Yes"])
99
-
100
- # urgency = st.radio("How quickly do you need help?", ("Today", "In the next few days", "In a week or more"))
101
- # duration = st.radio("How long do you need a place to stay?", ("Overnight", "A month or less", "A couple of months", "A year or more"))
102
- # needs = st.text_area("Optional - Needs (tell us what you need and how we can help)")
103
-
104
- # if st.button("Submit"):
105
- # data = {
106
- # "City": city,
107
- # "Zip Code": zipcode,
108
- # "Sex": sex,
109
- # "LGBTQ": lgbtq,
110
- # "Domestic Violence": domestic_violence,
111
- # "Urgency": urgency,
112
- # "Duration": duration,
113
- # "Needs": needs
114
- # }
115
-
116
- # with open('data.json', 'w') as f:
117
- # json.dump(data, f)
118
-
119
- # st.session_state.form_submitted = True
120
- # st.session_state.data = data
121
- # st.rerun()
122
-
123
  # Initialize session state
124
  if 'form_submitted' not in st.session_state:
125
  st.session_state.form_submitted = False
@@ -136,6 +103,8 @@ st.set_page_config(
136
  st.title("ShelterSearch")
137
 
138
  if not st.session_state.form_submitted:
 
 
139
  city = st.selectbox("City", ['San Francisco', 'Oakland', 'Berkeley'])
140
  zipcode = st.text_input("Zipcode")
141
 
@@ -170,77 +139,77 @@ else:
170
  data = json.load(f)
171
  st.json(data)
172
 
173
- # shelters = pd.read_csv("database.csv")
174
 
175
- # # filter city
176
- # shelters = shelters[(shelters['City'] == data['City'])]
177
 
178
- # # filter sex
179
- # shelters = shelters[(shelters['Sex'] == data['Sex']) | (shelters['Sex'] == 'All')]
180
 
181
- # # filter lgbtq
182
- # if data['LGBTQ'] == 'No':
183
- # shelters = shelters[(shelters['LGBTQ'] == "No")]
184
 
185
- # # filter domestic violence
186
- # if data['Domestic Violence'] == "No":
187
- # shelters = shelters[(shelters['Domestic Violence'] == "No")]
188
 
189
- # # calculate distances between zipcodes
190
- # if data['Zip Code'] != "Unsure":
191
- # geocoding_api_key = os.environ['OpenWeather_API_KEY']
192
 
193
- # shelters_coordinates = shelters.apply(lambda row: get_coordinates(row['Zip Code'], geocoding_api_key), axis=1).tolist()
194
- # user_coordinates = get_coordinates(data['Zip Code'], geocoding_api_key)
195
 
196
- # distances = []
197
- # for coordinates in shelters_coordinates:
198
- # distances.append(haversine(coordinates[0], coordinates[1], user_coordinates[0], user_coordinates[1]))
199
 
200
- # max = max(distances) if (max(distances) != 0) else 1
201
- # shelters['zipcode_score'] = [d / max for d in distances]
202
 
203
- # # get urgency scores
204
- # urgency_scores = shelters.apply(lambda row: get_urgency_score(data['Urgency'], row['Urgency']), axis=1).tolist()
205
- # shelters['urgency_score'] = urgency_scores
206
 
207
- # # get duration scores
208
- # duration_scores = shelters.apply(lambda row: get_duration_score(data['Duration'], row['Duration']), axis=1).tolist()
209
- # shelters['duration_score'] = duration_scores
210
 
211
 
212
 
213
- # # services
214
- # if data['Needs'] != "":
215
- # services_scores = shelters.apply(lambda row: call_gpt(data['Needs'], row['Services']), axis=1).tolist()
216
- # services_scores = [s / 10 for s in services_scores]
217
 
218
- # shelters['services_score'] = services_scores
219
 
220
- # st.table(shelters)
221
 
222
- # shelters = [
223
- # {"title": "Shelter 1", "description": "This is the 1st shelter",},
224
- # {"title": "Shelter 2", "description": "This is the 2nd shelter.",},
225
- # {"title": "Shelter 3", "description": "This is the 3rd shelter.",}
226
- # ]
227
-
228
- # # Display the current shelter information
229
- # shelter = shelters[st.session_state.shelter_index]
230
- # st.write(shelter["description"])
231
 
232
- # # Create two columns
233
- # col1, col2 = st.columns([1,1])
234
 
235
- # # Add buttons to each column
236
- # with col1:
237
- # if st.button("Previous"):
238
- # if st.session_state.shelter_index > 0:
239
- # st.session_state.shelter_index -= 1
240
- # st.experimental_rerun()
241
 
242
- # with col2:
243
- # if st.button("Next"):
244
- # if st.session_state.shelter_index < len(shelters) - 1:
245
- # st.session_state.shelter_index += 1
246
- # st.experimental_rerun()
 
87
  distance = R * c
88
  return distance
89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  # Initialize session state
91
  if 'form_submitted' not in st.session_state:
92
  st.session_state.form_submitted = False
 
103
  st.title("ShelterSearch")
104
 
105
  if not st.session_state.form_submitted:
106
+ st.write("Fill out this form")
107
+
108
  city = st.selectbox("City", ['San Francisco', 'Oakland', 'Berkeley'])
109
  zipcode = st.text_input("Zipcode")
110
 
 
139
  data = json.load(f)
140
  st.json(data)
141
 
142
+ shelters = pd.read_csv("database.csv")
143
 
144
+ # filter city
145
+ shelters = shelters[(shelters['City'] == data['City'])]
146
 
147
+ # filter sex
148
+ shelters = shelters[(shelters['Sex'] == data['Sex']) | (shelters['Sex'] == 'All')]
149
 
150
+ # filter lgbtq
151
+ if data['LGBTQ'] == 'No':
152
+ shelters = shelters[(shelters['LGBTQ'] == "No")]
153
 
154
+ # filter domestic violence
155
+ if data['Domestic Violence'] == "No":
156
+ shelters = shelters[(shelters['Domestic Violence'] == "No")]
157
 
158
+ # calculate distances between zipcodes
159
+ if data['Zip Code'] != "Unsure":
160
+ geocoding_api_key = os.environ['OpenWeather_API_KEY']
161
 
162
+ shelters_coordinates = shelters.apply(lambda row: get_coordinates(row['Zip Code'], geocoding_api_key), axis=1).tolist()
163
+ user_coordinates = get_coordinates(data['Zip Code'], geocoding_api_key)
164
 
165
+ distances = []
166
+ for coordinates in shelters_coordinates:
167
+ distances.append(haversine(coordinates[0], coordinates[1], user_coordinates[0], user_coordinates[1]))
168
 
169
+ max = max(distances) if (max(distances) != 0) else 1
170
+ shelters['zipcode_score'] = [d / max for d in distances]
171
 
172
+ # get urgency scores
173
+ urgency_scores = shelters.apply(lambda row: get_urgency_score(data['Urgency'], row['Urgency']), axis=1).tolist()
174
+ shelters['urgency_score'] = urgency_scores
175
 
176
+ # get duration scores
177
+ duration_scores = shelters.apply(lambda row: get_duration_score(data['Duration'], row['Duration']), axis=1).tolist()
178
+ shelters['duration_score'] = duration_scores
179
 
180
 
181
 
182
+ # services
183
+ if data['Needs'] != "":
184
+ services_scores = shelters.apply(lambda row: call_gpt(data['Needs'], row['Services']), axis=1).tolist()
185
+ services_scores = [s / 10 for s in services_scores]
186
 
187
+ shelters['services_score'] = services_scores
188
 
189
+ st.table(shelters)
190
 
191
+ shelters = [
192
+ {"title": "Shelter 1", "description": "This is the 1st shelter",},
193
+ {"title": "Shelter 2", "description": "This is the 2nd shelter.",},
194
+ {"title": "Shelter 3", "description": "This is the 3rd shelter.",}
195
+ ]
196
+
197
+ # Display the current shelter information
198
+ shelter = shelters[st.session_state.shelter_index]
199
+ st.write(shelter["description"])
200
 
201
+ # Create two columns
202
+ col1, col2 = st.columns([1,1])
203
 
204
+ # Add buttons to each column
205
+ with col1:
206
+ if st.button("Previous"):
207
+ if st.session_state.shelter_index > 0:
208
+ st.session_state.shelter_index -= 1
209
+ st.experimental_rerun()
210
 
211
+ with col2:
212
+ if st.button("Next"):
213
+ if st.session_state.shelter_index < len(shelters) - 1:
214
+ st.session_state.shelter_index += 1
215
+ st.experimental_rerun()