jskinner215 commited on
Commit
203bb0f
·
1 Parent(s): 4758881

modified ingest_data_to_weaviate function

Browse files

modified
def ingest_data_to_weaviate(dataframe, class_name, class_description):
# Create class schema
class_schema = {
"class": class_name,
"description": class_description,
"properties": [] # Start with an empty properties list
}

# Try to create the class without properties first
try:
client.schema.create({"classes": [class_schema]})
except weaviate.exceptions.SchemaValidationException:
# Class might already exist, so we can continue
pass

# Now, let's add properties to the class
for column_name, data_type in zip(dataframe.columns, dataframe.dtypes):
property_schema = {
"name": column_name,
"description": f"Property for {column_name}",
"dataType": [map_dtype_to_weaviate(data_type)]
}
try:
client.schema.property.create(class_name, property_schema)
except weaviate.exceptions.SchemaValidationException:
# Property might already exist, so we can continue
pass

# Ingest data
for index, row in dataframe.iterrows():
obj = {
"class": class_name,
"id": str(index),
"properties": row.to_dict()
}
client.data_object.create(obj)

Files changed (1) hide show
  1. app.py +30 -29
app.py CHANGED
@@ -19,41 +19,42 @@ client = weaviate.Client(
19
  )
20
 
21
  def ingest_data_to_weaviate(dataframe, class_name, class_description):
22
- properties = []
23
- for column in dataframe.columns:
24
- data_type = "string"
25
- if dataframe[column].dtype == "float64":
26
- data_type = "float"
27
- elif dataframe[column].dtype == "int64":
28
- data_type = "int"
29
- properties.append({
30
- "name": column,
31
- "description": column,
32
- "dataType": [data_type]
33
- })
34
-
35
- schema = {
36
- "classes": [
37
- {
38
- "class": class_name,
39
- "description": class_description,
40
- "properties": properties
41
- }
42
- ]
43
  }
44
-
45
- # Create Schema in Weaviate
46
- client.schema.create(schema)
47
 
48
- # Ingest Data
49
- batch_request = weaviate.ObjectsBatchRequest()
50
- for _, row in dataframe.iterrows():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  obj = {
52
  "class": class_name,
 
53
  "properties": row.to_dict()
54
  }
55
- batch_request.add(obj)
56
- client.batch.create(batch_request)
57
 
58
  def query_weaviate(question):
59
  # This is a basic example; adapt the query based on the question
 
19
  )
20
 
21
  def ingest_data_to_weaviate(dataframe, class_name, class_description):
22
+ # Create class schema
23
+ class_schema = {
24
+ "class": class_name,
25
+ "description": class_description,
26
+ "properties": [] # Start with an empty properties list
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  }
 
 
 
28
 
29
+ # Try to create the class without properties first
30
+ try:
31
+ client.schema.create({"classes": [class_schema]})
32
+ except weaviate.exceptions.SchemaValidationException:
33
+ # Class might already exist, so we can continue
34
+ pass
35
+
36
+ # Now, let's add properties to the class
37
+ for column_name, data_type in zip(dataframe.columns, dataframe.dtypes):
38
+ property_schema = {
39
+ "name": column_name,
40
+ "description": f"Property for {column_name}",
41
+ "dataType": [map_dtype_to_weaviate(data_type)]
42
+ }
43
+ try:
44
+ client.schema.property.create(class_name, property_schema)
45
+ except weaviate.exceptions.SchemaValidationException:
46
+ # Property might already exist, so we can continue
47
+ pass
48
+
49
+ # Ingest data
50
+ for index, row in dataframe.iterrows():
51
  obj = {
52
  "class": class_name,
53
+ "id": str(index),
54
  "properties": row.to_dict()
55
  }
56
+ client.data_object.create(obj)
57
+
58
 
59
  def query_weaviate(question):
60
  # This is a basic example; adapt the query based on the question