Spaces:
Sleeping
Sleeping
import streamlit as st | |
import os | |
api_token = os.environ.get("Key2") | |
from langchain_community.vectorstores import FAISS | |
from langchain_community.document_loaders import PyPDFLoader | |
from langchain.text_splitter import RecursiveCharacterTextSplitter | |
from langchain_community.embeddings import HuggingFaceEmbeddings | |
from langchain_community.llms import HuggingFaceEndpoint | |
from langchain.chains import ConversationalRetrievalChain | |
from langchain.memory import ConversationBufferMemory | |
list_llm = ["meta-llama/Llama-3.2-3B-Instruct", "mistralai/Mistral-7B-Instruct-v0.2"] | |
# Load and split PDF document | |
def load_doc(list_file_path): | |
loaders = [PyPDFLoader(x) for x in list_file_path] | |
pages = [] | |
for loader in loaders: | |
pages.extend(loader.load()) | |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1024, chunk_overlap=64) | |
return text_splitter.split_documents(pages) | |
# Create vector database | |
def create_db(splits): | |
embeddings = HuggingFaceEmbeddings() | |
return FAISS.from_documents(splits, embeddings) | |
# Initialize langchain LLM chain | |
def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db): | |
llm = HuggingFaceEndpoint( | |
repo_id=llm_model, | |
huggingfacehub_api_token=api_token, | |
temperature=temperature, | |
max_new_tokens=max_tokens, | |
top_k=top_k, | |
) | |
memory = ConversationBufferMemory(memory_key="chat_history", output_key='answer', return_messages=True) | |
retriever = vector_db.as_retriever() | |
return ConversationalRetrievalChain.from_llm( | |
llm, | |
retriever=retriever, | |
chain_type="stuff", | |
memory=memory, | |
return_source_documents=True, | |
verbose=False, | |
) | |
st.title("RAG PDF Chatbot") | |
uploaded_files = st.file_uploader("Upload PDF files", accept_multiple_files=True, type="pdf") | |
if uploaded_files: | |
# Save uploaded files to local disk | |
file_paths = [] | |
for uploaded_file in uploaded_files: | |
file_path = os.path.join("temp", uploaded_file.name) | |
os.makedirs("temp", exist_ok=True) | |
with open(file_path, "wb") as f: | |
f.write(uploaded_file.getbuffer()) | |
file_paths.append(file_path) | |
st.session_state["doc_splits"] = load_doc(file_paths) | |
st.success("Documents successfully loaded and split!") | |
if 'vector_db' not in st.session_state and 'doc_splits' in st.session_state: | |
st.session_state['vector_db'] = create_db(st.session_state['doc_splits']) | |
llm_option = st.selectbox("Select LLM", list_llm) | |
temperature = st.slider("Temperature", 0.01, 1.0, 0.5, 0.1) | |
max_tokens = st.slider("Max Tokens", 128, 9192, 4096, 128) | |
top_k = st.slider("Top K", 1, 10, 3, 1) | |
if 'qa_chain' not in st.session_state and 'vector_db' in st.session_state: | |
st.session_state['qa_chain'] = initialize_llmchain(llm_option, temperature, max_tokens, top_k, st.session_state['vector_db']) | |
if "chat_history" not in st.session_state: | |
st.session_state["chat_history"] = [] | |
user_input = st.text_input("Ask a question") | |
if st.button("Submit") and user_input: | |
qa_chain = st.session_state['qa_chain'] | |
response = qa_chain.invoke({"question": user_input, "chat_history": st.session_state["chat_history"]}) | |
st.session_state["chat_history"].append((user_input, response["answer"])) | |
st.write("### Response:") | |
st.write(response["answer"]) | |
st.write("### Sources:") | |
for doc in response["source_documents"][:3]: | |
st.write(f"Page {doc.metadata['page'] + 1}: {doc.page_content[:300]}...") | |
st.write("### Chat History") | |
for user_msg, bot_msg in st.session_state["chat_history"]: | |
st.text(f"User: {user_msg}") | |
st.text(f"Assistant: {bot_msg}") | |