-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
453 lines (376 loc) · 15.7 KB
/
app.py
File metadata and controls
453 lines (376 loc) · 15.7 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import streamlit as st
import os
import tempfile
from typing import List
# LangChain imports - SIMPLIFIED VERSION THAT WORKS
from langchain_community.document_loaders import PyPDFLoader, TextLoader, Docx2txtLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import Chroma
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_groq import ChatGroq
from langchain_google_genai import ChatGoogleGenerativeAI
# Page configuration
st.set_page_config(
page_title="Chat with Your Documents 📚",
page_icon="📚",
layout="wide",
initial_sidebar_state="expanded"
)
# Initialize session state
if "messages" not in st.session_state:
st.session_state.messages = []
if "vectorstore" not in st.session_state:
st.session_state.vectorstore = None
if "retriever" not in st.session_state:
st.session_state.retriever = None
if "llm" not in st.session_state:
st.session_state.llm = None
if "processed_files" not in st.session_state:
st.session_state.processed_files = []
import streamlit as st
# --- Sidebar Styling ---
import streamlit as st
# ---- ULTRA MODERN SIDEBAR STYLING ----
st.markdown("""
<style>
/* Sidebar Background */
[data-testid="stSidebar"] {
background: linear-gradient(160deg, #0f172a 0%, #1e293b 45%, #020617 100%);
color: #f1f5f9 !important;
padding: 1.4rem 1rem;
border-right: 1px solid rgba(255,255,255,0.05);
box-shadow: 4px 0 20px rgba(0,0,0,0.3);
}
/* Title */
.sidebar-title {
font-size: 1.6rem;
font-weight: 800;
background: linear-gradient(90deg, #38bdf8, #818cf8, #a855f7);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
text-align: center;
letter-spacing: 1px;
}
/* Glowing Divider */
.divider {
height: 1px;
margin: 1rem 0;
background: linear-gradient(90deg, rgba(56,189,248,0) 0%, rgba(56,189,248,0.7) 50%, rgba(56,189,248,0) 100%);
}
/* Section Box */
.glass-box {
background: rgba(255,255,255,0.05);
border: 1px solid rgba(255,255,255,0.08);
border-radius: 12px;
padding: 0.8rem 1rem;
margin-bottom: 1rem;
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
transition: all 0.3s ease;
}
.glass-box:hover {
background: rgba(255,255,255,0.07);
}
/* Dropdown & Inputs */
.stSelectbox, .stTextInput {
border-radius: 8px !important;
}
/* Buttons */
.stButton button {
border-radius: 10px;
background: linear-gradient(90deg, #2563eb, #9333ea);
color: #fff;
border: none;
box-shadow: 0 0 12px rgba(147,51,234,0.4);
transition: all 0.3s ease-in-out;
}
.stButton button:hover {
transform: translateY(-1px);
box-shadow: 0 0 18px rgba(147,51,234,0.7);
background: linear-gradient(90deg, #1d4ed8, #7e22ce);
}
/* Metrics */
[data-testid="stMetricValue"] {
color: #38bdf8 !important;
font-weight: 700 !important;
}
/* Caption */
.stCaption {
color: #94a3b8 !important;
text-align: center;
font-size: 0.8rem !important;
margin-top: 1.5rem;
}
</style>
""", unsafe_allow_html=True)
# ---- SIDEBAR CONTENT ----
with st.sidebar:
st.markdown('<div class="sidebar-title">🤖 Document Intelligence Bot</div>', unsafe_allow_html=True)
st.markdown('<div class="divider"></div>', unsafe_allow_html=True)
# --- How to Use ---
with st.expander("📘 How to Use", expanded=False):
st.markdown("""
**Quick Start Guide**
1️⃣ Select an AI Model
2️⃣ Enter your API Key
3️⃣ Upload PDF / DOCX / TXT
4️⃣ Click **Process Documents**
5️⃣ Ask your questions! 💬
**Supported Formats**
- 📄 PDF
- 📝 Word
- 📃 Text
""")
st.markdown('<div class="divider"></div>', unsafe_allow_html=True)
# --- Model Selection ---
st.markdown('<div class="glass-box">', unsafe_allow_html=True)
st.markdown("### 🧠 Model Selection")
model_option = st.selectbox(
"Choose AI Model:",
[
"⚡ Llama-3.2-3B (Groq) — Fast",
"🆓 Gemini 1.5 Flash — Free",
"💪 Mixtral-8x7B (Groq) — Powerful",
"⚖️ Llama-3.1-8B (Groq) — Balanced"
],
index=0
)
st.markdown('</div>', unsafe_allow_html=True)
# --- API Key ---
api_key = None
st.markdown('<div class="glass-box">', unsafe_allow_html=True)
if "Groq" in model_option:
api_key = st.text_input("🔑 API Key", type="password", help="Get your key at console.groq.com")
if not api_key:
st.info("👆 Enter your API key to continue")
elif "Gemini" in model_option:
api_key = st.text_input("🔑 Gemini API Key", type="password", help="Get your key at ai.google.dev")
if not api_key:
st.info("👆 Enter your Gemini API key to continue")
st.markdown('</div>', unsafe_allow_html=True)
# --- Stats ---
if st.session_state.get("vectorstore"):
st.markdown('<div class="glass-box">', unsafe_allow_html=True)
st.markdown("### 📊 Document Stats")
st.metric("Documents Loaded", len(st.session_state.get("processed_files", [])))
st.markdown('</div>', unsafe_allow_html=True)
# --- Actions ---
st.markdown('<div class="glass-box">', unsafe_allow_html=True)
st.markdown("### ⚙️ Quick Actions")
if st.button("🗑️ Clear Chat", use_container_width=True):
st.session_state.messages = []
st.rerun()
if st.button("📄 Reset Docs", use_container_width=True):
st.session_state.vectorstore = None
st.session_state.retriever = None
st.session_state.llm = None
st.session_state.processed_files = []
st.session_state.messages = []
st.rerun()
st.markdown('</div>', unsafe_allow_html=True)
# --- Export Chat ---
if st.session_state.get("messages"):
st.markdown('<div class="glass-box">', unsafe_allow_html=True)
chat_text = "\n\n".join([
f"{'USER' if msg['role'] == 'user' else 'ASSISTANT'}: {msg['content']}"
for msg in st.session_state.messages
])
st.download_button(
"💾 Export Chat History",
chat_text,
file_name="chat_history.txt",
mime="text/plain",
use_container_width=True
)
st.markdown('</div>', unsafe_allow_html=True)
# Footer
st.markdown('<div class="divider"></div>', unsafe_allow_html=True)
st.caption("Designed with ❤️ by CH Hussain Ali")
# Main content
st.title("📚 Chat with Your Documents")
st.markdown("Upload your documents and ask questions using AI")
# Function to load documents
def load_documents(uploaded_files):
"""Load documents from uploaded files"""
documents = []
temp_dir = tempfile.mkdtemp()
for uploaded_file in uploaded_files:
file_path = os.path.join(temp_dir, uploaded_file.name)
with open(file_path, "wb") as f:
f.write(uploaded_file.getbuffer())
try:
if uploaded_file.name.endswith('.pdf'):
loader = PyPDFLoader(file_path)
elif uploaded_file.name.endswith('.docx'):
loader = Docx2txtLoader(file_path)
elif uploaded_file.name.endswith('.txt'):
loader = TextLoader(file_path)
else:
continue
docs = loader.load()
for doc in docs:
doc.metadata['source'] = uploaded_file.name
documents.extend(docs)
except Exception as e:
st.error(f"Error loading {uploaded_file.name}: {str(e)}")
return documents
# Function to initialize LLM
def initialize_llm(model_option, api_key):
"""Initialize the selected LLM"""
try:
if "Llama-3.2-3B" in model_option:
return ChatGroq(model="llama-3.1-8b-instant", temperature=0.7, groq_api_key=api_key)
elif "Gemini" in model_option:
return ChatGoogleGenerativeAI(model="gemini-1.5-flash", google_api_key=api_key, temperature=0.7)
elif "Mixtral-8x7B" in model_option:
return ChatGroq(model="mixtral-8x7b-32768", temperature=0.7, groq_api_key=api_key)
elif "Llama-3.1-8B" in model_option:
return ChatGroq(model="llama-3.1-8b-instant", temperature=0.7, groq_api_key=api_key)
except Exception as e:
st.error(f"Error initializing model: {str(e)}")
return None
# Function to format documents
def format_docs(docs):
return "\n\n".join([doc.page_content for doc in docs])
# File upload section
st.subheader("📤 Upload Your Documents")
uploaded_files = st.file_uploader(
"Choose files (PDF, DOCX, TXT)",
type=['pdf', 'docx', 'txt'],
accept_multiple_files=True,
help="Upload one or more documents"
)
# Process documents button
if uploaded_files and api_key:
if st.button("🚀 Process Documents", type="primary", use_container_width=True):
with st.spinner("Processing your documents..."):
try:
progress_bar = st.progress(0)
status_text = st.empty()
# Load documents
status_text.text("📂 Loading documents...")
progress_bar.progress(20)
documents = load_documents(uploaded_files)
if not documents:
st.error("No documents loaded. Check your files.")
st.stop()
# Split documents
status_text.text("✂️ Splitting text...")
progress_bar.progress(40)
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200
)
chunks = text_splitter.split_documents(documents)
# Create embeddings
status_text.text("🧮 Creating embeddings...")
progress_bar.progress(60)
embeddings = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-MiniLM-L6-v2",
model_kwargs={'device': 'cpu'}
)
# Create vector store
status_text.text("💾 Building database...")
progress_bar.progress(80)
vectorstore = Chroma.from_documents(
documents=chunks,
embedding=embeddings
)
# Initialize LLM
status_text.text("🤖 Initializing AI...")
llm = initialize_llm(model_option, api_key)
if not llm:
st.error("Failed to initialize AI model.")
st.stop()
# Save to session
st.session_state.vectorstore = vectorstore
st.session_state.retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
st.session_state.llm = llm
st.session_state.processed_files = [f.name for f in uploaded_files]
progress_bar.progress(100)
status_text.empty()
progress_bar.empty()
st.success(f"✅ Processed {len(uploaded_files)} document(s) into {len(chunks)} chunks!")
st.balloons()
except Exception as e:
st.error(f"❌ Error: {str(e)}")
st.exception(e)
elif uploaded_files and not api_key:
st.warning("⚠️ Please enter your API key in the sidebar.")
# Chat interface
if st.session_state.retriever and st.session_state.llm:
st.markdown("---")
st.subheader("💬 Chat with Your Documents")
# Display chat history
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if "sources" in message and message["sources"]:
with st.expander("📚 View Sources"):
for idx, source in enumerate(message["sources"], 1):
st.markdown(f"**Source {idx}: {source['filename']}**")
st.text(source["content"][:300] + "...")
st.markdown("---")
# Chat input
if prompt := st.chat_input("Ask about your documents..."):
# Add user message
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
# Generate response
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
try:
# Get relevant documents
retrieved_docs = st.session_state.retriever.invoke(prompt)
# Create prompt
template = """Answer the question based only on the following context:
{context}
Question: {question}
Answer concisely based on the context provided."""
prompt_template = ChatPromptTemplate.from_template(template)
# Create chain using LCEL
chain = (
{"context": lambda x: format_docs(retrieved_docs), "question": RunnablePassthrough()}
| prompt_template
| st.session_state.llm
| StrOutputParser()
)
# Get answer
answer = chain.invoke(prompt)
# Display answer
st.markdown(answer)
# Prepare sources
source_info = []
for doc in retrieved_docs:
source_info.append({
"filename": doc.metadata.get("source", "Unknown"),
"content": doc.page_content
})
# Show sources
if source_info:
with st.expander("📚 View Sources"):
for idx, source in enumerate(source_info, 1):
st.markdown(f"**Source {idx}: {source['filename']}**")
st.text(source["content"][:300] + "...")
st.markdown("---")
# Add to history
st.session_state.messages.append({
"role": "assistant",
"content": answer,
"sources": source_info
})
except Exception as e:
error_msg = f"❌ Error: {str(e)}"
st.error(error_msg)
else:
st.info("👆 Upload documents and click 'Process Documents' to start!")
with st.expander("💡 Example Questions"):
st.markdown("""
- What is the main topic?
- Summarize the key points
- What dates are mentioned?
- List all names in the documents
""")