Spaces:
Build error
Build error
nileshhanotia
commited on
Commit
•
d7a26ff
1
Parent(s):
ac02b78
Update models/rag_system.py
Browse files- models/rag_system.py +54 -32
models/rag_system.py
CHANGED
@@ -1,9 +1,9 @@
|
|
1 |
import os
|
2 |
-
import pandas as pd
|
3 |
-
from transformers import pipeline, AutoTokenizer, AutoModel
|
4 |
-
import torch
|
5 |
import numpy as np
|
|
|
|
|
6 |
from sentence_transformers import SentenceTransformer
|
|
|
7 |
from utils.logger import setup_logger
|
8 |
from utils.model_loader import ModelLoader
|
9 |
|
@@ -12,13 +12,18 @@ logger = setup_logger(__name__)
|
|
12 |
class RAGSystem:
|
13 |
def __init__(self, csv_path="apparel.csv"):
|
14 |
try:
|
15 |
-
|
16 |
-
self.
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
21 |
)
|
|
|
|
|
|
|
22 |
except Exception as e:
|
23 |
logger.error(f"Failed to initialize RAGSystem: {str(e)}")
|
24 |
raise
|
@@ -28,41 +33,58 @@ class RAGSystem:
|
|
28 |
raise FileNotFoundError(f"CSV file not found at {csv_path}")
|
29 |
|
30 |
try:
|
|
|
31 |
self.documents = pd.read_csv(csv_path)
|
|
|
|
|
32 |
# Create embeddings for all documents
|
33 |
-
self.
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
except Exception as e:
|
38 |
logger.error(f"Failed to setup RAG system: {str(e)}")
|
39 |
raise
|
40 |
|
41 |
def get_relevant_documents(self, query, top_k=5):
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
query_embedding.
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
54 |
|
55 |
def process_query(self, query):
|
56 |
try:
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
|
|
60 |
qa_input = {
|
61 |
"question": query,
|
62 |
-
"context":
|
63 |
}
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
66 |
except Exception as e:
|
67 |
-
logger.error(f"
|
68 |
-
return "Failed to process query
|
|
|
1 |
import os
|
|
|
|
|
|
|
2 |
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
+
from transformers import pipeline
|
5 |
from sentence_transformers import SentenceTransformer
|
6 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
7 |
from utils.logger import setup_logger
|
8 |
from utils.model_loader import ModelLoader
|
9 |
|
|
|
12 |
class RAGSystem:
|
13 |
def __init__(self, csv_path="apparel.csv"):
|
14 |
try:
|
15 |
+
# Initialize the sentence transformer model
|
16 |
+
self.embedder = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
17 |
+
|
18 |
+
# Initialize the QA pipeline
|
19 |
+
self.qa_pipeline = pipeline(
|
20 |
+
"question-answering",
|
21 |
+
model="distilbert-base-cased-distilled-squad",
|
22 |
+
tokenizer="distilbert-base-cased-distilled-squad"
|
23 |
)
|
24 |
+
|
25 |
+
self.setup_system(csv_path)
|
26 |
+
|
27 |
except Exception as e:
|
28 |
logger.error(f"Failed to initialize RAGSystem: {str(e)}")
|
29 |
raise
|
|
|
33 |
raise FileNotFoundError(f"CSV file not found at {csv_path}")
|
34 |
|
35 |
try:
|
36 |
+
# Load and preprocess documents
|
37 |
self.documents = pd.read_csv(csv_path)
|
38 |
+
self.texts = self.documents['Title'].astype(str).tolist()
|
39 |
+
|
40 |
# Create embeddings for all documents
|
41 |
+
self.embeddings = self.embedder.encode(self.texts)
|
42 |
+
|
43 |
+
logger.info(f"Successfully loaded {len(self.texts)} documents")
|
44 |
+
|
45 |
except Exception as e:
|
46 |
logger.error(f"Failed to setup RAG system: {str(e)}")
|
47 |
raise
|
48 |
|
49 |
def get_relevant_documents(self, query, top_k=5):
|
50 |
+
try:
|
51 |
+
# Get query embedding
|
52 |
+
query_embedding = self.embedder.encode([query])
|
53 |
+
|
54 |
+
# Calculate similarities
|
55 |
+
similarities = cosine_similarity(query_embedding, self.embeddings)[0]
|
56 |
+
|
57 |
+
# Get top k most similar documents
|
58 |
+
top_indices = np.argsort(similarities)[-top_k:][::-1]
|
59 |
+
|
60 |
+
return [self.texts[i] for i in top_indices]
|
61 |
+
|
62 |
+
except Exception as e:
|
63 |
+
logger.error(f"Error retrieving relevant documents: {str(e)}")
|
64 |
+
return []
|
65 |
|
66 |
def process_query(self, query):
|
67 |
try:
|
68 |
+
# Get relevant documents
|
69 |
+
relevant_docs = self.get_relevant_documents(query)
|
70 |
+
|
71 |
+
if not relevant_docs:
|
72 |
+
return "No relevant documents found."
|
73 |
+
|
74 |
+
# Combine retrieved documents into context
|
75 |
+
context = " ".join(relevant_docs)
|
76 |
|
77 |
+
# Prepare QA input
|
78 |
qa_input = {
|
79 |
"question": query,
|
80 |
+
"context": context[:512] # Limit context length for the model
|
81 |
}
|
82 |
+
|
83 |
+
# Get answer using QA pipeline
|
84 |
+
answer = self.qa_pipeline(qa_input)
|
85 |
+
|
86 |
+
return answer['answer']
|
87 |
+
|
88 |
except Exception as e:
|
89 |
+
logger.error(f"Error processing query: {str(e)}")
|
90 |
+
return f"Failed to process query: {str(e)}"
|