-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulate_from_csv.py
More file actions
132 lines (111 loc) · 4.7 KB
/
Copy pathpopulate_from_csv.py
File metadata and controls
132 lines (111 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import csv
import weaviate
import uuid
from datetime import datetime
import os
from weaviate_schema import setup_weaviate_client
from shelter_utils import geocode_address
# Format date in RFC3339 format
def rfc3339_date():
return datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
def read_hotels_from_csv(csv_file_path):
"""Read hotel data from CSV file."""
hotels = []
try:
with open(csv_file_path, 'r', encoding='utf-8') as csv_file:
# Skip the first 3 rows (header info)
for _ in range(3):
next(csv_file)
csv_reader = csv.reader(csv_file)
for row in csv_reader:
if len(row) >= 3 and row[0] and row[1]: # Ensure we have at least hotel name and address
hotel = {
"name": row[0].strip(),
"address": row[1].strip(),
"website": row[2].strip() if len(row) > 2 and row[2] else "",
"phoneNumber": row[3].strip() if len(row) > 3 and row[3] else "No phone available",
"notes": row[4].strip() if len(row) > 4 and row[4] else ""
}
hotels.append(hotel)
except Exception as e:
print(f"Error reading CSV file: {e}")
return hotels
def convert_hotels_to_shelters(hotels):
"""Convert hotel data to shelter format."""
shelters = []
for hotel in hotels:
# Get geocode for the address
location = geocode_address(hotel["address"])
if not location:
print(f"Could not geocode address for {hotel['name']}, skipping...")
continue
# Create shelter object
shelter = {
"name": hotel["name"],
"description": f"Hotel available for evacuees. {hotel.get('notes', '')}",
"location": location,
"address": hotel["address"],
"currentCapacity": 10, # Default values since we don't have this information
"maximumCapacity": 50,
"hasSpace": True,
"services": ["Temporary Housing", "Evacuation Support"],
"amenities": ["Beds", "Bathroom"],
"restrictions": [],
"acceptsPets": "pet" in hotel.get("notes", "").lower(), # Guess based on notes
"accessibilityFeatures": ["Standard Hotel Accessibility"],
"phoneNumber": hotel["phoneNumber"],
"email": "",
"website": hotel["website"],
"operatingHours": "24/7",
"lastUpdated": rfc3339_date(),
"shelterType": "Emergency",
"disasterResponse": ["Fire", "Evacuation"],
"languages": ["English"]
}
shelters.append(shelter)
return shelters
def add_shelters_to_weaviate(client, shelters):
"""Add shelters to Weaviate."""
print(f"Adding {len(shelters)} shelters to Weaviate...")
success_count = 0
error_count = 0
for shelter in shelters:
try:
# Generate a UUID based on the shelter name for consistency
shelter_uuid = uuid.uuid5(uuid.NAMESPACE_DNS, shelter["name"])
# Add the shelter to Weaviate
client.data_object.create(
data_object=shelter,
class_name="Shelter",
uuid=str(shelter_uuid)
)
print(f"Added shelter: {shelter['name']}")
success_count += 1
except Exception as e:
print(f"Error adding shelter {shelter['name']}: {e}")
error_count += 1
print(f"Finished adding shelters. Success: {success_count}, Errors: {error_count}")
def main():
"""Main function to read CSV and populate Weaviate."""
csv_file_path = "shelters.csv"
if not os.path.exists(csv_file_path):
print(f"Error: File {csv_file_path} not found.")
return
# Read hotels from CSV
print(f"Reading hotels from {csv_file_path}...")
hotels = read_hotels_from_csv(csv_file_path)
print(f"Found {len(hotels)} hotels in CSV file.")
# Convert hotels to shelters
print("Converting hotels to shelter format...")
shelters = convert_hotels_to_shelters(hotels)
print(f"Converted {len(shelters)} hotels to shelter format.")
# Create a Weaviate client
client = setup_weaviate_client()
# Check if we already have data
result = client.query.get("Shelter", ["name"]).do()
existing_shelters = len(result["data"]["Get"]["Shelter"]) if result["data"]["Get"]["Shelter"] else 0
print(f"Found {existing_shelters} existing shelters in Weaviate.")
# Add shelters to Weaviate
add_shelters_to_weaviate(client, shelters)
if __name__ == "__main__":
main()