Spaces:
Runtime error
Runtime error
File size: 2,284 Bytes
8b582d7 921072e 6c4a2c9 8e2a2a1 921072e 6c4a2c9 86d4425 6c4a2c9 86d4425 6c4a2c9 86d4425 6c4a2c9 921072e 6c4a2c9 921072e 8e2a2a1 921072e 6c4a2c9 921072e 6c4a2c9 921072e 6c4a2c9 921072e 5cb1a88 921072e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import gradio as gr
import pandas as pd
import numpy as np
from transformers import pipeline, BertTokenizer, BertModel
import faiss
import torch
import spaces
# Load CSV data
data = pd.read_csv('RB10kstats.csv')
# Convert embedding column from string to numpy array
data['embeddings'] = data['embeddings'].apply(lambda x: np.fromstring(x[1:-1], sep=', '))
# Initialize FAISS index
dimension = len(data['embeddings'][0])
res = faiss.StandardGpuResources() # use a single GPU
index = faiss.IndexFlatL2(dimension)
gpu_index = faiss.index_cpu_to_gpu(res, 0, index) # move to GPU
gpu_index.add(np.stack(data['embeddings'].values))
# Check if GPU is available
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Load QA model
qa_model = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad", device=0 if torch.cuda.is_available() else -1)
# Load BERT model and tokenizer
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertModel.from_pretrained('bert-base-uncased').to(device)
# Function to embed the question using BERT
@spaces.GPU(duration=120)
def embed_question(question, model, tokenizer):
inputs = tokenizer(question, return_tensors='pt').to(device)
with torch.no_grad():
outputs = model(**inputs)
return outputs.last_hidden_state.mean(dim=1).cpu().numpy()
# Function to retrieve the relevant document and generate a response
@spaces.GPU(duration=120)
def retrieve_and_generate(question):
# Embed the question
question_embedding = embed_question(question, model, tokenizer)
# Search in FAISS index
_, indices = gpu_index.search(question_embedding, k=1)
# Retrieve the most relevant document
relevant_doc = data.iloc[indices[0][0]]
# Use the QA model to generate the answer
context = relevant_doc['Abstract']
response = qa_model(question=question, context=context)
return response['answer']
# Create a Gradio interface
interface = gr.Interface(
fn=retrieve_and_generate,
inputs=gr.inputs.Textbox(lines=2, placeholder="Ask a question about the documents..."),
outputs="text",
title="RAG Chatbot",
description="Ask questions about the documents in the CSV file."
)
# Launch the Gradio app
interface.launch()
|