Spaces:
Sleeping
Sleeping
File size: 7,034 Bytes
204d2a4 2855745 204d2a4 2855745 4a9f6fa 204d2a4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | import streamlit as st
from rag_pipelline import (
extract_text_from_pdf,
split_text_into_chunks,
create_vector_store,
create_rag_agent,
get_answer
)
# Page Config-----
st.set_page_config(
page_title = "PDF Chatbot- using RAG",
page_icon = "π",
layout = "wide"
)
# Header-----
st.markdown("### π PDF Chatbot - RAG + Gemini")
st.markdown("Powered by Langchain and Gemini 2.5 Flash")
st.divider()
# Session State -----
if "agent" not in st.session_state:
st.session_state.agent = None
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
if "display_messages" not in st.session_state:
st.session_state.display_messages = []
if "pdf_processed" not in st.session_state:
st.session_state.pdf_processed = False
if "pdf_name" not in st.session_state:
st.session_state.pdf_name = ""
# Sidebar ----
with st.sidebar:
st.header("βοΈ Stack Info")
st.markdown("**Framework:** Langchain 1.2.10")
st.markdown("**LLM:** Gemini 2.5 Flash")
st.markdown("**Embeddings:** Google embedding-001")
st.markdown("**Vector Store:** FAISS")
st.divider()
st.header("π Upload or Select PDF")
# Upload a PDF
uploaded_file = st.file_uploader(
"Upload a PDF",
type = ["pdf"],
help = "Max 10 MB Β· Max 50 pages Β· Must have selectable text (not scanned)"
)
# Select a sample PDF
sample_pdf = st.selectbox(
"Or pick a sample PDF:",
["None", "Attention is All You Need", "2025 ICC Champions Trophy-Wikipedia.pdf"]
)
# Ensure only one PDF is uploaded at a time
chosen_file , chosen_name = None,""
if uploaded_file is not None:
chosen_file = uploaded_file
chosen_name = uploaded_file.name
elif sample_pdf != "None":
sample_map = {
"Attention is All You Need": "src/sample_pdf/Attention_is_all_you_need.pdf",
"2025 ICC Champions Trophy-Wikipedia.pdf":"src/sample_pdf/2025_ICC_Champions_Trophy-Wikipedia.pdf",
}
# Using a variable and closing after use
sample_path = sample_map.get(sample_pdf)
if sample_path:
try:
chosen_file = open(sample_path, "rb")
chosen_name = sample_pdf
st.info(f"π Using sample file: {chosen_name}")
except FileNotFoundError:
st.error(f"β Sample file not found: {sample_path}")
chosen_file = None
if chosen_file is not None:
if st.button("Process PDF", type = "primary", use_container_width = True):
with st.spinner("Step 1/4 - Extracting raw text"):
raw_text = extract_text_from_pdf(chosen_file)
# Close sample file after reading to avoid resource leak
if sample_pdf != "None" and hasattr(chosen_file, "close"):
chosen_file.close()
if not raw_text.strip():
st.error("β No text found, please check your PDF and confirm its text selectable")
else:
with st.spinner("Step 2/4 - Splitting text into chunks"):
chunks = split_text_into_chunks(raw_text)
with st.spinner("Step 3/4 - Creating embedding and vector store"):
vector_store = create_vector_store(chunks)
with st.spinner("Step 4/4 - Creating RAG Agent"):
st.session_state.agent = create_rag_agent(vector_store)
st.session_state.pdf_processed = True
st.session_state.pdf_name = chosen_name
st.session_state.chat_history = []
st.session_state.display_messages = []
st.success(f"β
Ready! {len(chunks)} chunks indexed")
if st.session_state.pdf_processed:
st.divider()
st.success(f" Active :\n{st.session_state.pdf_name}")
st.caption(f"Messages so far:{len(st.session_state.display_messages)}")
if st.button("Clear & Reset", use_container_width= True):
st.session_state.agent = None
st.session_state.chat_history = []
st.session_state.display_messages = []
st.session_state.pdf_processed = False
st.session_state.pdf_name = ""
st.rerun()
# Main Area -----
if not st.session_state.pdf_processed:
st.markdown("### How to use")
col1, col2, col3 = st.columns(3)
with col1:
st.markdown("Step 1 - Upload or select the PDF from sidebar")
with col2:
st.markdown("Step 2 - Click Process PDF")
with col3:
st.markdown("Step 3 - Ask your questions in the chat box")
st.divider()
else:
st.markdown(f"### Chatting with {st.session_state.pdf_name}")
# Display all previous messages
for msg in st.session_state.display_messages:
with st.chat_message(msg["role"]):
st.write(msg["content"])
# Show source chunks for assistant messages
if msg["role"] == "assistant" and msg.get("sources"):
with st.expander(" PDF Chunks used to generate this answer"):
for i, doc in enumerate(msg["sources"]):
st.markdown(f"**Chunk {i+1}:**")
st.markdown(f"> {doc.page_content[:400]}...")
st.divider()
#Chat Input
if st.session_state.pdf_processed:
user_question = st.chat_input(f"Ask Something about {st.session_state.pdf_name}...")
if user_question:
# Show user message
with st.chat_message("user"):
st.write(user_question)
# Store in both histories
st.session_state.chat_history.append({
"role":"user",
"content":user_question
})
st.session_state.display_messages.append({
"role": "user",
"content": user_question
})
# Get answer from agent
with st.chat_message("assistant"):
with st.spinner("Agent is searching PDF and thinking"):
answer, source_docs = get_answer(
st.session_state.agent,
user_question,
st.session_state.chat_history[:-1] # history without current question
)
st.write(answer)
if source_docs:
with st.expander(" PDF chunks used to generate this answer"):
for i, doc in enumerate(source_docs):
st.markdown(f"**Chunk {i+1}:**")
st.markdown(f"> {doc.page_content[:400]}...")
#Store assistant response
st.session_state.chat_history.append({
"role":"assistant",
"content" : answer
})
st.session_state.display_messages.append({
"role": "assistant",
"content": answer,
"sources":source_docs
})
|