KeshavRa commited on
Commit
24b9639
·
verified ·
1 Parent(s): 39b56f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -59
app.py CHANGED
@@ -94,6 +94,9 @@ if 'form_submitted' not in st.session_state:
94
  if 'shelter_index' not in st.session_state:
95
  st.session_state.shelter_index = 0
96
 
 
 
 
97
  # Page config
98
  st.set_page_config(
99
  page_title="ShelterSearch",
@@ -139,78 +142,81 @@ if not st.session_state.form_submitted:
139
  json.dump(data, f)
140
 
141
  st.session_state.form_submitted = True
142
- st.session_state.data = data
143
  st.rerun()
144
  else:
145
- with open('data.json', 'r') as f:
146
- data = json.load(f)
147
-
148
- shelters = pd.read_csv("database.csv")
149
 
150
- # filter city
151
- shelters = shelters[(shelters['City'] == data['City'])]
152
 
153
- # filter sex
154
- shelters = shelters[(shelters['Sex'] == data['Sex']) | (shelters['Sex'] == 'All')]
155
-
156
- # filter lgbtq
157
- if data['LGBTQ'] == 'No':
158
- shelters = shelters[(shelters['LGBTQ'] == "No")]
159
-
160
- # filter domestic violence
161
- if data['Domestic Violence'] == "No":
162
- shelters = shelters[(shelters['Domestic Violence'] == "No")]
163
-
164
- # keep track of which scores are calculated
165
- scores = []
166
-
167
- # calculate distances between zipcodes
168
- if data['Zip Code'] != "Unsure":
169
- geocoding_api_key = os.environ['OpenWeather_API_KEY']
170
 
171
- shelters_coordinates = shelters.apply(lambda row: get_coordinates(row['Zip Code'], geocoding_api_key), axis=1).tolist()
172
- user_coordinates = get_coordinates(data['Zip Code'], geocoding_api_key)
173
-
174
- distances = []
175
- for coordinates in shelters_coordinates:
176
- distances.append(haversine(coordinates[0], coordinates[1], user_coordinates[0], user_coordinates[1]))
177
 
178
- max = max(distances) if (max(distances) != 0) else 1
179
- shelters['zipcode_score'] = [d / max for d in distances]
180
- scores.append('zipcode_score')
181
-
182
- # get urgency scores
183
- urgency_scores = shelters.apply(lambda row: get_urgency_score(data['Urgency'], row['Urgency']), axis=1).tolist()
184
- shelters['urgency_score'] = urgency_scores
185
- scores.append('urgency_score')
186
-
187
- # get duration scores
188
- duration_scores = shelters.apply(lambda row: get_duration_score(data['Duration'], row['Duration']), axis=1).tolist()
189
- shelters['duration_score'] = duration_scores
190
- scores.append('duration_score')
191
 
192
- # services
193
- if data['Needs'] != "":
194
- OpenAI_API_KEY = os.environ["OPENAI_API_KEY"]
 
 
 
 
 
 
 
 
 
 
195
 
196
- services_scores = shelters.apply(lambda row: call_gpt(data['Needs'], row['Services'], OpenAI_API_KEY), axis=1).tolist()
197
- services_scores = [s / 10 for s in services_scores]
 
198
 
199
- shelters['services_score'] = services_scores
200
- scores.append('services_score')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
 
202
- # calcualte cumulative score
203
- shelters['total_score'] = shelters[scores].sum(axis=1)
204
- shelters['total_score'] = shelters['total_score'] / len(scores)
205
 
206
- shelters = shelters.sort_values(by='total_score', ascending=True)
207
- shelters = shelters.head(3)
208
-
209
- # convert pandas df into list of dicts
210
- shelters = shelters.to_dict(orient='records')
211
 
212
  # Display the current shelter information
213
- shelter = shelters[st.session_state.shelter_index]
214
 
215
  st.header(f"{shelter['Organization Name']}: {shelter['Program Name']}")
216
  st.divider()
@@ -261,4 +267,5 @@ else:
261
  if st.button("Reset"):
262
  st.session_state.shelter_index = 0
263
  st.session_state.form_submitted = False
 
264
  st.experimental_rerun()
 
94
  if 'shelter_index' not in st.session_state:
95
  st.session_state.shelter_index = 0
96
 
97
+ if 'shelters_filtered' not in st.session_state:
98
+ st.session_state.shelters_filtered = False
99
+
100
  # Page config
101
  st.set_page_config(
102
  page_title="ShelterSearch",
 
142
  json.dump(data, f)
143
 
144
  st.session_state.form_submitted = True
 
145
  st.rerun()
146
  else:
147
+ if not st.session_state.shelters_filtered:
148
+ with open('data.json', 'r') as f:
149
+ data = json.load(f)
 
150
 
151
+ shelters = pd.read_csv("database.csv")
 
152
 
153
+ # filter city
154
+ shelters = shelters[(shelters['City'] == data['City'])]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
 
156
+ # filter sex
157
+ shelters = shelters[(shelters['Sex'] == data['Sex']) | (shelters['Sex'] == 'All')]
 
 
 
 
158
 
159
+ # filter lgbtq
160
+ if data['LGBTQ'] == 'No':
161
+ shelters = shelters[(shelters['LGBTQ'] == "No")]
 
 
 
 
 
 
 
 
 
 
162
 
163
+ # filter domestic violence
164
+ if data['Domestic Violence'] == "No":
165
+ shelters = shelters[(shelters['Domestic Violence'] == "No")]
166
+
167
+ # keep track of which scores are calculated
168
+ scores = []
169
+
170
+ # calculate distances between zipcodes
171
+ if data['Zip Code'] != "Unsure":
172
+ geocoding_api_key = os.environ['OpenWeather_API_KEY']
173
+
174
+ shelters_coordinates = shelters.apply(lambda row: get_coordinates(row['Zip Code'], geocoding_api_key), axis=1).tolist()
175
+ user_coordinates = get_coordinates(data['Zip Code'], geocoding_api_key)
176
 
177
+ distances = []
178
+ for coordinates in shelters_coordinates:
179
+ distances.append(haversine(coordinates[0], coordinates[1], user_coordinates[0], user_coordinates[1]))
180
 
181
+ max = max(distances) if (max(distances) != 0) else 1
182
+ shelters['zipcode_score'] = [d / max for d in distances]
183
+ scores.append('zipcode_score')
184
+
185
+ # get urgency scores
186
+ urgency_scores = shelters.apply(lambda row: get_urgency_score(data['Urgency'], row['Urgency']), axis=1).tolist()
187
+ shelters['urgency_score'] = urgency_scores
188
+ scores.append('urgency_score')
189
+
190
+ # get duration scores
191
+ duration_scores = shelters.apply(lambda row: get_duration_score(data['Duration'], row['Duration']), axis=1).tolist()
192
+ shelters['duration_score'] = duration_scores
193
+ scores.append('duration_score')
194
+
195
+ # get services scores
196
+ if data['Needs'] != "":
197
+ OpenAI_API_KEY = os.environ["OPENAI_API_KEY"]
198
+
199
+ services_scores = shelters.apply(lambda row: call_gpt(data['Needs'], row['Services'], OpenAI_API_KEY), axis=1).tolist()
200
+ services_scores = [s / 10 for s in services_scores]
201
+
202
+ shelters['services_score'] = services_scores
203
+ scores.append('services_score')
204
+
205
+ # calcualte cumulative score
206
+ shelters['total_score'] = shelters[scores].sum(axis=1)
207
+ shelters['total_score'] = shelters['total_score'] / len(scores)
208
+
209
+ shelters = shelters.sort_values(by='total_score', ascending=True)
210
+ shelters = shelters.head(3)
211
 
212
+ # convert pandas df into list of dicts
213
+ shelters = shelters.to_dict(orient='records')
 
214
 
215
+ st.session_state.shelters_filtered = True
216
+ st.session_state.shelters = shelters
 
 
 
217
 
218
  # Display the current shelter information
219
+ shelter = st.session_state.shelters[st.session_state.shelter_index]
220
 
221
  st.header(f"{shelter['Organization Name']}: {shelter['Program Name']}")
222
  st.divider()
 
267
  if st.button("Reset"):
268
  st.session_state.shelter_index = 0
269
  st.session_state.form_submitted = False
270
+ st.session_state.shelters_filtered = False
271
  st.experimental_rerun()