-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvectorDatabaseInit.py
More file actions
58 lines (40 loc) · 1.43 KB
/
vectorDatabaseInit.py
File metadata and controls
58 lines (40 loc) · 1.43 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
from docx import Document
from sentence_transformers import SentenceTransformer
import re
import faiss
import numpy as np
from google import genai
import os
import requests
from dotenv import load_dotenv
load_dotenv()
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
# gemini api
client = genai.Client(api_key=GOOGLE_API_KEY)
def extract_text_from_docx(file_path):
doc = Document(file_path)
return "\n".join([para.text for para in doc.paragraphs if para.text.strip()])
def chunk_text(text, chunk_size=300, overlap=50):
words = text.split() # Split text into words
chunks = []
for i in range(0, len(words), chunk_size - overlap):
chunk = " ".join(words[i : i + chunk_size]) # Create chunk
chunks.append(chunk)
return chunks
# Example Usage
doc_text = extract_text_from_docx("portfolioContent.docx")
text_chunks = chunk_text(doc_text)
# Print len chunks for verification
print(len(text_chunks))
# Load Pre-trained Model
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
chunk_embeddings = embed_model.encode(text_chunks)
print("Embeddings Shape:", chunk_embeddings.shape)
# Convert to NumPy Array
embeddings_array = np.array(chunk_embeddings, dtype="float32")
# Create FAISS Index
index = faiss.IndexFlatL2(embeddings_array.shape[1]) # L2 Distance
index.add(embeddings_array)
# Save FAISS Index
faiss.write_index(index, "vector_database.index")
print("FAISS Index Created & Saved Successfully")