eva-assistant-assets / src /reload_session.py
irthayag's picture
Upload folder using huggingface_hub
57598d0 verified
Raw
History Blame Contribute Delete
2.6 kB
"""Run this at the start of every session to instantly restore everything."""
from google.colab import drive
drive.mount('/content/drive')
import pickle, numpy as np, faiss, torch, sys
from sentence_transformers import SentenceTransformer, CrossEncoder
from google.colab import userdata
from groq import Groq
from transformers import DistilBertTokenizer, DistilBertForSequenceClassification, AutoTokenizer, AutoModelForQuestionAnswering
PROJECT_ROOT = "/content/drive/MyDrive/Enterprise_Knowledge_Assistant"
device = 'cuda' if torch.cuda.is_available() else 'cpu'
print(f"Using device: {device}")
# Global FAISS index + chunks
index = faiss.read_index(f"{PROJECT_ROOT}/embeddings/faiss_index.bin")
with open(f"{PROJECT_ROOT}/embeddings/all_chunks_metadata.pkl", 'rb') as f:
all_chunks = pickle.load(f)
# Domain-specific FAISS indices
domain_indices = {}
domain_faiss_folder = f"{PROJECT_ROOT}/embeddings/domain_indices"
for domain in ['HR', 'Legal', 'Finance', 'IT']:
domain_indices[domain] = faiss.read_index(f"{domain_faiss_folder}/{domain}_index.bin")
with open(f"{domain_faiss_folder}/domain_chunks_map.pkl", 'rb') as f:
domain_chunks_map = pickle.load(f)
# Embedding model
embedding_model = SentenceTransformer(f"{PROJECT_ROOT}/models/embedding_model", device=device)
# Groq client
groq_client = Groq(api_key=userdata.get("GROQ_API_KEY"))
# Router (DistilBERT)
router_path = f"{PROJECT_ROOT}/models/distilbert_router"
tokenizer = DistilBertTokenizer.from_pretrained(router_path)
router_model = DistilBertForSequenceClassification.from_pretrained(router_path)
router_model.eval()
with open(f"{router_path}/label_encoder.pkl", 'rb') as f:
label_encoder = pickle.load(f)
# Extractive QA model
qa_tokenizer = AutoTokenizer.from_pretrained("distilbert-base-cased-distilled-squad")
qa_model = AutoModelForQuestionAnswering.from_pretrained("distilbert-base-cased-distilled-squad")
qa_model.eval()
# Reranker
reranker = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
# Custom pipeline functions
sys.path.append(f"{PROJECT_ROOT}/src")
from rag_pipeline import retrieve_relevant_chunks, generate_with_groq, ask_chatbot_v4
from router_utils import classify_query
from hybrid_retrieval import retrieve_from_domain_index, retrieve_hybrid
from reranker_utils import retrieve_with_reranking
from extractive_qa import extract_exact_answer, ask_with_extraction
from eva_chatbot import ask_eva
conversation_history = []
print("Everything reloaded successfully. Ready to chat.")
print(f"Total chunks in index: {index.ntotal}")
print(f"Domains: {list(domain_indices.keys())}")