Spaces:
Build error
Build error
Commit
·
3ae17c8
1
Parent(s):
f419ec9
Update weaviate_utils.py
Browse files- weaviate_utils.py +19 -23
weaviate_utils.py
CHANGED
|
@@ -22,37 +22,33 @@ def map_dtype_to_weaviate(dtype):
|
|
| 22 |
else:
|
| 23 |
return "string"
|
| 24 |
|
| 25 |
-
def
|
| 26 |
-
# Create class schema
|
| 27 |
class_schema = {
|
| 28 |
"class": class_name,
|
| 29 |
"description": class_description,
|
| 30 |
-
"properties": []
|
| 31 |
}
|
| 32 |
-
|
| 33 |
-
# Try to create the class without properties first
|
| 34 |
try:
|
| 35 |
client.schema.create({"classes": [class_schema]})
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
"description": f"Property for {column_name}",
|
| 45 |
-
"dataType": [map_dtype_to_weaviate(data_type)]
|
| 46 |
-
}
|
| 47 |
-
try:
|
| 48 |
-
client.schema.property.create(class_name, property_schema)
|
| 49 |
-
except weaviate.exceptions.SchemaValidationException:
|
| 50 |
-
# Property might already exist, so we can continue
|
| 51 |
-
pass
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
def get_class_schema(client, class_name):
|
| 58 |
try:
|
|
|
|
| 22 |
else:
|
| 23 |
return "string"
|
| 24 |
|
| 25 |
+
def create_new_class_schema(client, class_name, class_description):
|
|
|
|
| 26 |
class_schema = {
|
| 27 |
"class": class_name,
|
| 28 |
"description": class_description,
|
| 29 |
+
"properties": []
|
| 30 |
}
|
|
|
|
|
|
|
| 31 |
try:
|
| 32 |
client.schema.create({"classes": [class_schema]})
|
| 33 |
+
st.success(f"Class {class_name} created successfully!")
|
| 34 |
+
except Exception as e:
|
| 35 |
+
st.error(f"Error creating class: {e}")
|
| 36 |
|
| 37 |
+
def ingest_data_to_weaviate(client, csv_file, selected_class):
|
| 38 |
+
# Convert CSV to DataFrame
|
| 39 |
+
data = csv_file.read().decode("utf-8")
|
| 40 |
+
dataframe = pd.read_csv(StringIO(data))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# Check if columns match the selected class schema
|
| 43 |
+
class_schema = get_class_schema(client, selected_class)
|
| 44 |
+
if class_schema:
|
| 45 |
+
schema_columns = [prop["name"] for prop in class_schema["properties"]]
|
| 46 |
+
if set(dataframe.columns) == set(schema_columns):
|
| 47 |
+
data = dataframe.to_dict(orient="records")
|
| 48 |
+
client.data_object.create(data, selected_class)
|
| 49 |
+
st.success("Data ingested successfully!")
|
| 50 |
+
else:
|
| 51 |
+
st.error("The columns in the uploaded CSV do not match the schema of the selected class.")
|
| 52 |
|
| 53 |
def get_class_schema(client, class_name):
|
| 54 |
try:
|