Zaious commited on
Commit
d2fbef1
·
verified ·
1 Parent(s): 150d61f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -7
app.py CHANGED
@@ -16,19 +16,23 @@ import faiss
16
  # Initialize OpenAI client
17
  client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
18
 
19
- def initialize_embeddings_from_faiss(faiss_path: str):
20
- """Load product embeddings directly from FAISS index"""
 
 
 
21
  if not os.path.exists(faiss_path):
22
  raise FileNotFoundError(f"FAISS index file not found at {faiss_path}")
23
 
 
 
 
 
24
  print(f"Loading FAISS index from {faiss_path}...")
25
  index = faiss.read_index(faiss_path)
26
 
27
- # Extract embeddings from FAISS index
28
- product_embeddings_array = faiss.vector_to_array(index.xb).reshape(index.ntotal, index.d)
29
  print(f"FAISS index loaded with {index.ntotal} embeddings.")
30
-
31
- return index, product_embeddings_array
32
 
33
  # Load CSV data
34
  #df = pd.read_csv("item_new.csv", encoding='utf-8')
@@ -37,7 +41,11 @@ def initialize_embeddings_from_faiss(faiss_path: str):
37
  # Load embeddings from FAISS
38
  print("Initializing embeddings...")
39
  faiss_path = "product_index.faiss" # Path to FAISS index file
40
- faiss_index, product_embeddings_array = initialize_embeddings_from_faiss(faiss_path)
 
 
 
 
41
  print("Embeddings initialized")
42
 
43
 
 
16
  # Initialize OpenAI client
17
  client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
18
 
19
+ def initialize_embeddings_from_pkl(pkl_path: str, faiss_path: str):
20
+ """ PKL 檔案載入嵌入,並加載 FAISS 索引"""
21
+ if not os.path.exists(pkl_path):
22
+ raise FileNotFoundError(f"Embedding file not found at {pkl_path}")
23
+
24
  if not os.path.exists(faiss_path):
25
  raise FileNotFoundError(f"FAISS index file not found at {faiss_path}")
26
 
27
+ print(f"Loading embeddings from {pkl_path}...")
28
+ with open(pkl_path, "rb") as f:
29
+ embeddings = pickle.load(f)
30
+
31
  print(f"Loading FAISS index from {faiss_path}...")
32
  index = faiss.read_index(faiss_path)
33
 
 
 
34
  print(f"FAISS index loaded with {index.ntotal} embeddings.")
35
+ return index, embeddings
 
36
 
37
  # Load CSV data
38
  #df = pd.read_csv("item_new.csv", encoding='utf-8')
 
41
  # Load embeddings from FAISS
42
  print("Initializing embeddings...")
43
  faiss_path = "product_index.faiss" # Path to FAISS index file
44
+ pkl_path = "product_embeddings.pkl" # Path to embeddings file
45
+
46
+ # 初始化嵌入
47
+ faiss_index, product_embeddings_array = initialize_embeddings_from_pkl(pkl_path, faiss_path)
48
+ print(f"Loaded embeddings with shape: {len(product_embeddings_array)} x {len(product_embeddings_array[0])}")
49
  print("Embeddings initialized")
50
 
51