Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,63 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
-
|
|
|
|
|
|
|
4 |
import spaces
|
5 |
-
|
6 |
# Load CSV data
|
7 |
-
data = pd.read_csv('
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
# Load
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# Function to retrieve the relevant document and generate a response
|
14 |
@spaces.GPU(duration=120)
|
15 |
def retrieve_and_generate(question):
|
16 |
-
#
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
#
|
20 |
-
|
|
|
21 |
|
22 |
return response['answer']
|
23 |
|
24 |
# Create a Gradio interface
|
25 |
interface = gr.Interface(
|
26 |
fn=retrieve_and_generate,
|
27 |
-
inputs=gr.Textbox(lines=2, placeholder="Ask a question about the documents..."),
|
28 |
outputs="text",
|
29 |
title="RAG Chatbot",
|
30 |
description="Ask questions about the documents in the CSV file."
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
+
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['embedding'] = data['embedding'].apply(lambda x: np.fromstring(x[1:-1], sep=', '))
|
13 |
+
|
14 |
+
# Initialize FAISS index
|
15 |
+
dimension = len(data['embedding'][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['embedding'].values))
|
20 |
+
|
21 |
+
# Check if GPU is available
|
22 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
23 |
+
|
24 |
+
# Load QA model
|
25 |
+
qa_model = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad", device=0 if torch.cuda.is_available() else -1)
|
26 |
|
27 |
+
# Load BERT model and tokenizer
|
28 |
+
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():
|
36 |
+
outputs = model(**inputs)
|
37 |
+
return outputs.last_hidden_state.mean(dim=1).cpu().numpy()
|
38 |
|
39 |
# Function to retrieve the relevant document and generate a response
|
40 |
@spaces.GPU(duration=120)
|
41 |
def retrieve_and_generate(question):
|
42 |
+
# Embed the question
|
43 |
+
question_embedding = embed_question(question, model, tokenizer)
|
44 |
+
|
45 |
+
# Search in FAISS index
|
46 |
+
_, indices = gpu_index.search(question_embedding, k=1)
|
47 |
+
|
48 |
+
# Retrieve the most relevant document
|
49 |
+
relevant_doc = data.iloc[indices[0][0]]
|
50 |
|
51 |
+
# Use the QA model to generate the answer
|
52 |
+
context = relevant_doc['Abstract']
|
53 |
+
response = qa_model(question=question, context=context)
|
54 |
|
55 |
return response['answer']
|
56 |
|
57 |
# Create a Gradio interface
|
58 |
interface = gr.Interface(
|
59 |
fn=retrieve_and_generate,
|
60 |
+
inputs=gr.inputs.Textbox(lines=2, placeholder="Ask a question about the documents..."),
|
61 |
outputs="text",
|
62 |
title="RAG Chatbot",
|
63 |
description="Ask questions about the documents in the CSV file."
|