️ DEPRECATED GITLAB INSTANCE ️ This GitLab is now read-only for reference. Please use https://gitlab.iauro.co for all new work.

Migration completed on September 17, 2025

Commit 5da8e051 authored by MotiramShinde's avatar MotiramShinde

rag basic assignment

parents e025474f 0f17df78
OPENAI_API_KEY=your-api-key-here
import streamlit as st
from utils import load_pdf_chunks, build_faiss_index, get_answer
import os
from dotenv import load_dotenv
load_dotenv()
st.set_page_config(page_title="PDF Q&A Bot", layout="centered")
st.title("📄 Ask Questions to Your PDF")
uploaded_file = st.file_uploader("Upload a PDF", type="pdf")
if uploaded_file:
with open("temp.pdf", "wb") as f:
f.write(uploaded_file.read())
st.success("PDF uploaded successfully!")
with st.spinner("Reading and indexing the document..."):
chunks = load_pdf_chunks("temp.pdf")
faiss_index = build_faiss_index(chunks)
st.success("Document is ready for questions!")
query = st.text_input("Ask a question:")
if query:
with st.spinner("Thinking..."):
answer = get_answer(faiss_index, query)
st.markdown(f"**Answer:** {answer}")
streamlit
langchain
openai
faiss-cpu
python-dotenv
PyPDF2
import os
from langchain_community.document_loaders import PyPDFLoader
#from langchain.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import FAISS
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI
def load_pdf_chunks(pdf_path):
loader = PyPDFLoader(pdf_path)
docs = loader.load()
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
return splitter.split_documents(docs)
def build_faiss_index(chunks):
embeddings = OpenAIEmbeddings()
return FAISS.from_documents(chunks, embeddings)
def get_answer(faiss_index, query):
docs = faiss_index.similarity_search(query, k=3)
llm = OpenAI(temperature=0)
chain = load_qa_chain(llm, chain_type="stuff")
return chain.run(input_documents=docs, question=query)
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment