Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,91 +1,148 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import os
|
| 3 |
from PyPDF2 import PdfReader
|
| 4 |
-
import
|
| 5 |
-
from
|
| 6 |
-
import
|
| 7 |
-
from
|
| 8 |
-
import
|
| 9 |
-
from transformers import AutoTokenizer,
|
| 10 |
-
import
|
| 11 |
|
| 12 |
-
# ------
|
| 13 |
-
|
| 14 |
-
# -----------------------------
|
| 15 |
-
st.title("π Free Document QA App (Hugging Face)")
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
context += " ".join([str(cell) for cell in row if cell]) + "\n"
|
| 45 |
-
else:
|
| 46 |
-
st.warning("Unsupported file type.")
|
| 47 |
-
context = ""
|
| 48 |
|
| 49 |
-
|
| 50 |
-
# Split into chunks
|
| 51 |
-
# -----------------------------
|
| 52 |
-
CHUNK_SIZE = 500 # characters
|
| 53 |
-
chunks = [context[i:i+CHUNK_SIZE] for i in range(0, len(context), CHUNK_SIZE)]
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
embed_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 59 |
-
embeddings = embed_model.encode(chunks)
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
st.info("Loading model... (this may take a minute)")
|
| 69 |
-
hf_model_name = "TheBloke/guanaco-7B-GPTQ" # works on CPU or GPU
|
| 70 |
-
tokenizer = AutoTokenizer.from_pretrained(hf_model_name)
|
| 71 |
-
model = AutoModelForCausalLM.from_pretrained(hf_model_name, device_map="auto")
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import streamlit as st
|
|
|
|
| 3 |
from PyPDF2 import PdfReader
|
| 4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 5 |
+
from langchain.vectorstores import FAISS
|
| 6 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 7 |
+
from langchain.chains.question_answering import load_qa_chain
|
| 8 |
+
from langchain.llms import HuggingFacePipeline
|
| 9 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 10 |
+
import tempfile
|
| 11 |
|
| 12 |
+
# --- Streamlit page config ---
|
| 13 |
+
st.set_page_config(page_title="π Multi PDF Chatbot", layout="wide", page_icon="π€")
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# --- Header ---
|
| 16 |
+
st.markdown(
|
| 17 |
+
"<h1 style='text-align: center; color: #2F4F4F;'>π Multi-PDF Chat Agent π€</h1>",
|
| 18 |
+
unsafe_allow_html=True
|
| 19 |
+
)
|
| 20 |
|
| 21 |
+
# --- Sidebar Styling ---
|
| 22 |
+
st.markdown(
|
| 23 |
+
"""
|
| 24 |
+
<style>
|
| 25 |
+
section[data-testid="stSidebar"] {
|
| 26 |
+
background-color: #111827;
|
| 27 |
+
padding: 30px 20px;
|
| 28 |
+
}
|
| 29 |
+
.profile-img-container {
|
| 30 |
+
display: flex;
|
| 31 |
+
justify-content: center;
|
| 32 |
+
margin-bottom: 15px;
|
| 33 |
+
}
|
| 34 |
+
.profile-img-container img {
|
| 35 |
+
border-radius: 12px;
|
| 36 |
+
height: 100px;
|
| 37 |
+
width: 100px;
|
| 38 |
+
object-fit: cover;
|
| 39 |
+
border: 2px solid #4ade80;
|
| 40 |
+
box-shadow: 0 0 10px rgba(0,0,0,0.5);
|
| 41 |
+
}
|
| 42 |
+
.upload-section {
|
| 43 |
+
background-color: #1f2937;
|
| 44 |
+
padding: 20px;
|
| 45 |
+
border-radius: 12px;
|
| 46 |
+
margin-top: 20px;
|
| 47 |
+
margin-bottom: 30px;
|
| 48 |
+
}
|
| 49 |
+
.upload-section h4 {
|
| 50 |
+
color: #facc15;
|
| 51 |
+
margin-bottom: 15px;
|
| 52 |
+
}
|
| 53 |
+
.footer {
|
| 54 |
+
text-align: center;
|
| 55 |
+
font-size: 13px;
|
| 56 |
+
color: #9ca3af;
|
| 57 |
+
}
|
| 58 |
+
.footer a {
|
| 59 |
+
color: #facc15;
|
| 60 |
+
text-decoration: none;
|
| 61 |
+
}
|
| 62 |
+
.footer a:hover {
|
| 63 |
+
color: #ffffff;
|
| 64 |
+
}
|
| 65 |
+
</style>
|
| 66 |
+
""",
|
| 67 |
+
unsafe_allow_html=True
|
| 68 |
+
)
|
| 69 |
|
| 70 |
+
# --- Sidebar Layout ---
|
| 71 |
+
with st.sidebar:
|
| 72 |
+
# Profile image
|
| 73 |
+
col1, col2, col3 = st.columns([1, 2, 1])
|
| 74 |
+
with col2:
|
| 75 |
+
st.image("assets/img/main.png", width=100)
|
| 76 |
+
|
| 77 |
+
# Upload PDFs
|
| 78 |
+
st.markdown('<div class="upload-section">', unsafe_allow_html=True)
|
| 79 |
+
st.markdown("#### π Upload PDF Files")
|
| 80 |
+
pdf_docs = st.file_uploader("Drag and drop your PDFs here", accept_multiple_files=True, label_visibility="collapsed")
|
| 81 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 82 |
+
|
| 83 |
+
# Footer
|
| 84 |
+
st.markdown(
|
| 85 |
+
"""
|
| 86 |
+
<div class="footer">
|
| 87 |
+
Built with β€οΈ by <a href="https://github.com/Danish7861" target="_blank">Danish Shahzad</a>
|
| 88 |
+
</div>
|
| 89 |
+
""",
|
| 90 |
+
unsafe_allow_html=True
|
| 91 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
# --- Main Logic ---
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
# Store FAISS vector store in session state
|
| 96 |
+
if "vectorstore" not in st.session_state:
|
| 97 |
+
st.session_state.vectorstore = None
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
if pdf_docs:
|
| 100 |
+
if st.button("π€ Submit & Process"):
|
| 101 |
+
with st.spinner("Processing PDFs..."):
|
| 102 |
+
all_text = ""
|
| 103 |
+
for pdf_file in pdf_docs:
|
| 104 |
+
pdf_reader = PdfReader(pdf_file)
|
| 105 |
+
for page in pdf_reader.pages:
|
| 106 |
+
all_text += page.extract_text() + "\n"
|
| 107 |
|
| 108 |
+
# Split text into chunks
|
| 109 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
| 110 |
+
chunks = splitter.split_text(all_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
# Create embeddings
|
| 113 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 114 |
+
st.session_state.vectorstore = FAISS.from_texts(chunks, embeddings)
|
| 115 |
+
st.success("β
PDF content indexed successfully!")
|
| 116 |
|
| 117 |
+
# --- Load local LLM ---
|
| 118 |
+
@st.cache_resource(show_spinner=False)
|
| 119 |
+
def load_local_llm():
|
| 120 |
+
tokenizer = AutoTokenizer.from_pretrained("TheBloke/guanaco-7B-GGML")
|
| 121 |
+
model = AutoModelForCausalLM.from_pretrained("TheBloke/guanaco-7B-GGML")
|
| 122 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_length=512)
|
| 123 |
+
return HuggingFacePipeline(pipeline=pipe)
|
| 124 |
+
|
| 125 |
+
llm = load_local_llm()
|
| 126 |
+
|
| 127 |
+
# --- Question input ---
|
| 128 |
+
user_question = st.text_input("π Ask something from your uploaded PDFs:")
|
| 129 |
+
if user_question:
|
| 130 |
+
if st.session_state.vectorstore is None:
|
| 131 |
+
st.warning("β οΈ Please upload and process PDFs first.")
|
| 132 |
+
else:
|
| 133 |
+
with st.spinner("Thinking... π"):
|
| 134 |
+
relevant_docs = st.session_state.vectorstore.similarity_search(user_question)
|
| 135 |
+
chain = load_qa_chain(llm, chain_type="stuff")
|
| 136 |
+
answer = chain.run(input_documents=relevant_docs, question=user_question)
|
| 137 |
+
st.success("Answer:")
|
| 138 |
+
st.write(answer)
|
| 139 |
|
| 140 |
+
# --- Fixed footer ---
|
| 141 |
+
st.markdown(
|
| 142 |
+
"""
|
| 143 |
+
<div style="position: fixed; bottom: 0; width: 100%; background-color: #2F4F4F; padding: 10px; color: white; text-align: center; font-size: 13px;">
|
| 144 |
+
π Multi-PDF Chatbot | Powered by LangChain, FAISS & Local AI
|
| 145 |
+
</div>
|
| 146 |
+
""",
|
| 147 |
+
unsafe_allow_html=True
|
| 148 |
+
)
|