Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -43,47 +43,35 @@ st.title("Blah-2")
|
|
| 43 |
# Step 1: Choose PDF Source
|
| 44 |
pdf_source = st.radio("Upload or provide a link to a PDF:", ["Enter a PDF URL", "Upload a PDF file"], index=0, horizontal=True)
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
with st.spinner("Downloading PDF..."):
|
| 50 |
try:
|
| 51 |
-
response = requests.get(
|
| 52 |
if response.status_code == 200:
|
| 53 |
st.session_state.pdf_path = "temp.pdf"
|
| 54 |
with open(st.session_state.pdf_path, "wb") as f:
|
| 55 |
f.write(response.content)
|
| 56 |
-
|
| 57 |
-
# Reset processing state
|
| 58 |
st.session_state.pdf_loaded = False
|
| 59 |
st.session_state.chunked = False
|
| 60 |
st.session_state.vector_created = False
|
| 61 |
-
|
| 62 |
st.success("β
PDF Downloaded Successfully!")
|
| 63 |
else:
|
| 64 |
st.error("β Failed to download PDF. Check the URL.")
|
| 65 |
except Exception as e:
|
| 66 |
st.error(f"β Error downloading PDF: {e}")
|
| 67 |
|
| 68 |
-
if pdf_source == "Upload a PDF file":
|
| 69 |
-
uploaded_file = st.file_uploader("Upload your PDF file", type="pdf")
|
| 70 |
-
if uploaded_file:
|
| 71 |
-
st.session_state.pdf_path = "temp.pdf"
|
| 72 |
-
with open(st.session_state.pdf_path, "wb") as f:
|
| 73 |
-
f.write(uploaded_file.getbuffer())
|
| 74 |
-
st.session_state.pdf_loaded = False
|
| 75 |
-
st.session_state.chunked = False
|
| 76 |
-
st.session_state.vector_created = False
|
| 77 |
-
|
| 78 |
-
elif pdf_source == "Enter a PDF URL":
|
| 79 |
-
# β
Text input with Enter support
|
| 80 |
-
st.text_input("Enter PDF URL:", value="https://arxiv.org/pdf/2406.06998", key="pdf_url", on_change=download_pdf)
|
| 81 |
-
|
| 82 |
-
# β
Button support
|
| 83 |
-
if st.button("Download and Process PDF"):
|
| 84 |
-
download_pdf()
|
| 85 |
-
|
| 86 |
-
|
| 87 |
# Step 2: Load & Process PDF (Only Once)
|
| 88 |
if st.session_state.pdf_path and not st.session_state.pdf_loaded:
|
| 89 |
with st.spinner("Loading PDF..."):
|
|
@@ -132,17 +120,7 @@ if st.session_state.pdf_loaded and not st.session_state.chunked:
|
|
| 132 |
|
| 133 |
# Step 4: Setup Vectorstore
|
| 134 |
def load_vector_store():
|
| 135 |
-
|
| 136 |
-
vector_store = Chroma(
|
| 137 |
-
persist_directory=VECTOR_DB_PATH,
|
| 138 |
-
collection_name="deepseek_collection",
|
| 139 |
-
embedding_function=HuggingFaceEmbeddings(model_name="nomic-ai/modernbert-embed-base")
|
| 140 |
-
)
|
| 141 |
-
st.success("β
Vector store loaded successfully!")
|
| 142 |
-
return vector_store
|
| 143 |
-
except Exception as e:
|
| 144 |
-
st.error(f"β Failed to load vector store: {e}")
|
| 145 |
-
return None # Return None if there's an error
|
| 146 |
|
| 147 |
if st.session_state.chunked and not st.session_state.vector_created:
|
| 148 |
with st.spinner("Creating vector store..."):
|
|
@@ -169,11 +147,7 @@ st.write("π **Vector Store Created:**", st.session_state.vector_created)
|
|
| 169 |
query = st.text_input("π Ask a question about the document:")
|
| 170 |
if query:
|
| 171 |
with st.spinner("π Retrieving relevant context..."):
|
| 172 |
-
|
| 173 |
-
st.error("β Vector store is not initialized. Ensure document processing and chunking are completed.")
|
| 174 |
-
else:
|
| 175 |
-
retriever = st.session_state.vector_store.as_retriever(search_type="similarity", search_kwargs={"k": 5})
|
| 176 |
-
|
| 177 |
contexts = retriever.invoke(query)
|
| 178 |
# Debugging: Check what was retrieved
|
| 179 |
st.write("Retrieved Contexts:", contexts)
|
|
|
|
| 43 |
# Step 1: Choose PDF Source
|
| 44 |
pdf_source = st.radio("Upload or provide a link to a PDF:", ["Enter a PDF URL", "Upload a PDF file"], index=0, horizontal=True)
|
| 45 |
|
| 46 |
+
if pdf_source == "Upload a PDF file":
|
| 47 |
+
uploaded_file = st.file_uploader("Upload your PDF file", type="pdf")
|
| 48 |
+
if uploaded_file:
|
| 49 |
+
st.session_state.pdf_path = "temp.pdf"
|
| 50 |
+
with open(st.session_state.pdf_path, "wb") as f:
|
| 51 |
+
f.write(uploaded_file.getbuffer())
|
| 52 |
+
st.session_state.pdf_loaded = False
|
| 53 |
+
st.session_state.chunked = False
|
| 54 |
+
st.session_state.vector_created = False
|
| 55 |
+
|
| 56 |
+
elif pdf_source == "Enter a PDF URL":
|
| 57 |
+
pdf_url = st.text_input("Enter PDF URL:", value = "https://arxiv.org/pdf/2406.06998")
|
| 58 |
+
if pdf_url and not st.session_state.pdf_path:
|
| 59 |
with st.spinner("Downloading PDF..."):
|
| 60 |
try:
|
| 61 |
+
response = requests.get(pdf_url)
|
| 62 |
if response.status_code == 200:
|
| 63 |
st.session_state.pdf_path = "temp.pdf"
|
| 64 |
with open(st.session_state.pdf_path, "wb") as f:
|
| 65 |
f.write(response.content)
|
|
|
|
|
|
|
| 66 |
st.session_state.pdf_loaded = False
|
| 67 |
st.session_state.chunked = False
|
| 68 |
st.session_state.vector_created = False
|
|
|
|
| 69 |
st.success("β
PDF Downloaded Successfully!")
|
| 70 |
else:
|
| 71 |
st.error("β Failed to download PDF. Check the URL.")
|
| 72 |
except Exception as e:
|
| 73 |
st.error(f"β Error downloading PDF: {e}")
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
# Step 2: Load & Process PDF (Only Once)
|
| 76 |
if st.session_state.pdf_path and not st.session_state.pdf_loaded:
|
| 77 |
with st.spinner("Loading PDF..."):
|
|
|
|
| 120 |
|
| 121 |
# Step 4: Setup Vectorstore
|
| 122 |
def load_vector_store():
|
| 123 |
+
return Chroma(persist_directory=VECTOR_DB_PATH, collection_name="deepseek_collection", embedding_function=HuggingFaceEmbeddings(model_name="nomic-ai/modernbert-embed-base"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
if st.session_state.chunked and not st.session_state.vector_created:
|
| 126 |
with st.spinner("Creating vector store..."):
|
|
|
|
| 147 |
query = st.text_input("π Ask a question about the document:")
|
| 148 |
if query:
|
| 149 |
with st.spinner("π Retrieving relevant context..."):
|
| 150 |
+
retriever = st.session_state.vector_store.as_retriever(search_type="similarity", search_kwargs={"k": 5})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
contexts = retriever.invoke(query)
|
| 152 |
# Debugging: Check what was retrieved
|
| 153 |
st.write("Retrieved Contexts:", contexts)
|