Spaces:
Build error
modified ingest_data_to_weaviate function
Browse filesmodified
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)
18 unmodified lines192021222324252627282930313233343536373839404142222324252627444546284849502930313233343536373839404142434445464748495051525354555556565758596018 unmodified lines)
def ingest_data_to_weaviate(dataframe, class_name, class_description): properties = [] for column in dataframe.columns: data_type = "string" if dataframe[column].dtype == "float64": data_type = "float" elif dataframe[column].dtype == "int64": data_type = "int" properties.append({ "name": column, "description": column, "dataType": [data_type] }) schema = { "classes": [ { "class": class_name, "description": class_description, "properties": properties } ] # Create class schema class_schema = { "class": class_name, "description": class_description, "properties": [] # Start with an empty properties list }
# Create Schema in Weaviate client.schema.create(schema) # Ingest Data batch_request = weaviate.ObjectsBatchRequest() for _, row in dataframe.iterrows(): # 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() } batch_request.add(obj) client.batch.create(batch_request) client.data_object.create(obj)
def query_weaviate(question): # This is a basic example; adapt the query based on the question