Spaces:
Build error
Build error
Commit ·
1cb0871
1
Parent(s): 5e4315c
Update weaviate_utils.py
Browse files35 unmodified lines3637383940414243444546474849505152535455565735 unmodified linesst.success(f"Class {class_name} created successfully!")except Exception as e:st.error(f"Error creating class: {e}")def ingest_data_to_weaviate(client, csv_file, selected_class):# Convert CSV to DataFramedata = csv_file.read().decode("utf-8")dataframe = pd.read_csv(StringIO(data))# Check if columns match the selected class schemaclass_schema = get_class_schema(client, selected_class)if class_schema:schema_columns = [prop["name"] for prop in class_schema["properties"]]if set(dataframe.columns) == set(schema_columns):data = dataframe.to_dict(orient="records")client.data_object.create(data, selected_class)st.success("Data ingested successfully!")else:st.error("The columns in the uploaded CSV do not match the schema of the selected class.")def get_class_schema(client, class_name):try:35 unmodified lines3637383940414243444546474849505152535455565758596061626364656667686970717273747576777835 unmodified linesst.success(f"Class {class_name} created successfully!")except Exception as e:st.error(f"Error creating class: {e}")def ingest_data_to_weaviate(client, csv_file, selected_class):# Read the CSV datadata = csv_file.read().decode("utf-8")dataframe = pd.read_csv(StringIO(data))# Fetch the schema for the selected classclass_schema = get_class_schema(client, selected_class)# If the schema is empty, create it based on the CSV columnsif not class_schema or not class_schema["properties"]: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(selected_class, property_schema)except weaviate.exceptions.SchemaValidationException:# Property might already exist, so we can continuepasselse:# If the schema is not empty, compare it with the CSV columnsschema_columns = [prop["name"] for prop in class_schema["properties"]]if set(dataframe.columns) != set(schema_columns):st.error("The columns in the uploaded CSV do not match the schema of the selected class. Please check and upload the correct CSV or create a new class.")return# Ingest the data into Weaviatedata = dataframe.to_dict(orient="records")client.data_object.create(data, selected_class)# Display a preview of the ingested datast.write(f"Your CSV was successfully integrated into the vector database under the class '{selected_class}'")st.write(dataframe.head()) # Display the first few rows of the dataframe as a previewdef get_class_schema(client, class_name):try:
weaviate_utils.py+31-10