Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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']
|
33 |
-
|
34 |
-
|
35 |
-
# Initialize FAISS resources and index
|
36 |
-
res = faiss.StandardGpuResources() if gpu_available else None
|
37 |
-
index = faiss.IndexFlatL2(dimension)
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
|
43 |
|
44 |
-
#
|
45 |
-
device = torch.device('cuda' if
|
46 |
|
47 |
# Load QA model
|
48 |
-
qa_model = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad", device=0 if
|
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 =
|
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]]
|