Spaces:
Sleeping
Sleeping
import streamlit as st | |
import json | |
import pandas as pd | |
import requests | |
import os | |
import math | |
from openai import OpenAI | |
def call_gpt(user_needs, shelter_services): | |
client = OpenAI(os.environ("OPENAI_API_KEY")) | |
completion = client.chat.completions.create( | |
model="gpt-4o-mini", | |
messages=[ | |
{"role": "system", "content": "Given two variables 'user needs' (the ideal qualities/services of a shelter) and 'shelter services' (the services offered by a shelter), return an integer 0-10 that scores how well the 'shelter services' match the 'user needs' where 0 is the best fit and 10 is the worst fit. IMPORTANT: NO MATTER WHAT, ONLY RETURN THE INTEGER (NO EXTRA WORDS, PUNCTUATION, ETC.)"}, | |
{"role": "user", "content": f"user_needs: {user_needs}, shelter_services: {shelter_services}"} | |
] | |
) | |
score = completion.choices[0].message.content.strip() | |
return int(score) | |
def get_urgency_score(user, shelter): | |
if user == "Today": | |
if shelter == "Immidiate": return 0 | |
if shelter == "High": return 0.75 | |
if shelter == "Moderate": return 1 | |
elif user == "In the next few days": | |
if shelter == "Immidiate": return 0.25 | |
if shelter == "High": return 0 | |
if shelter == "Moderate": return 0.75 | |
elif user == "In a week or so": | |
if shelter == "Immidiate": return 0.75 | |
if shelter == "High": return 0.25 | |
if shelter == "Moderate": return 0 | |
def get_duration_score(user, shelter): | |
if user == "Overnight": | |
if shelter == "Overnight": return 0 | |
if shelter == "Temporary": return 0.5 | |
if shelter == "Transitional": return 0.75 | |
if shelter == "Long-Term": return 1 | |
elif user == "A month or less": | |
if shelter == "Overnight": return 0.5 | |
if shelter == "Temporary": return 0 | |
if shelter == "Transitional": return 0.25 | |
if shelter == "Long-Term": return 0.75 | |
elif user == "A couple of months": | |
if shelter == "Overnight": return 0.75 | |
if shelter == "Temporary": return 0.25 | |
if shelter == "Transitional": return 0 | |
if shelter == "Long-Term": return 0.5 | |
elif user == "A year or more": | |
if shelter == "Overnight": return 1 | |
if shelter == "Temporary": return 0.75 | |
if shelter == "Transitional": return 0.5 | |
if shelter == "Long-Term": return 0 | |
def get_coordinates(zipcode: str, api_key: str) -> list: | |
""" | |
Get the coordinates (latitude and longitude) of an address using the OpenWeather Geocoding API. | |
Parameters: | |
zipcode (str): The zipcode to geocode. | |
api_key (str): Your OpenWeather API key. | |
Returns: | |
list: A list containing the latitude and longitude of the address. | |
""" | |
base_url = "http://api.openweathermap.org/geo/1.0/zip" | |
params = { | |
'zip': str(zipcode) + ",US", | |
'appid': api_key | |
} | |
response = requests.get(base_url, params=params) | |
data = response.json() | |
return [data.get('lat'), data.get('lon')] | |
def haversine(lat1, lon1, lat2, lon2): | |
R = 6371 # Earth radius in kilometers. Use 3956 for miles. | |
dlat = math.radians(lat2 - lat1) | |
dlon = math.radians(lon2 - lon1) | |
a = math.sin(dlat / 2) ** 2 + math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) * math.sin(dlon / 2) ** 2 | |
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a)) | |
distance = R * c | |
return distance | |
# Function to handle form submission | |
def form_dialog(): | |
city = st.selectbox("City", ['San Francisco', 'Oakland', 'Berkeley']) | |
zipcode = st.text_input("Zipcode") | |
sex = st.radio("Sex", ["Male", "Female", "Other"]) | |
lgbtq = st.radio("Do you identify as LGBTQ+ (some shelters serve this community specifically)", ["No", "Yes"]) | |
domestic_violence = st.radio("Have you experienced domestic violence (some shelters serve these individuals specifically", ["No", "Yes"]) | |
urgency = st.radio("How quickly do you need help?", ("Today", "In the next few days", "In a week or more")) | |
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")) | |
needs = st.text_area("Optional - Needs (tell us what you need and how we can help)") | |
if st.button("Submit"): | |
data = { | |
"City": city, | |
"Zip Code": zipcode, | |
"Sex": sex, | |
"LGBTQ": lgbtq, | |
"Domestic Violence": domestic_violence, | |
"Urgency": urgency, | |
"Duration": duration, | |
"Needs": needs | |
} | |
with open('data.json', 'w') as f: | |
json.dump(data, f) | |
st.session_state.form_submitted = True | |
st.session_state.data = data | |
st.rerun() | |
# Initialize session state | |
if 'form_submitted' not in st.session_state: | |
st.session_state.form_submitted = False | |
if 'shelter_index' not in st.session_state: | |
st.session_state.shelter_index = 0 | |
# Page config | |
st.set_page_config( | |
page_title="ShelterSearch", | |
layout="wide", | |
) | |
st.title("ShelterSearch") | |
if not st.session_state.form_submitted: | |
if st.button("Open Form"): | |
form_dialog() | |
else: | |
with open('data.json', 'r') as f: | |
data = json.load(f) | |
st.json(data) | |
# shelters = pd.read_csv("database.csv") | |
# # filter city | |
# shelters = shelters[(shelters['City'] == data['City'])] | |
# # filter sex | |
# shelters = shelters[(shelters['Sex'] == data['Sex']) | (shelters['Sex'] == 'All')] | |
# # filter lgbtq | |
# if data['LGBTQ'] == 'No': | |
# shelters = shelters[(shelters['LGBTQ'] == "No")] | |
# # filter domestic violence | |
# if data['Domestic Violence'] == "No": | |
# shelters = shelters[(shelters['Domestic Violence'] == "No")] | |
# # calculate distances between zipcodes | |
# if data['Zip Code'] != "Unsure": | |
# geocoding_api_key = os.environ['OpenWeather_API_KEY'] | |
# shelters_coordinates = shelters.apply(lambda row: get_coordinates(row['Zip Code'], geocoding_api_key), axis=1).tolist() | |
# user_coordinates = get_coordinates(data['Zip Code'], geocoding_api_key) | |
# distances = [] | |
# for coordinates in shelters_coordinates: | |
# distances.append(haversine(coordinates[0], coordinates[1], user_coordinates[0], user_coordinates[1])) | |
# max = max(distances) if (max(distances) != 0) else 1 | |
# shelters['zipcode_score'] = [d / max for d in distances] | |
# # get urgency scores | |
# urgency_scores = shelters.apply(lambda row: get_urgency_score(data['Urgency'], row['Urgency']), axis=1).tolist() | |
# shelters['urgency_score'] = urgency_scores | |
# # get duration scores | |
# duration_scores = shelters.apply(lambda row: get_duration_score(data['Duration'], row['Duration']), axis=1).tolist() | |
# shelters['duration_score'] = duration_scores | |
# # services | |
# if data['Needs'] != "": | |
# services_scores = shelters.apply(lambda row: call_gpt(data['Needs'], row['Services']), axis=1).tolist() | |
# services_scores = [s / 10 for s in services_scores] | |
# shelters['services_score'] = services_scores | |
# st.table(shelters) | |
# shelters = [ | |
# {"title": "Shelter 1", "description": "This is the 1st shelter",}, | |
# {"title": "Shelter 2", "description": "This is the 2nd shelter.",}, | |
# {"title": "Shelter 3", "description": "This is the 3rd shelter.",} | |
# ] | |
# # Display the current shelter information | |
# shelter = shelters[st.session_state.shelter_index] | |
# st.write(shelter["description"]) | |
# # Create two columns | |
# col1, col2 = st.columns([1,1]) | |
# # Add buttons to each column | |
# with col1: | |
# if st.button("Previous"): | |
# if st.session_state.shelter_index > 0: | |
# st.session_state.shelter_index -= 1 | |
# st.experimental_rerun() | |
# with col2: | |
# if st.button("Next"): | |
# if st.session_state.shelter_index < len(shelters) - 1: | |
# st.session_state.shelter_index += 1 | |
# st.experimental_rerun() |