Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,33 +3,40 @@ import torch
|
|
3 |
from sentence_transformers import SentenceTransformer, util
|
4 |
import gradio as gr
|
5 |
import json
|
6 |
-
import
|
|
|
|
|
|
|
7 |
|
8 |
# Load the CSV file with embeddings
|
9 |
df = pd.read_csv('RBDx10kstats.csv')
|
10 |
df['embedding'] = df['embedding'].apply(json.loads) # Convert JSON string back to list
|
11 |
|
12 |
# Convert embeddings to tensor for efficient retrieval
|
13 |
-
embeddings = torch.tensor(df['embedding'].tolist())
|
|
|
|
|
|
|
14 |
|
15 |
-
# Load the
|
16 |
-
|
|
|
17 |
|
18 |
# Define the function to find the most relevant document
|
19 |
-
@spaces.GPU(duration=120)
|
20 |
def retrieve_relevant_doc(query):
|
21 |
-
query_embedding = model.encode(query, convert_to_tensor=True)
|
22 |
similarities = util.pytorch_cos_sim(query_embedding, embeddings)[0]
|
23 |
best_match_idx = torch.argmax(similarities).item()
|
24 |
return df.iloc[best_match_idx]['Abstract']
|
25 |
|
26 |
-
# Define the function to generate a response
|
27 |
-
@spaces.GPU(duration=120)
|
28 |
def generate_response(query):
|
29 |
relevant_doc = retrieve_relevant_doc(query)
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
33 |
|
34 |
# Create a Gradio interface
|
35 |
iface = gr.Interface(
|
@@ -37,7 +44,7 @@ iface = gr.Interface(
|
|
37 |
inputs=gr.Textbox(lines=2, placeholder="Enter your query here..."),
|
38 |
outputs="text",
|
39 |
title="RAG Chatbot",
|
40 |
-
description="This chatbot retrieves relevant documents based on your query."
|
41 |
)
|
42 |
|
43 |
# Launch the Gradio interface
|
|
|
3 |
from sentence_transformers import SentenceTransformer, util
|
4 |
import gradio as gr
|
5 |
import json
|
6 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
7 |
+
|
8 |
+
# Ensure you have GPU support
|
9 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
10 |
|
11 |
# Load the CSV file with embeddings
|
12 |
df = pd.read_csv('RBDx10kstats.csv')
|
13 |
df['embedding'] = df['embedding'].apply(json.loads) # Convert JSON string back to list
|
14 |
|
15 |
# Convert embeddings to tensor for efficient retrieval
|
16 |
+
embeddings = torch.tensor(df['embedding'].tolist(), device=device)
|
17 |
+
|
18 |
+
# Load the Sentence Transformer model
|
19 |
+
model = SentenceTransformer('all-MiniLM-L6-v2', device=device)
|
20 |
|
21 |
+
# Load the LLaMA model for response generation
|
22 |
+
llama_tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct")
|
23 |
+
llama_model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct").to(device)
|
24 |
|
25 |
# Define the function to find the most relevant document
|
|
|
26 |
def retrieve_relevant_doc(query):
|
27 |
+
query_embedding = model.encode(query, convert_to_tensor=True, device=device)
|
28 |
similarities = util.pytorch_cos_sim(query_embedding, embeddings)[0]
|
29 |
best_match_idx = torch.argmax(similarities).item()
|
30 |
return df.iloc[best_match_idx]['Abstract']
|
31 |
|
32 |
+
# Define the function to generate a response
|
|
|
33 |
def generate_response(query):
|
34 |
relevant_doc = retrieve_relevant_doc(query)
|
35 |
+
input_text = f"Document: {relevant_doc}\n\nQuestion: {query}\n\nAnswer:"
|
36 |
+
inputs = llama_tokenizer(input_text, return_tensors="pt").to(device)
|
37 |
+
outputs = llama_model.generate(inputs["input_ids"], max_length=150)
|
38 |
+
response = llama_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
39 |
+
return response
|
40 |
|
41 |
# Create a Gradio interface
|
42 |
iface = gr.Interface(
|
|
|
44 |
inputs=gr.Textbox(lines=2, placeholder="Enter your query here..."),
|
45 |
outputs="text",
|
46 |
title="RAG Chatbot",
|
47 |
+
description="This chatbot retrieves relevant documents based on your query and generates responses using LLaMA."
|
48 |
)
|
49 |
|
50 |
# Launch the Gradio interface
|