Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -24,7 +24,7 @@ rag_llm.verbose = True
|
|
24 |
# Clear ChromaDB cache to fix tenant issue
|
25 |
chromadb.api.client.SharedSystemClient.clear_system_cache()
|
26 |
|
27 |
-
st.title("Blah")
|
28 |
|
29 |
# **Initialize session state variables**
|
30 |
if "pdf_path" not in st.session_state:
|
@@ -56,7 +56,7 @@ if pdf_source == "Upload a PDF file":
|
|
56 |
st.session_state.vector_created = False
|
57 |
|
58 |
elif pdf_source == "Enter a PDF URL":
|
59 |
-
pdf_url = st.text_input("Enter PDF URL:"
|
60 |
if pdf_url and not st.session_state.get("pdf_loaded", False):
|
61 |
with st.spinner("Downloading PDF..."):
|
62 |
try:
|
@@ -74,7 +74,7 @@ elif pdf_source == "Enter a PDF URL":
|
|
74 |
except Exception as e:
|
75 |
st.error(f"Error downloading PDF: {e}")
|
76 |
|
77 |
-
# Step 2: Process PDF
|
78 |
if st.session_state.pdf_path and not st.session_state.get("pdf_loaded", False):
|
79 |
with st.spinner("Loading and processing PDF..."):
|
80 |
loader = PDFPlumberLoader(st.session_state.pdf_path)
|
@@ -83,7 +83,7 @@ if st.session_state.pdf_path and not st.session_state.get("pdf_loaded", False):
|
|
83 |
st.session_state.pdf_loaded = True # β
Prevent re-loading
|
84 |
st.success(f"β
**PDF Loaded!** Total Pages: {len(docs)}")
|
85 |
|
86 |
-
# Step 3: Chunking
|
87 |
if st.session_state.get("pdf_loaded", False) and not st.session_state.get("chunked", False):
|
88 |
with st.spinner("Chunking the document..."):
|
89 |
model_name = "nomic-ai/modernbert-embed-base"
|
@@ -94,12 +94,11 @@ if st.session_state.get("pdf_loaded", False) and not st.session_state.get("chunk
|
|
94 |
st.session_state.chunked = True # β
Prevent re-chunking
|
95 |
st.success(f"β
**Document Chunked!** Total Chunks: {len(documents)}")
|
96 |
|
97 |
-
# Step 4: Setup Vectorstore
|
98 |
if st.session_state.get("chunked", False) and not st.session_state.get("vector_created", False):
|
99 |
with st.spinner("Creating vector store..."):
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
vector_store = Chroma(
|
104 |
collection_name="deepseek_collection",
|
105 |
collection_metadata={"hnsw:space": "cosine"},
|
@@ -112,11 +111,11 @@ if st.session_state.get("chunked", False) and not st.session_state.get("vector_c
|
|
112 |
st.session_state.vector_created = True # β
Prevent re-creating vector store
|
113 |
st.success(f"β
**Vector Store Created!** Total documents stored: {num_documents}")
|
114 |
|
115 |
-
# Step 5: Query Input
|
116 |
if st.session_state.get("vector_created", False) and st.session_state.get("vector_store", None):
|
117 |
query = st.text_input("π Enter a Query:")
|
118 |
|
119 |
-
if query:
|
120 |
with st.spinner("Retrieving relevant contexts..."):
|
121 |
retriever = st.session_state.vector_store.as_retriever(search_type="similarity", search_kwargs={"k": 5})
|
122 |
contexts = retriever.invoke(query)
|
|
|
24 |
# Clear ChromaDB cache to fix tenant issue
|
25 |
chromadb.api.client.SharedSystemClient.clear_system_cache()
|
26 |
|
27 |
+
st.title("Blah - 1")
|
28 |
|
29 |
# **Initialize session state variables**
|
30 |
if "pdf_path" not in st.session_state:
|
|
|
56 |
st.session_state.vector_created = False
|
57 |
|
58 |
elif pdf_source == "Enter a PDF URL":
|
59 |
+
pdf_url = st.text_input("Enter PDF URL:")
|
60 |
if pdf_url and not st.session_state.get("pdf_loaded", False):
|
61 |
with st.spinner("Downloading PDF..."):
|
62 |
try:
|
|
|
74 |
except Exception as e:
|
75 |
st.error(f"Error downloading PDF: {e}")
|
76 |
|
77 |
+
# Step 2: Process PDF
|
78 |
if st.session_state.pdf_path and not st.session_state.get("pdf_loaded", False):
|
79 |
with st.spinner("Loading and processing PDF..."):
|
80 |
loader = PDFPlumberLoader(st.session_state.pdf_path)
|
|
|
83 |
st.session_state.pdf_loaded = True # β
Prevent re-loading
|
84 |
st.success(f"β
**PDF Loaded!** Total Pages: {len(docs)}")
|
85 |
|
86 |
+
# Step 3: Chunking
|
87 |
if st.session_state.get("pdf_loaded", False) and not st.session_state.get("chunked", False):
|
88 |
with st.spinner("Chunking the document..."):
|
89 |
model_name = "nomic-ai/modernbert-embed-base"
|
|
|
94 |
st.session_state.chunked = True # β
Prevent re-chunking
|
95 |
st.success(f"β
**Document Chunked!** Total Chunks: {len(documents)}")
|
96 |
|
97 |
+
# Step 4: Setup Vectorstore
|
98 |
if st.session_state.get("chunked", False) and not st.session_state.get("vector_created", False):
|
99 |
with st.spinner("Creating vector store..."):
|
100 |
+
embedding_model = HuggingFaceEmbeddings(model_name="nomic-ai/modernbert-embed-base", model_kwargs={'device': 'cpu'}, encode_kwargs={'normalize_embeddings': False})
|
101 |
+
|
|
|
102 |
vector_store = Chroma(
|
103 |
collection_name="deepseek_collection",
|
104 |
collection_metadata={"hnsw:space": "cosine"},
|
|
|
111 |
st.session_state.vector_created = True # β
Prevent re-creating vector store
|
112 |
st.success(f"β
**Vector Store Created!** Total documents stored: {num_documents}")
|
113 |
|
114 |
+
# Step 5: Query Input
|
115 |
if st.session_state.get("vector_created", False) and st.session_state.get("vector_store", None):
|
116 |
query = st.text_input("π Enter a Query:")
|
117 |
|
118 |
+
if query and st.session_state.get("vector_created", False):
|
119 |
with st.spinner("Retrieving relevant contexts..."):
|
120 |
retriever = st.session_state.vector_store.as_retriever(search_type="similarity", search_kwargs={"k": 5})
|
121 |
contexts = retriever.invoke(query)
|