Spaces:
Runtime error
Runtime error
File size: 1,560 Bytes
e7abd03 716f829 e7abd03 1f3ecf4 e7abd03 716f829 e7abd03 716f829 e7abd03 716f829 e7abd03 716f829 e7abd03 716f829 b71f887 716f829 e7abd03 716f829 6f729e6 e7abd03 716f829 6f729e6 716f829 |
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 |
import pandas as pd
import torch
from sentence_transformers import SentenceTransformer, util
import gradio as gr
import json
import spaces
# Load the CSV file with embeddings
df = pd.read_csv('RBDx10kstats.csv')
df['embedding'] = df['embedding'].apply(json.loads) # Convert JSON string back to list
# Convert embeddings to tensor for efficient retrieval
embeddings = torch.tensor(df['embedding'].tolist())
# Load the same Sentence Transformer model
model = SentenceTransformer('all-MiniLM-L6-v2')
# Define the function to find the most relevant document
@spaces.GPU(duration=120)
def retrieve_relevant_doc(query):
query_embedding = model.encode(query, convert_to_tensor=True)
similarities = util.pytorch_cos_sim(query_embedding, embeddings)[0]
best_match_idx = torch.argmax(similarities).item()
return df.iloc[best_match_idx]['Abstract']
# Define the function to generate a response (for simplicity, echo the retrieved doc)
@spaces.GPU(duration=120)
def generate_response(query):
relevant_doc = retrieve_relevant_doc(query)
# Here you could use a more sophisticated language model to generate a response
# For now, we will just return the relevant document as the response
return relevant_doc
# Create a Gradio interface
iface = gr.Interface(
fn=generate_response,
inputs=gr.inputs.Textbox(lines=2, placeholder="Enter your query here..."),
outputs="text",
title="RAG Chatbot",
description="This chatbot retrieves relevant documents based on your query."
)
# Launch the Gradio interface
iface.launch() |