Repository navigation
Expand file tree
/
Copy pathcreateDatabase.py
More file actions
133 lines (92 loc) · 4.09 KB
/
Copy pathcreateDatabase.py
File metadata and controls
133 lines (92 loc) · 4.09 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
import weaviate # type: ignore
import weaviate.classes as wvc # type: ignore
# import numpy as np
import pandas as pd # type: ignore
from sentence_transformers import SentenceTransformer # type: ignore
import warnings
import argparse
# Suppressing warning
warnings.filterwarnings("ignore", category=FutureWarning, message=".*resume_download.*")
# Handling arguments for python Scripts
def main(arg1, arg2, arg3):
print(f'Model: {arg1}')
print(f'Weaviate Collection : {arg2}')
print(f'CSV Path : {arg3}')
MODEL_DICT = {
'mini-6' : "all-MiniLM-L6-v2",
"mini-12" : 'all-MiniLM-L12-v2',
'paraphrase-6' : "paraphrase-MiniLM-L6-v2",
'paraphrase-12' : "paraphrase-MiniLM-L12-v2",
}
# MODEL_LIST = ["sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2", "paraphrase-MiniLM-L6-v2", "all-MiniLM-L6-v2", "paraphrase-MiniLM-L12-v2"]
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Script for creating a weaviate Database from CSV")
parser.add_argument('--model', type=str, default='mini-6', help='Set the Vectorizer Model from the given : '.join(MODEL_DICT.keys()).join(MODEL_DICT.values()))
parser.add_argument('--col', type=str, default='Products', help='Set the Collection in weaviate Database')
parser.add_argument('--csv', type=str, default='datasets_csv/Products.csv', help='Set the path of CSV to vectorize and insert in DB')
args = parser.parse_args()
main(args.model, args.col, args.csv)
MODEL_NAME = MODEL_DICT[args.model]
collection_name = args.col
CSV_PATH = args.csv
# Setting the model
model = SentenceTransformer(MODEL_NAME)
# Importing the CSV
df = pd.read_csv(CSV_PATH, on_bad_lines="skip")
# Initialize an empty dictionary to store lists
column_lists = {}
# Iterate through each column in the DataFrame
for column in df.columns:
column_lists[column] = df[column].tolist()
# Example Access
# displayNames = column_lists['productDisplayName']
# masterCategories = column_lists['masterCategory']
# subCategories = column_lists['subCategory']
# articleTypes = column_lists['articleType']
# baseColours = column_lists['baseColour']
# seasons = column_lists['season']
# years = column_lists['year']
# usages = column_lists['usage']
# descriptions = column_lists['description']
# averageRatings = column_lists['averageRating']id
# numberOfRatings = column_lists['numberOfRatings']
# Prices = column_lists['Price']
# Setting the text that will converted to vector and stored in the database
texts_to_vectorize = column_lists['description']
# Encoding/Vectorize/Embedding the texts_toVectorize
vectors = model.encode(texts_to_vectorize)
# Creating a weaviate client and connection to the weaviate running in docker
client = weaviate.connect_to_local()
# Creating a fresh collection in weaviate Vector DB
if client.collections.exists(collection_name):
client.collections.delete(collection_name)
products = client.collections.create(
collection_name,
vectorizer_config=wvc.config.Configure.Vectorizer.none()
)
# Creating a list of wvc.data.DataObject in put in product.data.insert_many()
prod_objs = [
wvc.data.DataObject(
properties = {
'product_id': column_lists['id'][i],
'productDisplayName' : column_lists['productDisplayName'][i],
'masterCategory' : column_lists['masterCategory'][i],
'subCategory' : column_lists['subCategory'][i],
'articleType' : column_lists['articleType'][i],
'baseColour' : column_lists['baseColour'][i],
'season' : column_lists['season'][i],
'year' : column_lists['year'][i],
'usage' : column_lists['usage'][i],
'description' : column_lists['description'][i],
'averageRating' : column_lists['averageRating'][i],
'numberOfRatings' : column_lists['numberOfRatings'][i],
'Price' : column_lists['Price'][i],
'imagePath' : f"images/{column_lists['id'][i]}.jpg"
},
vector = vectors[i].tolist()
)
for i in range(len(df))
]
products = client.collections.get(collection_name)
products.data.insert_many(prod_objs)
client.close()