import os import shutil from langchain_community.document_loaders import PyPDFLoader from langchain_community.embeddings import HuggingFaceEmbeddings from langchain_community.vectorstores import Chroma from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.chains import RetrievalQA from langchain_community.llms import HuggingFaceEndpoint # ---------------- Global QA object ---------------- qa = None # ---------------- Initialize LLM ---------------- def initialize_llm(): """ Initialize HuggingFaceEndpoint LLM for Mistral-7B ⚠️ Replace 'hf_your_actual_token_here' with your actual Hugging Face token """ hf_token = "token2" # <-- Your Hugging Face token llm = HuggingFaceEndpoint( endpoint_url="https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2", huggingfacehub_api_token=hf_token, task="text2text-generation", model_kwargs={"temperature": 0.1, "max_length": 512} ) print("✅ Mistral-7B LLM initialized") return llm # Initialize global LLM llm = initialize_llm() # ---------------- PDF Processing ---------------- def process_pdf(pdf_file_path): """Load PDF, create embeddings, and initialize QA system""" global qa if llm is None: return "❌ Error: Failed to initialize LLM. Check your HF token." try: # Load PDF loader = PyPDFLoader(pdf_file_path) documents = loader.load() if not documents: return "❌ Error: No content found in PDF" # Split text into chunks text_splitter = RecursiveCharacterTextSplitter( chunk_size=1000, chunk_overlap=200, length_function=len ) docs = text_splitter.split_documents(documents) docs = [doc for doc in docs if doc.page_content.strip()] if not docs: return "❌ Error: PDF has no extractable text chunks" # Create embeddings embeddings = HuggingFaceEmbeddings( model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={"device": "cpu"} ) # Clear previous Chroma cache if os.path.exists("./chroma_cache"): shutil.rmtree("./chroma_cache") # Create vectorstore vectorstore = Chroma.from_documents( docs, embeddings, persist_directory="./chroma_cache" ) # Initialize QA system qa = RetrievalQA.from_chain_type( llm=llm, # ✅ Pass HuggingFaceEndpoint instance here chain_type="stuff", retriever=vectorstore.as_retriever(search_kwargs={"k": 3}), return_source_documents=False ) return f"✅ PDF processed successfully! {len(docs)} text chunks ready." except Exception as e: return f"❌ Error processing PDF: {str(e)}" # ---------------- Query PDF ---------------- def query_pdf(question): """Query the processed PDF""" global qa if qa is None: return "❌ No PDF processed yet. Please upload and process a PDF first." if not question.strip(): return "⚠️ Please ask a valid question." try: result = qa.invoke({"query": question}) return result.get("result", "No answer found") except Exception as e: return f"❌ Error answering question: {str(e)}"