Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,10 +19,6 @@ st.title("Blah-1")
|
|
| 19 |
# ----------------- API Keys -----------------
|
| 20 |
os.environ["GROQ_API_KEY"] = st.secrets.get("GROQ_API_KEY", "")
|
| 21 |
|
| 22 |
-
# ----------------- Ensure Vector Store Directory Exists -----------------
|
| 23 |
-
if not os.path.exists("./chroma_langchain_db"):
|
| 24 |
-
os.makedirs("./chroma_langchain_db")
|
| 25 |
-
|
| 26 |
# ----------------- Clear ChromaDB Cache -----------------
|
| 27 |
chromadb.api.client.SharedSystemClient.clear_system_cache()
|
| 28 |
|
|
@@ -38,7 +34,7 @@ if "processed_chunks" not in st.session_state:
|
|
| 38 |
if "vector_store" not in st.session_state:
|
| 39 |
st.session_state.vector_store = None
|
| 40 |
|
| 41 |
-
# ----------------- Load Models
|
| 42 |
llm_judge = ChatGroq(model="deepseek-r1-distill-llama-70b")
|
| 43 |
rag_llm = ChatGroq(model="mixtral-8x7b-32768")
|
| 44 |
|
|
@@ -46,7 +42,7 @@ rag_llm = ChatGroq(model="mixtral-8x7b-32768")
|
|
| 46 |
llm_judge.verbose = True
|
| 47 |
rag_llm.verbose = True
|
| 48 |
|
| 49 |
-
# ----------------- PDF Selection
|
| 50 |
st.sidebar.subheader("π PDF Selection")
|
| 51 |
pdf_source = st.radio("Choose a PDF source:", ["Upload a PDF file", "Enter a PDF URL"], index=0, horizontal=True)
|
| 52 |
|
|
@@ -89,26 +85,25 @@ if not st.session_state.pdf_loaded and "pdf_path" in st.session_state:
|
|
| 89 |
model_name = "nomic-ai/modernbert-embed-base"
|
| 90 |
embedding_model = HuggingFaceEmbeddings(model_name=model_name, model_kwargs={"device": "cpu"})
|
| 91 |
|
| 92 |
-
#
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
# Store chunks in session state
|
| 97 |
-
st.session_state.processed_chunks = document_chunks
|
| 98 |
st.session_state.pdf_loaded = True
|
| 99 |
st.success("β
Document processed and chunked successfully!")
|
| 100 |
|
| 101 |
-
# ----------------- Setup Vector Store
|
| 102 |
if not st.session_state.vector_created and st.session_state.processed_chunks:
|
| 103 |
with st.spinner("π Initializing Vector Store..."):
|
| 104 |
-
vector_store = Chroma(
|
| 105 |
collection_name="deepseek_collection",
|
| 106 |
collection_metadata={"hnsw:space": "cosine"},
|
| 107 |
-
embedding_function=embedding_model
|
| 108 |
-
persist_directory="./chroma_langchain_db"
|
| 109 |
)
|
| 110 |
-
vector_store.add_documents(st.session_state.processed_chunks)
|
| 111 |
-
st.session_state.vector_store = vector_store
|
| 112 |
st.session_state.vector_created = True
|
| 113 |
st.success("β
Vector store initialized successfully!")
|
| 114 |
|
|
@@ -124,34 +119,23 @@ if query:
|
|
| 124 |
|
| 125 |
# ----------------- Full SequentialChain Execution -----------------
|
| 126 |
with st.spinner("π Running full pipeline..."):
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
relevant_contexts_chain = LLMChain(llm=llm_judge, prompt=context_prompt, output_key="relevant_contexts")
|
| 135 |
-
response_chain = LLMChain(llm=rag_llm, prompt=final_prompt, output_key="final_response")
|
| 136 |
-
|
| 137 |
-
context_management_chain = SequentialChain(
|
| 138 |
-
chains=[context_relevancy_chain, relevant_context_chain, relevant_contexts_chain, response_chain],
|
| 139 |
input_variables=["context", "retriever_query", "query"],
|
| 140 |
output_variables=["relevancy_response", "context_number", "relevant_contexts", "final_response"]
|
| 141 |
-
)
|
| 142 |
-
|
| 143 |
-
final_output = context_management_chain.invoke({"context": context, "retriever_query": query, "query": query})
|
| 144 |
-
st.success("β
Full pipeline executed successfully!")
|
| 145 |
|
| 146 |
-
# ----------------- Display All Outputs
|
| 147 |
-
st.
|
| 148 |
st.json(final_output["relevancy_response"])
|
| 149 |
-
|
| 150 |
-
st.markdown("### π¦ Picked Relevant Contexts")
|
| 151 |
st.json(final_output["context_number"])
|
| 152 |
-
|
| 153 |
-
st.markdown("### π₯ Extracted Relevant Contexts")
|
| 154 |
st.json(final_output["relevant_contexts"])
|
| 155 |
-
|
| 156 |
-
st.
|
| 157 |
-
st.write(final_output["final_response"])
|
|
|
|
| 19 |
# ----------------- API Keys -----------------
|
| 20 |
os.environ["GROQ_API_KEY"] = st.secrets.get("GROQ_API_KEY", "")
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# ----------------- Clear ChromaDB Cache -----------------
|
| 23 |
chromadb.api.client.SharedSystemClient.clear_system_cache()
|
| 24 |
|
|
|
|
| 34 |
if "vector_store" not in st.session_state:
|
| 35 |
st.session_state.vector_store = None
|
| 36 |
|
| 37 |
+
# ----------------- Load Models -----------------
|
| 38 |
llm_judge = ChatGroq(model="deepseek-r1-distill-llama-70b")
|
| 39 |
rag_llm = ChatGroq(model="mixtral-8x7b-32768")
|
| 40 |
|
|
|
|
| 42 |
llm_judge.verbose = True
|
| 43 |
rag_llm.verbose = True
|
| 44 |
|
| 45 |
+
# ----------------- PDF Selection -----------------
|
| 46 |
st.sidebar.subheader("π PDF Selection")
|
| 47 |
pdf_source = st.radio("Choose a PDF source:", ["Upload a PDF file", "Enter a PDF URL"], index=0, horizontal=True)
|
| 48 |
|
|
|
|
| 85 |
model_name = "nomic-ai/modernbert-embed-base"
|
| 86 |
embedding_model = HuggingFaceEmbeddings(model_name=model_name, model_kwargs={"device": "cpu"})
|
| 87 |
|
| 88 |
+
# Prevent unnecessary re-chunking
|
| 89 |
+
if not st.session_state.chunked:
|
| 90 |
+
text_splitter = SemanticChunker(embedding_model)
|
| 91 |
+
document_chunks = text_splitter.split_documents(docs)
|
| 92 |
+
st.session_state.processed_chunks = document_chunks
|
| 93 |
+
st.session_state.chunked = True
|
| 94 |
|
|
|
|
|
|
|
| 95 |
st.session_state.pdf_loaded = True
|
| 96 |
st.success("β
Document processed and chunked successfully!")
|
| 97 |
|
| 98 |
+
# ----------------- Setup Vector Store -----------------
|
| 99 |
if not st.session_state.vector_created and st.session_state.processed_chunks:
|
| 100 |
with st.spinner("π Initializing Vector Store..."):
|
| 101 |
+
st.session_state.vector_store = Chroma(
|
| 102 |
collection_name="deepseek_collection",
|
| 103 |
collection_metadata={"hnsw:space": "cosine"},
|
| 104 |
+
embedding_function=embedding_model
|
|
|
|
| 105 |
)
|
| 106 |
+
st.session_state.vector_store.add_documents(st.session_state.processed_chunks)
|
|
|
|
| 107 |
st.session_state.vector_created = True
|
| 108 |
st.success("β
Vector store initialized successfully!")
|
| 109 |
|
|
|
|
| 119 |
|
| 120 |
# ----------------- Full SequentialChain Execution -----------------
|
| 121 |
with st.spinner("π Running full pipeline..."):
|
| 122 |
+
final_output = SequentialChain(
|
| 123 |
+
chains=[
|
| 124 |
+
LLMChain(llm=llm_judge, prompt=PromptTemplate(input_variables=["retriever_query", "context"], template=relevancy_prompt), output_key="relevancy_response"),
|
| 125 |
+
LLMChain(llm=llm_judge, prompt=PromptTemplate(input_variables=["relevancy_response"], template=relevant_context_picker_prompt), output_key="context_number"),
|
| 126 |
+
LLMChain(llm=llm_judge, prompt=PromptTemplate(input_variables=["context_number", "context"], template=response_synth), output_key="relevant_contexts"),
|
| 127 |
+
LLMChain(llm=rag_llm, prompt=PromptTemplate(input_variables=["query", "context"], template=rag_prompt), output_key="final_response")
|
| 128 |
+
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
input_variables=["context", "retriever_query", "query"],
|
| 130 |
output_variables=["relevancy_response", "context_number", "relevant_contexts", "final_response"]
|
| 131 |
+
).invoke({"context": context, "retriever_query": query, "query": query})
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
+
# ----------------- Display All Outputs -----------------
|
| 134 |
+
st.subheader("π₯ Context Relevancy Evaluation")
|
| 135 |
st.json(final_output["relevancy_response"])
|
| 136 |
+
st.subheader("π¦ Picked Relevant Contexts")
|
|
|
|
| 137 |
st.json(final_output["context_number"])
|
| 138 |
+
st.subheader("π₯ Extracted Relevant Contexts")
|
|
|
|
| 139 |
st.json(final_output["relevant_contexts"])
|
| 140 |
+
st.subheader("π₯ RAG Final Response")
|
| 141 |
+
st.write(final_output["final_response"])
|
|
|