Update app.py
Browse files
app.py
CHANGED
@@ -1,98 +1,64 @@
|
|
1 |
import streamlit as st
|
2 |
import PyPDF2
|
|
|
3 |
from sentence_transformers import SentenceTransformer
|
4 |
import faiss
|
5 |
import numpy as np
|
6 |
from transformers import pipeline
|
7 |
|
8 |
-
st.set_page_config(page_title="π PDF
|
9 |
|
10 |
# Custom styles
|
11 |
st.markdown("""
|
12 |
<style>
|
13 |
.main {background-color: #f7faff;}
|
14 |
-
|
15 |
-
h1 {color: #4051b5;}
|
16 |
.stTextInput>div>div>input {border: 2px solid #d0d7ff;}
|
17 |
-
.stButton button {background-color: #
|
18 |
-
.stSidebar {background-color: #eaf0ff;}
|
19 |
-
.sample-dropdown label {font-weight: bold;}
|
20 |
</style>
|
21 |
""", unsafe_allow_html=True)
|
22 |
|
23 |
-
st.title("π Ask Me Anything
|
24 |
-
st.caption("
|
25 |
|
26 |
-
|
27 |
-
|
28 |
|
29 |
-
|
30 |
-
"
|
31 |
-
|
32 |
-
|
33 |
-
"How is ML used in computer vision?",
|
34 |
-
"Describe the importance of training data."
|
35 |
-
]
|
36 |
-
|
37 |
-
@st.cache_data
|
38 |
-
def load_pdf(file):
|
39 |
-
reader = PyPDF2.PdfReader(file)
|
40 |
-
return [page.extract_text() for page in reader.pages]
|
41 |
|
42 |
def chunk_text(pages, max_len=1000):
|
43 |
text = " ".join(pages)
|
44 |
words = text.split()
|
45 |
return [' '.join(words[i:i+max_len]) for i in range(0, len(words), max_len)]
|
46 |
|
47 |
-
|
|
|
|
|
|
|
|
|
48 |
embeddings = model.encode(chunks)
|
49 |
index = faiss.IndexFlatL2(embeddings.shape[1])
|
50 |
index.add(np.array(embeddings))
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
st.
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
question = st.text_input("Enter your question here...", placeholder="e.g. What is deep learning?")
|
72 |
-
with col2:
|
73 |
-
if st.button("Ask"):
|
74 |
-
with st.spinner("π§ Thinking..."):
|
75 |
-
context = retrieve_context(question, chunks, index, model)
|
76 |
-
result = qa_pipeline(question=question, context=context)
|
77 |
-
with st.expander("π Answer", expanded=True):
|
78 |
-
st.markdown(result['answer'])
|
79 |
-
|
80 |
-
st.divider()
|
81 |
-
st.subheader("β¨ Sample Questions")
|
82 |
-
selected_q = st.selectbox("Pick one to try:", default_questions, key="sample-dropdown")
|
83 |
-
if st.button("Try Selected Question"):
|
84 |
-
with st.spinner("β³ Searching..."):
|
85 |
-
context = retrieve_context(selected_q, chunks, index, model)
|
86 |
-
result = qa_pipeline(question=selected_q, context=context)
|
87 |
-
with st.expander(f"π‘ Answer to: '{selected_q}'", expanded=True):
|
88 |
-
st.markdown(result['answer'])
|
89 |
-
|
90 |
-
st.divider()
|
91 |
-
st.subheader("π Preview PDF Pages")
|
92 |
-
for i, page in enumerate(pages[:3]):
|
93 |
-
st.markdown(f"**Page {i+1}**")
|
94 |
-
st.code(page[:800] + "..." if len(page) > 800 else page)
|
95 |
-
|
96 |
-
else:
|
97 |
-
st.info("Upload a PDF from the sidebar to begin.")
|
98 |
-
|
|
|
1 |
import streamlit as st
|
2 |
import PyPDF2
|
3 |
+
import os
|
4 |
from sentence_transformers import SentenceTransformer
|
5 |
import faiss
|
6 |
import numpy as np
|
7 |
from transformers import pipeline
|
8 |
|
9 |
+
st.set_page_config(page_title="π PDF RAG QA", layout="wide")
|
10 |
|
11 |
# Custom styles
|
12 |
st.markdown("""
|
13 |
<style>
|
14 |
.main {background-color: #f7faff;}
|
15 |
+
h1 {color: #4a4a8a;}
|
|
|
16 |
.stTextInput>div>div>input {border: 2px solid #d0d7ff;}
|
17 |
+
.stButton button {background-color: #4a4a8a; color: white;}
|
|
|
|
|
18 |
</style>
|
19 |
""", unsafe_allow_html=True)
|
20 |
|
21 |
+
st.title("π Ask Me Anything About Machine Learning")
|
22 |
+
st.caption("Using RAG (Retrieval-Augmented Generation) and a preloaded PDF")
|
23 |
|
24 |
+
# Load PDF from local file
|
25 |
+
PDF_FILE = "data.pdf"
|
26 |
|
27 |
+
def load_pdf(file_path):
|
28 |
+
with open(file_path, "rb") as f:
|
29 |
+
reader = PyPDF2.PdfReader(f)
|
30 |
+
return [page.extract_text() for page in reader.pages]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
def chunk_text(pages, max_len=1000):
|
33 |
text = " ".join(pages)
|
34 |
words = text.split()
|
35 |
return [' '.join(words[i:i+max_len]) for i in range(0, len(words), max_len)]
|
36 |
|
37 |
+
@st.cache_resource
|
38 |
+
def setup_rag():
|
39 |
+
pages = load_pdf(PDF_FILE)
|
40 |
+
chunks = chunk_text(pages)
|
41 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
42 |
embeddings = model.encode(chunks)
|
43 |
index = faiss.IndexFlatL2(embeddings.shape[1])
|
44 |
index.add(np.array(embeddings))
|
45 |
+
qa = pipeline("question-answering", model="deepset/roberta-base-squad2")
|
46 |
+
return chunks, model, index, qa
|
47 |
+
|
48 |
+
def retrieve_answer(question, chunks, model, index, qa_pipeline, k=6):
|
49 |
+
q_embed = model.encode([question])
|
50 |
+
_, I = index.search(np.array(q_embed), k)
|
51 |
+
context = "\n\n".join([chunks[i] for i in I[0]])
|
52 |
+
result = qa_pipeline(question=question, context=context)
|
53 |
+
return result['answer']
|
54 |
+
|
55 |
+
chunks, embed_model, faiss_index, qa_model = setup_rag()
|
56 |
+
|
57 |
+
st.subheader("π¬ Ask a Question")
|
58 |
+
question = st.text_input("Enter your question:", placeholder="e.g., What is supervised learning?")
|
59 |
+
|
60 |
+
if question:
|
61 |
+
with st.spinner("π§ Searching for the answer..."):
|
62 |
+
answer = retrieve_answer(question, chunks, embed_model, faiss_index, qa_model)
|
63 |
+
st.markdown("#### π Answer:")
|
64 |
+
st.write(answer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|