Yoxas commited on
Commit
d0ab9b7
·
verified ·
1 Parent(s): e3be5c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -12
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 spaces
 
 
 
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 same Sentence Transformer model
16
- model = SentenceTransformer('all-MiniLM-L6-v2')
 
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 (for simplicity, echo the retrieved doc)
27
- @spaces.GPU(duration=120)
28
  def generate_response(query):
29
  relevant_doc = retrieve_relevant_doc(query)
30
- # Here you could use a more sophisticated language model to generate a response
31
- # For now, we will just return the relevant document as the response
32
- return relevant_doc
 
 
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