Yoxas commited on
Commit
8f37923
·
verified ·
1 Parent(s): 551f52d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -6
app.py CHANGED
@@ -4,18 +4,26 @@ import numpy as np
4
  from transformers import pipeline, BertTokenizer, BertModel
5
  import faiss
6
  import torch
 
7
  import spaces
8
  # Load CSV data
9
  data = pd.read_csv('RB10kstats.csv')
10
 
11
- # Convert embedding column from string to numpy array
12
- data['embeddings'] = data['embeddings'].apply(lambda x: np.fromstring(x[1:-1], sep=', '))
13
 
14
  # Initialize FAISS index
15
  dimension = len(data['embeddings'][0])
16
  res = faiss.StandardGpuResources() # use a single GPU
17
- index = faiss.IndexFlatL2(dimension)
18
- gpu_index = faiss.index_cpu_to_gpu(res, 0, index) # move to GPU
 
 
 
 
 
 
 
19
  gpu_index.add(np.stack(data['embeddings'].values))
20
 
21
  # Check if GPU is available
@@ -29,7 +37,6 @@ tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
29
  model = BertModel.from_pretrained('bert-base-uncased').to(device)
30
 
31
  # Function to embed the question using BERT
32
- @spaces.GPU(duration=120)
33
  def embed_question(question, model, tokenizer):
34
  inputs = tokenizer(question, return_tensors='pt').to(device)
35
  with torch.no_grad():
@@ -64,4 +71,4 @@ interface = gr.Interface(
64
  )
65
 
66
  # Launch the Gradio app
67
- interface.launch()
 
4
  from transformers import pipeline, BertTokenizer, BertModel
5
  import faiss
6
  import torch
7
+ import json
8
  import spaces
9
  # Load CSV data
10
  data = pd.read_csv('RB10kstats.csv')
11
 
12
+ # Convert embedding column from JSON string to numpy array
13
+ data['embeddings'] = data['embeddings'].apply(lambda x: np.array(json.loads(x)))
14
 
15
  # Initialize FAISS index
16
  dimension = len(data['embeddings'][0])
17
  res = faiss.StandardGpuResources() # use a single GPU
18
+
19
+ # Check available GPU devices
20
+ num_gpus = faiss.get_num_gpus()
21
+ if num_gpus > 0:
22
+ gpu_index = faiss.IndexFlatL2(dimension)
23
+ gpu_index = faiss.index_cpu_to_gpu(res, 0, gpu_index) # move to GPU
24
+ else:
25
+ raise RuntimeError("No GPU devices available.")
26
+
27
  gpu_index.add(np.stack(data['embeddings'].values))
28
 
29
  # Check if GPU is available
 
37
  model = BertModel.from_pretrained('bert-base-uncased').to(device)
38
 
39
  # Function to embed the question using BERT
 
40
  def embed_question(question, model, tokenizer):
41
  inputs = tokenizer(question, return_tensors='pt').to(device)
42
  with torch.no_grad():
 
71
  )
72
 
73
  # Launch the Gradio app
74
+ interface.launch()