Yoxas commited on
Commit
5780f69
·
verified ·
1 Parent(s): 5b97bf9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -15
app.py CHANGED
@@ -3,31 +3,58 @@ import pandas as pd
3
  import numpy as np
4
  from transformers import pipeline, BertTokenizer, BertModel
5
  import faiss
 
 
6
  import spaces
7
 
8
  # Load CSV data
9
  data = pd.read_csv('RBDx10kstats.csv')
10
 
11
- # Convert embedding column from string to numpy array
12
- data['embedding'] = data['embedding'].apply(lambda x: np.fromstring(x[1:-1], sep=', '))
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  # Initialize FAISS index
15
- dimension = len(data['embedding'][0])
16
- index = faiss.IndexFlatL2(dimension)
17
- index.add(np.stack(data['embedding'].values))
18
 
19
- # Load QA model
20
- qa_model = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
 
 
 
 
21
 
22
- # Function to embed the question using BERT
23
- def embed_question(question, model, tokenizer):
24
- inputs = tokenizer(question, return_tensors='pt')
25
- outputs = model(**inputs)
26
- return outputs.last_hidden_state.mean(dim=1).detach().numpy()
 
 
 
 
27
 
28
  # Load BERT model and tokenizer
29
  tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
30
- model = BertModel.from_pretrained('bert-base-uncased')
 
 
 
 
 
 
 
31
 
32
  # Function to retrieve the relevant document and generate a response
33
  @spaces.GPU(duration=120)
@@ -36,7 +63,7 @@ def retrieve_and_generate(question):
36
  question_embedding = embed_question(question, model, tokenizer)
37
 
38
  # Search in FAISS index
39
- _, indices = index.search(question_embedding, k=1)
40
 
41
  # Retrieve the most relevant document
42
  relevant_doc = data.iloc[indices[0][0]]
@@ -50,7 +77,7 @@ def retrieve_and_generate(question):
50
  # Create a Gradio interface
51
  interface = gr.Interface(
52
  fn=retrieve_and_generate,
53
- inputs=gr.inputs.Textbox(lines=2, placeholder="Ask a question about the documents..."),
54
  outputs="text",
55
  title="RAG Chatbot",
56
  description="Ask questions about the documents in the CSV file."
 
3
  import numpy as np
4
  from transformers import pipeline, BertTokenizer, BertModel
5
  import faiss
6
+ import torch
7
+ import json
8
  import spaces
9
 
10
  # Load CSV data
11
  data = pd.read_csv('RBDx10kstats.csv')
12
 
13
+ # Function to safely convert JSON strings to numpy arrays
14
+ def safe_json_loads(x):
15
+ try:
16
+ return np.array(json.loads(x), dtype=np.float32) # Ensure the array is of type float32
17
+ except json.JSONDecodeError as e:
18
+ print(f"Error decoding JSON: {e}")
19
+ return np.array([], dtype=np.float32) # Return an empty array or handle it as appropriate
20
+
21
+ # Apply the safe_json_loads function to the embedding column
22
+ data['embedding'] = data['embedding'].apply(safe_json_loads)
23
+
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'].iloc[0])
29
+ res = faiss.StandardGpuResources() # use a single GPU
 
30
 
31
+ # Create FAISS index
32
+ if faiss.get_num_gpus() > 0:
33
+ gpu_index = faiss.IndexFlatL2(dimension)
34
+ gpu_index = faiss.index_cpu_to_gpu(res, 0, gpu_index) # move to GPU
35
+ else:
36
+ gpu_index = faiss.IndexFlatL2(dimension) # fall back to CPU
37
 
38
+ # Ensure embeddings are stacked as float32
39
+ embeddings = np.vstack(data['embedding'].values).astype(np.float32)
40
+ gpu_index.add(embeddings)
41
+
42
+ # Check if GPU is available
43
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
44
+
45
+ # Load QA model
46
+ qa_model = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad", device=0 if torch.cuda.is_available() else -1)
47
 
48
  # Load BERT model and tokenizer
49
  tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
50
+ model = BertModel.from_pretrained('bert-base-uncased').to(device)
51
+
52
+ # Function to embed the question using BERT
53
+ def embed_question(question, model, tokenizer):
54
+ inputs = tokenizer(question, return_tensors='pt').to(device)
55
+ with torch.no_grad():
56
+ outputs = model(**inputs)
57
+ return outputs.last_hidden_state.mean(dim=1).cpu().numpy().astype(np.float32)
58
 
59
  # Function to retrieve the relevant document and generate a response
60
  @spaces.GPU(duration=120)
 
63
  question_embedding = embed_question(question, model, tokenizer)
64
 
65
  # Search in FAISS index
66
+ _, indices = gpu_index.search(question_embedding, k=1)
67
 
68
  # Retrieve the most relevant document
69
  relevant_doc = data.iloc[indices[0][0]]
 
77
  # Create a Gradio interface
78
  interface = gr.Interface(
79
  fn=retrieve_and_generate,
80
+ inputs=gr.Textbox(lines=2, placeholder="Ask a question about the documents..."),
81
  outputs="text",
82
  title="RAG Chatbot",
83
  description="Ask questions about the documents in the CSV file."