Update src/streamlit_app.py
Browse files- src/streamlit_app.py +109 -110
src/streamlit_app.py
CHANGED
|
@@ -3,7 +3,6 @@
|
|
| 3 |
@title: PDF AI Assistant
|
| 4 |
@author: Your Name
|
| 5 |
"""
|
| 6 |
-
# app.py
|
| 7 |
import streamlit as st
|
| 8 |
import fitz # PyMuPDF
|
| 9 |
import numpy as np
|
|
@@ -14,7 +13,7 @@ from tqdm.auto import tqdm
|
|
| 14 |
import textwrap
|
| 15 |
import re
|
| 16 |
import pandas as pd
|
| 17 |
-
from spacy.lang.en import English
|
| 18 |
|
| 19 |
# Configuration
|
| 20 |
MODEL_NAME = "all-mpnet-base-v2"
|
|
@@ -41,86 +40,65 @@ def split_list(input_list: list, slice_size: int = 10) -> list[list[str]]:
|
|
| 41 |
return [input_list[i:i + slice_size] for i in range(0, len(input_list), slice_size)]
|
| 42 |
|
| 43 |
def print_wrapped(text, wrap_length=80):
|
| 44 |
-
|
| 45 |
-
return wrapped_text
|
| 46 |
|
| 47 |
# PDF Processing
|
| 48 |
-
# def process_pdf(uploaded_file):
|
| 49 |
-
# doc = fitz.open(stream=uploaded_file.read(), filetype="pdf")
|
| 50 |
-
# pages_and_texts = []
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
# def process_pdf(uploaded_file):
|
| 54 |
-
# # Add this initialization
|
| 55 |
-
# from spacy.lang.en import English
|
| 56 |
-
|
| 57 |
-
# nlp = English() # Now this will work
|
| 58 |
-
# nlp.add_pipe("sentencizer")
|
| 59 |
-
|
| 60 |
-
# # Rest of your processing code...
|
| 61 |
-
|
| 62 |
-
|
| 63 |
def process_pdf(uploaded_file):
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
"
|
| 85 |
-
"
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
"token_count": len(text)/4
|
| 89 |
-
})
|
| 90 |
-
|
| 91 |
-
with st.spinner("Chunking text..."):
|
| 92 |
-
nlp = English()
|
| 93 |
-
nlp.add_pipe("sentencizer")
|
| 94 |
for item in tqdm(pages_and_texts):
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
}
|
| 105 |
-
chunk_dict["chunk_token_count"] = len(chunk_dict["sentence_chunk"])/4
|
| 106 |
-
pages_and_chunks.append(chunk_dict)
|
| 107 |
|
| 108 |
-
|
|
|
|
|
|
|
| 109 |
|
| 110 |
# Model Loading
|
| 111 |
@st.cache_resource
|
| 112 |
def load_models():
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
|
| 125 |
# Streamlit UI
|
| 126 |
st.title("PDF Knowledge Assistant 📚")
|
|
@@ -130,18 +108,18 @@ st.markdown("Upload a PDF document and ask questions about its content")
|
|
| 130 |
with st.sidebar:
|
| 131 |
st.header("Document Setup")
|
| 132 |
uploaded_file = st.file_uploader("Upload PDF", type=["pdf"])
|
| 133 |
-
if uploaded_file:
|
| 134 |
-
|
| 135 |
-
|
| 136 |
embedding_model, _, _ = load_models()
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
|
| 146 |
# Main Q&A Interface
|
| 147 |
if st.session_state.processed:
|
|
@@ -149,34 +127,55 @@ if st.session_state.processed:
|
|
| 149 |
|
| 150 |
if query:
|
| 151 |
embedding_model, tokenizer, llm_model = load_models()
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
# Display relevant passages
|
| 159 |
-
st.subheader("Most Relevant Passages:")
|
| 160 |
-
for score, idx in zip(top_results[0], top_results[1]):
|
| 161 |
-
with st.expander(f"Relevance: {score:.2f}"):
|
| 162 |
-
st.write(st.session_state.pages_and_chunks[idx]["sentence_chunk"])
|
| 163 |
-
st.caption(f"Page: {st.session_state.pages_and_chunks[idx]['page_number']+1}")
|
| 164 |
-
|
| 165 |
-
# Generate answer
|
| 166 |
-
with st.spinner("Generating answer..."):
|
| 167 |
-
context = [st.session_state.pages_and_chunks[i] for i in top_results[1]]
|
| 168 |
-
prompt = f"""
|
| 169 |
-
Answer this question: {query}
|
| 170 |
-
Using information from these passages:
|
| 171 |
-
{" ".join([c['sentence_chunk'] for c in context])}
|
| 172 |
-
Keep answers technical and specific to the document content.
|
| 173 |
-
"""
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
-
|
| 180 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
else:
|
| 182 |
st.info("Please upload a PDF document to get started")
|
|
|
|
| 3 |
@title: PDF AI Assistant
|
| 4 |
@author: Your Name
|
| 5 |
"""
|
|
|
|
| 6 |
import streamlit as st
|
| 7 |
import fitz # PyMuPDF
|
| 8 |
import numpy as np
|
|
|
|
| 13 |
import textwrap
|
| 14 |
import re
|
| 15 |
import pandas as pd
|
| 16 |
+
from spacy.lang.en import English
|
| 17 |
|
| 18 |
# Configuration
|
| 19 |
MODEL_NAME = "all-mpnet-base-v2"
|
|
|
|
| 40 |
return [input_list[i:i + slice_size] for i in range(0, len(input_list), slice_size)]
|
| 41 |
|
| 42 |
def print_wrapped(text, wrap_length=80):
|
| 43 |
+
return textwrap.fill(text, wrap_length)
|
|
|
|
| 44 |
|
| 45 |
# PDF Processing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
def process_pdf(uploaded_file):
|
| 47 |
+
try:
|
| 48 |
+
doc = fitz.open(stream=uploaded_file.read(), filetype="pdf")
|
| 49 |
+
pages_and_texts = []
|
| 50 |
+
|
| 51 |
+
with st.spinner("Processing PDF..."):
|
| 52 |
+
for page_number, page in tqdm(enumerate(doc)):
|
| 53 |
+
text = page.get_text()
|
| 54 |
+
text = text_formatter(text)
|
| 55 |
+
pages_and_texts.append({
|
| 56 |
+
"page_number": page_number,
|
| 57 |
+
"text": text,
|
| 58 |
+
"char_count": len(text),
|
| 59 |
+
"word_count": len(text.split(" ")),
|
| 60 |
+
"token_count": len(text)/4
|
| 61 |
+
})
|
| 62 |
+
|
| 63 |
+
with st.spinner("Chunking text..."):
|
| 64 |
+
nlp = English()
|
| 65 |
+
nlp.add_pipe("sentencizer")
|
| 66 |
+
for item in tqdm(pages_and_texts):
|
| 67 |
+
item['sentences'] = [str(s) for s in nlp(item["text"]).sents]
|
| 68 |
+
item["sentence_chunks"] = split_list(item["sentences"])
|
| 69 |
+
|
| 70 |
+
pages_and_chunks = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
for item in tqdm(pages_and_texts):
|
| 72 |
+
for sentence_chunk in item["sentence_chunks"]:
|
| 73 |
+
chunk_text = " ".join(sentence_chunk).replace(" ", " ").strip()
|
| 74 |
+
pages_and_chunks.append({
|
| 75 |
+
"page_number": item["page_number"],
|
| 76 |
+
"sentence_chunk": chunk_text,
|
| 77 |
+
"chunk_token_count": len(chunk_text)/4
|
| 78 |
+
})
|
| 79 |
+
|
| 80 |
+
return [c for c in pages_and_chunks if c["chunk_token_count"] > MIN_TOKEN_LENGTH]
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
except Exception as e:
|
| 83 |
+
st.error(f"Error processing PDF: {str(e)}")
|
| 84 |
+
return None
|
| 85 |
|
| 86 |
# Model Loading
|
| 87 |
@st.cache_resource
|
| 88 |
def load_models():
|
| 89 |
+
try:
|
| 90 |
+
embedding_model = SentenceTransformer(MODEL_NAME, device=DEVICE)
|
| 91 |
+
quantization_config = BitsAndBytesConfig(load_in_4bit=True)
|
| 92 |
+
tokenizer = AutoTokenizer.from_pretrained(LLM_MODEL_ID)
|
| 93 |
+
llm_model = AutoModelForCausalLM.from_pretrained(
|
| 94 |
+
LLM_MODEL_ID,
|
| 95 |
+
quantization_config=quantization_config,
|
| 96 |
+
device_map="auto"
|
| 97 |
+
)
|
| 98 |
+
return embedding_model, tokenizer, llm_model
|
| 99 |
+
except Exception as e:
|
| 100 |
+
st.error(f"Error loading models: {str(e)}")
|
| 101 |
+
return None, None, None
|
| 102 |
|
| 103 |
# Streamlit UI
|
| 104 |
st.title("PDF Knowledge Assistant 📚")
|
|
|
|
| 108 |
with st.sidebar:
|
| 109 |
st.header("Document Setup")
|
| 110 |
uploaded_file = st.file_uploader("Upload PDF", type=["pdf"])
|
| 111 |
+
if uploaded_file and not st.session_state.processed:
|
| 112 |
+
st.session_state.pages_and_chunks = process_pdf(uploaded_file)
|
| 113 |
+
if st.session_state.pages_and_chunks:
|
| 114 |
embedding_model, _, _ = load_models()
|
| 115 |
+
if embedding_model:
|
| 116 |
+
with st.spinner("Generating embeddings..."):
|
| 117 |
+
texts = [c["sentence_chunk"] for c in st.session_state.pages_and_chunks]
|
| 118 |
+
st.session_state.embeddings = torch.tensor(
|
| 119 |
+
embedding_model.encode(texts, convert_to_tensor=True),
|
| 120 |
+
device=DEVICE
|
| 121 |
+
)
|
| 122 |
+
st.session_state.processed = True
|
| 123 |
|
| 124 |
# Main Q&A Interface
|
| 125 |
if st.session_state.processed:
|
|
|
|
| 127 |
|
| 128 |
if query:
|
| 129 |
embedding_model, tokenizer, llm_model = load_models()
|
| 130 |
+
if None in (embedding_model, tokenizer, llm_model):
|
| 131 |
+
st.error("Failed to load models")
|
| 132 |
+
st.stop()
|
| 133 |
|
| 134 |
+
try:
|
| 135 |
+
# Retrieve relevant chunks
|
| 136 |
+
query_embedding = embedding_model.encode(query, convert_to_tensor=True)
|
| 137 |
+
scores = util.dot_score(query_embedding, st.session_state.embeddings)[0]
|
| 138 |
+
top_results = torch.topk(scores, k=NUM_RESULTS)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
+
# Display relevant passages
|
| 141 |
+
st.subheader("Most Relevant Passages:")
|
| 142 |
+
for score, idx in zip(top_results[0], top_results[1]):
|
| 143 |
+
with st.expander(f"Relevance: {score:.2f}"):
|
| 144 |
+
st.write(st.session_state.pages_and_chunks[idx]["sentence_chunk"])
|
| 145 |
+
st.caption(f"Page: {st.session_state.pages_and_chunks[idx]['page_number']+1}")
|
| 146 |
|
| 147 |
+
# Generate answer
|
| 148 |
+
with st.spinner("Generating answer..."):
|
| 149 |
+
context = [st.session_state.pages_and_chunks[i] for i in top_results[1]]
|
| 150 |
+
context_text = "\n".join([f"Context {i+1}: {c['sentence_chunk']}"
|
| 151 |
+
for i, c in enumerate(context)])
|
| 152 |
+
|
| 153 |
+
# Improved prompt template
|
| 154 |
+
prompt = f"""Answer the following question based on the context below.
|
| 155 |
+
Question: {query}
|
| 156 |
+
Context: {context_text}
|
| 157 |
+
Provide a detailed, technical answer with references to the document content."""
|
| 158 |
+
|
| 159 |
+
# Format for Gemma instruction-following
|
| 160 |
+
dialogue = [{"role": "user", "content": prompt}]
|
| 161 |
+
formatted_prompt = tokenizer.apply_chat_template(
|
| 162 |
+
dialogue,
|
| 163 |
+
tokenize=False,
|
| 164 |
+
add_generation_prompt=True
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
inputs = tokenizer(formatted_prompt, return_tensors="pt").to(DEVICE)
|
| 168 |
+
outputs = llm_model.generate(**inputs, max_new_tokens=1000)
|
| 169 |
+
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 170 |
+
|
| 171 |
+
# Clean up the answer
|
| 172 |
+
answer = answer.replace(formatted_prompt, "").strip()
|
| 173 |
+
|
| 174 |
+
st.subheader("Generated Answer:")
|
| 175 |
+
st.write(print_wrapped(answer))
|
| 176 |
+
|
| 177 |
+
except Exception as e:
|
| 178 |
+
st.error(f"Error generating answer: {str(e)}")
|
| 179 |
+
|
| 180 |
else:
|
| 181 |
st.info("Please upload a PDF document to get started")
|