Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,11 +26,7 @@ chromadb.api.client.SharedSystemClient.clear_system_cache()
|
|
| 26 |
|
| 27 |
st.title("Blah")
|
| 28 |
|
| 29 |
-
# Initialize session state variables
|
| 30 |
-
if "vector_store" not in st.session_state:
|
| 31 |
-
st.session_state.vector_store = None
|
| 32 |
-
if "documents" not in st.session_state:
|
| 33 |
-
st.session_state.documents = None
|
| 34 |
if "pdf_path" not in st.session_state:
|
| 35 |
st.session_state.pdf_path = None
|
| 36 |
if "pdf_loaded" not in st.session_state:
|
|
@@ -39,8 +35,14 @@ if "chunked" not in st.session_state:
|
|
| 39 |
st.session_state.chunked = False
|
| 40 |
if "vector_created" not in st.session_state:
|
| 41 |
st.session_state.vector_created = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
# Step 1: Choose PDF Source
|
| 44 |
pdf_source = st.radio("Upload or provide a link to a PDF:", ["Upload a PDF file", "Enter a PDF URL"], index=0, horizontal=True)
|
| 45 |
|
| 46 |
if pdf_source == "Upload a PDF file":
|
|
@@ -81,7 +83,7 @@ if st.session_state.pdf_path and not st.session_state.pdf_loaded:
|
|
| 81 |
st.session_state.pdf_loaded = True
|
| 82 |
st.success(f"β
**PDF Loaded!** Total Pages: {len(docs)}")
|
| 83 |
|
| 84 |
-
# Step 3: Chunking
|
| 85 |
if st.session_state.pdf_loaded and not st.session_state.chunked:
|
| 86 |
with st.spinner("Chunking the document..."):
|
| 87 |
model_name = "nomic-ai/modernbert-embed-base"
|
|
@@ -98,7 +100,8 @@ if st.session_state.chunked and not st.session_state.vector_created:
|
|
| 98 |
vector_store = Chroma(
|
| 99 |
collection_name="deepseek_collection",
|
| 100 |
collection_metadata={"hnsw:space": "cosine"},
|
| 101 |
-
embedding_function=embedding_model
|
|
|
|
| 102 |
)
|
| 103 |
vector_store.add_documents(st.session_state.documents)
|
| 104 |
num_documents = len(vector_store.get()["documents"])
|
|
@@ -109,7 +112,8 @@ if st.session_state.chunked and not st.session_state.vector_created:
|
|
| 109 |
# Step 5: Query Input
|
| 110 |
if st.session_state.vector_created:
|
| 111 |
query = st.text_input("π Enter a Query:")
|
| 112 |
-
|
|
|
|
| 113 |
with st.spinner("Retrieving relevant contexts..."):
|
| 114 |
retriever = st.session_state.vector_store.as_retriever(search_type="similarity", search_kwargs={"k": 5})
|
| 115 |
contexts = retriever.invoke(query)
|
|
@@ -119,41 +123,11 @@ if st.session_state.vector_created:
|
|
| 119 |
for i, text in enumerate(context_texts, 1):
|
| 120 |
st.write(f"**Context {i}:** {text[:500]}...")
|
| 121 |
|
| 122 |
-
# Step 6:
|
| 123 |
-
with st.spinner("Evaluating context relevancy..."):
|
| 124 |
-
context_relevancy_checker_prompt = PromptTemplate(input_variables=["retriever_query", "context"], template=relevancy_prompt)
|
| 125 |
-
context_relevancy_chain = LLMChain(llm=llm_judge, prompt=context_relevancy_checker_prompt, output_key="relevancy_response")
|
| 126 |
-
relevancy_response = context_relevancy_chain.invoke({"context": context_texts, "retriever_query": query})
|
| 127 |
-
|
| 128 |
-
st.subheader("π₯ Context Relevancy Evaluation")
|
| 129 |
-
st.json(relevancy_response['relevancy_response'])
|
| 130 |
-
|
| 131 |
-
# Step 7: Selecting Relevant Contexts
|
| 132 |
-
with st.spinner("Selecting the most relevant contexts..."):
|
| 133 |
-
relevant_prompt = PromptTemplate(input_variables=["relevancy_response"], template=relevant_context_picker_prompt)
|
| 134 |
-
pick_relevant_context_chain = LLMChain(llm=llm_judge, prompt=relevant_prompt, output_key="context_number")
|
| 135 |
-
relevant_response = pick_relevant_context_chain.invoke({"relevancy_response": relevancy_response['relevancy_response']})
|
| 136 |
-
|
| 137 |
-
st.subheader("π¦ Pick Relevant Context Chain")
|
| 138 |
-
st.json(relevant_response['context_number'])
|
| 139 |
-
|
| 140 |
-
# Step 8: Retrieving Context for Response Generation
|
| 141 |
-
with st.spinner("Retrieving final context..."):
|
| 142 |
-
context_prompt = PromptTemplate(input_variables=["context_number", "context"], template=response_synth)
|
| 143 |
-
relevant_contexts_chain = LLMChain(llm=llm_judge, prompt=context_prompt, output_key="relevant_contexts")
|
| 144 |
-
final_contexts = relevant_contexts_chain.invoke({"context_number": relevant_response['context_number'], "context": context_texts})
|
| 145 |
-
|
| 146 |
-
st.subheader("π₯ Relevant Contexts Extracted")
|
| 147 |
-
st.json(final_contexts['relevant_contexts'])
|
| 148 |
-
|
| 149 |
-
# Step 9: Generate Final Response
|
| 150 |
with st.spinner("Generating the final answer..."):
|
| 151 |
final_prompt = PromptTemplate(input_variables=["query", "context"], template=rag_prompt)
|
| 152 |
response_chain = LLMChain(llm=rag_llm, prompt=final_prompt, output_key="final_response")
|
| 153 |
-
final_response = response_chain.invoke({"query": query, "context":
|
| 154 |
|
| 155 |
st.subheader("π₯ RAG Final Response")
|
| 156 |
st.success(final_response['final_response'])
|
| 157 |
-
|
| 158 |
-
else:
|
| 159 |
-
st.warning("π Please upload or provide a PDF URL first.")
|
|
|
|
| 26 |
|
| 27 |
st.title("Blah")
|
| 28 |
|
| 29 |
+
# **Initialize session state variables**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
if "pdf_path" not in st.session_state:
|
| 31 |
st.session_state.pdf_path = None
|
| 32 |
if "pdf_loaded" not in st.session_state:
|
|
|
|
| 35 |
st.session_state.chunked = False
|
| 36 |
if "vector_created" not in st.session_state:
|
| 37 |
st.session_state.vector_created = False
|
| 38 |
+
if "vector_store_path" not in st.session_state:
|
| 39 |
+
st.session_state.vector_store_path = "./chroma_langchain_db"
|
| 40 |
+
if "vector_store" not in st.session_state:
|
| 41 |
+
st.session_state.vector_store = None
|
| 42 |
+
if "documents" not in st.session_state:
|
| 43 |
+
st.session_state.documents = None
|
| 44 |
|
| 45 |
+
# **Step 1: Choose PDF Source**
|
| 46 |
pdf_source = st.radio("Upload or provide a link to a PDF:", ["Upload a PDF file", "Enter a PDF URL"], index=0, horizontal=True)
|
| 47 |
|
| 48 |
if pdf_source == "Upload a PDF file":
|
|
|
|
| 83 |
st.session_state.pdf_loaded = True
|
| 84 |
st.success(f"β
**PDF Loaded!** Total Pages: {len(docs)}")
|
| 85 |
|
| 86 |
+
# Step 3: Chunking
|
| 87 |
if st.session_state.pdf_loaded and not st.session_state.chunked:
|
| 88 |
with st.spinner("Chunking the document..."):
|
| 89 |
model_name = "nomic-ai/modernbert-embed-base"
|
|
|
|
| 100 |
vector_store = Chroma(
|
| 101 |
collection_name="deepseek_collection",
|
| 102 |
collection_metadata={"hnsw:space": "cosine"},
|
| 103 |
+
embedding_function=embedding_model,
|
| 104 |
+
persist_directory=st.session_state.vector_store_path
|
| 105 |
)
|
| 106 |
vector_store.add_documents(st.session_state.documents)
|
| 107 |
num_documents = len(vector_store.get()["documents"])
|
|
|
|
| 112 |
# Step 5: Query Input
|
| 113 |
if st.session_state.vector_created:
|
| 114 |
query = st.text_input("π Enter a Query:")
|
| 115 |
+
|
| 116 |
+
if query and st.session_state.vector_store:
|
| 117 |
with st.spinner("Retrieving relevant contexts..."):
|
| 118 |
retriever = st.session_state.vector_store.as_retriever(search_type="similarity", search_kwargs={"k": 5})
|
| 119 |
contexts = retriever.invoke(query)
|
|
|
|
| 123 |
for i, text in enumerate(context_texts, 1):
|
| 124 |
st.write(f"**Context {i}:** {text[:500]}...")
|
| 125 |
|
| 126 |
+
# **Step 6: Generate Final Response**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
with st.spinner("Generating the final answer..."):
|
| 128 |
final_prompt = PromptTemplate(input_variables=["query", "context"], template=rag_prompt)
|
| 129 |
response_chain = LLMChain(llm=rag_llm, prompt=final_prompt, output_key="final_response")
|
| 130 |
+
final_response = response_chain.invoke({"query": query, "context": context_texts})
|
| 131 |
|
| 132 |
st.subheader("π₯ RAG Final Response")
|
| 133 |
st.success(final_response['final_response'])
|
|
|
|
|
|
|
|
|