Yoxas commited on
Commit
0d2fb97
·
verified ·
1 Parent(s): 6f729e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -17
app.py CHANGED
@@ -24,28 +24,25 @@ data['embedding'] = data['embedding'].apply(safe_json_loads)
24
  # Filter out any rows with empty embeddings
25
  data = data[data['embedding'].apply(lambda x: x.size > 0)]
26
 
27
- # Check if the DataFrame is empty after filtering
28
- if data.empty:
29
- raise RuntimeError("No valid embeddings found in the data.")
30
-
31
  # Initialize FAISS index
32
- dimension = len(data['embedding'].iloc[0])
33
- gpu_available = torch.cuda.is_available()
34
-
35
- # Initialize FAISS resources and index
36
- res = faiss.StandardGpuResources() if gpu_available else None
37
- index = faiss.IndexFlatL2(dimension)
38
 
39
- if gpu_available:
40
- index = faiss.index_cpu_to_gpu(res, 0, index) # move to GPU
 
 
 
 
 
41
 
42
- index.add(np.stack(data['embedding'].values))
43
 
44
- # Set the device
45
- device = torch.device('cuda' if gpu_available else 'cpu')
46
 
47
  # Load QA model
48
- qa_model = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad", device=0 if gpu_available else -1)
49
 
50
  # Load BERT model and tokenizer
51
  tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
@@ -65,7 +62,7 @@ def retrieve_and_generate(question):
65
  question_embedding = embed_question(question, model, tokenizer)
66
 
67
  # Search in FAISS index
68
- _, indices = index.search(question_embedding, k=1)
69
 
70
  # Retrieve the most relevant document
71
  relevant_doc = data.iloc[indices[0][0]]
 
24
  # Filter out any rows with empty embeddings
25
  data = data[data['embedding'].apply(lambda x: x.size > 0)]
26
 
 
 
 
 
27
  # Initialize FAISS index
28
+ dimension = len(data['embedding'][0])
29
+ res = faiss.StandardGpuResources() # use a single GPU
 
 
 
 
30
 
31
+ # Check available GPU devices
32
+ num_gpus = faiss.get_num_gpus()
33
+ if num_gpus > 0:
34
+ gpu_index = faiss.IndexFlatL2(dimension)
35
+ gpu_index = faiss.index_cpu_to_gpu(res, 0, gpu_index) # move to GPU
36
+ else:
37
+ raise RuntimeError("No GPU devices available.")
38
 
39
+ gpu_index.add(np.stack(data['embedding'].values))
40
 
41
+ # Check if GPU is available
42
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
43
 
44
  # Load QA model
45
+ qa_model = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad", device=0 if torch.cuda.is_available() else -1)
46
 
47
  # Load BERT model and tokenizer
48
  tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
 
62
  question_embedding = embed_question(question, model, tokenizer)
63
 
64
  # Search in FAISS index
65
+ _, indices = gpu_index.search(question_embedding, k=1)
66
 
67
  # Retrieve the most relevant document
68
  relevant_doc = data.iloc[indices[0][0]]