testchatbot / app.py
Yoxas's picture
Update app.py
716f829 verified
raw
history blame
1.56 kB
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()