import gradio as gr # LangChain imports from langchain_community.document_loaders import PyPDFLoader from langchain_text_splitters import RecursiveCharacterTextSplitter from langchain_community.embeddings import HuggingFaceEmbeddings from langchain_community.vectorstores import FAISS # LLM from transformers import pipeline from langchain_community.llms import HuggingFacePipeline # ----------------------------- # Step 1: Load docs loader1 = PyPDFLoader("AST-1.pdf") loader2 = PyPDFLoader("AST-2.pdf") docs = loader1.load() + loader2.load() # Step 2: Split splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100) chunks = splitter.split_documents(docs) # Step 3: Embeddings + DB embedding = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2") db = FAISS.from_documents(chunks, embedding) # Step 4: Retriever (MMR) retriever = db.as_retriever(search_type="mmr", search_kwargs={"k": 4}) # Step 5: LLM pipe = pipeline("text-generation", model="google/flan-t5-base", max_new_tokens=200) llm = HuggingFacePipeline(pipeline=pipe) # RAG function def rag_query(query): docs = retriever.invoke(query) context = "\n\n".join([d.page_content for d in docs]) prompt = f""" Answer the question based only on the context below. Context: {context} Question: {query} """ return llm.invoke(prompt) # ----------------------------- # Gradio UI with gr.Blocks() as app: gr.Markdown("# 📚 RAG Q&A System") chatbot = gr.Chatbot(height=500) msg = gr.Textbox(placeholder="Ask your question...") def respond(message, history): response = rag_query(message) history.append({"role": "user", "content": message}) history.append({"role": "assistant", "content": response}) return "", history msg.submit(respond, [msg, chatbot], [msg, chatbot]) app.launch()