MusaR commited on
Commit
0ab9683
·
verified ·
1 Parent(s): b2967c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -30
app.py CHANGED
@@ -1,6 +1,3 @@
1
- %%writefile app.py
2
-
3
- print("--- Python script starting ---")
4
  import streamlit as st
5
  import os
6
  os.environ['TOKENIZERS_PARALLELISM'] = 'false'
@@ -11,7 +8,6 @@ if not os.path.exists('/app/huggingface_cache'):
11
  os.makedirs('/app/huggingface_cache', exist_ok=True)
12
 
13
  import langchain
14
- langchain.debug = False # Keep this off for production speed
15
 
16
  from dotenv import load_dotenv
17
  from pinecone import Pinecone
@@ -25,7 +21,6 @@ from langchain_core.output_parsers import StrOutputParser
25
  from langchain.retrievers import ContextualCompressionRetriever
26
  from langchain.retrievers.document_compressors import CohereRerank
27
 
28
- print("--- All imports successful ---")
29
 
30
  try:
31
  print("Step 1: Loading environment variables...")
@@ -37,9 +32,8 @@ try:
37
  print("Step 1: SUCCESS")
38
 
39
  st.set_page_config(page_title="Advanced RAG Chatbot", page_icon="🚀", layout="wide")
40
- # Remove the st.title here, we can let the chat UI be the main focus or add it inside the main area
41
 
42
- # --- Custom CSS for Chat Bubbles (FROM YOUR OFFLINE APP) ---
43
  st.markdown("""
44
  <style>
45
  .chat-container {
@@ -96,13 +90,13 @@ try:
96
 
97
  @st.cache_resource
98
  def initialize_services():
99
- # (Same as your previous working initialize_services function)
100
  print("Step 2: Entering initialize_services function...")
101
  if not all([PINECONE_API_KEY, GROQ_API_KEY, COHERE_API_KEY]):
102
  raise ValueError("An API key is missing!")
103
  embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
104
  pinecone = Pinecone(api_key=PINECONE_API_KEY)
105
- host = "https://rag-chatbot-sg8t88c.svc.aped-4627-b74a.pinecone.io" # Your host
106
  index = pinecone.Index(host=host)
107
  vectorstore = PineconeVectorStore(index=index, embedding=embeddings)
108
  base_retriever = vectorstore.as_retriever(search_kwargs={'k': 10})
@@ -116,7 +110,7 @@ try:
116
  retriever, llm = initialize_services()
117
  print("Step 3: SUCCESS, services are loaded.")
118
 
119
- # --- RAG CHAIN (modified for streaming if possible, and simpler output) ---
120
  print("Step 4: Defining RAG chain...")
121
  system_prompt = """You are a helpful AI assistant that answers questions based ONLY on the provided context.
122
  Your answer should be concise and directly address the question.
@@ -149,14 +143,13 @@ try:
149
  )
150
  print("Step 4: SUCCESS")
151
 
152
- # --- Streamlit Chat UI (ADAPTED FROM YOUR OFFLINE APP) ---
153
- st.title("💬 Document Chatbot Interface") # You can add a title here
154
 
155
  if "messages" not in st.session_state:
156
  st.session_state.messages = [{"role": "assistant", "content": "Hello! I'm ready to answer questions about your documents.", "sources": []}]
157
 
158
- # Display chat messages using your custom HTML
159
- # Wrap messages in a container for better layout control if needed
160
  st.markdown('<div class="chat-container">', unsafe_allow_html=True)
161
  for message in st.session_state.messages:
162
  if message["role"] == "user":
@@ -185,7 +178,7 @@ try:
185
  try:
186
  print(f"--- UI DEBUG: Invoking RAG chain with query: {user_query} ---")
187
 
188
- # For streaming with StrOutputParser, we iterate over chunks
189
  assistant_response_content = ""
190
  for chunk in rag_chain.stream(user_query):
191
  assistant_response_content += chunk
@@ -194,7 +187,7 @@ try:
194
  message_placeholder.markdown(f'<div class="chat-bubble bot-bubble">{assistant_response_content}</div>', unsafe_allow_html=True) # Final response
195
  print(f"--- UI DEBUG: Full LLM Answer: {assistant_response_content} ---")
196
 
197
- # Retrieve sources separately for display (as before)
198
  retrieved_docs_for_display = retriever.invoke(user_query)
199
  sources_info_for_display = []
200
  if retrieved_docs_for_display:
@@ -205,17 +198,14 @@ try:
205
  "content_snippet": doc.page_content
206
  })
207
 
208
- # Add assistant's full response and sources to session state
209
  st.session_state.messages.append({
210
  "role": "assistant",
211
  "content": assistant_response_content,
212
- "sources": sources_info_for_display # Store source info with the message
213
  })
214
 
215
- # Display sources in an expander (this will appear below the last message outside the chat_message context)
216
- # This part needs to be rethought slightly for a clean UI if sources are tied to each message
217
- # For now, let's keep the separate expander logic for the last response.
218
- # The main display loop will handle showing sources for historical messages.
219
  if sources_info_for_display:
220
  with st.expander("Sources for the latest answer"):
221
  for i, doc_info in enumerate(sources_info_for_display):
@@ -223,8 +213,7 @@ try:
223
  st.markdown(f"> {doc_info['content_snippet'][:300]}...")
224
  st.markdown("---")
225
 
226
- # A slight hack to force rerun to update the main message display loop with the new message + sources
227
- # st.experimental_rerun() # Not always needed if placeholders work well
228
 
229
  except Exception as e_invoke:
230
  error_message = f"Error processing your query: {e_invoke}"
@@ -235,9 +224,3 @@ try:
235
  st.session_state.messages.append({"role": "assistant", "content": f"Sorry, I encountered an error: {error_message}", "sources": []})
236
 
237
  print("--- app.py script finished a run ---")
238
-
239
- except Exception as e:
240
- print(f"!!!!!!!!!! A FATAL ERROR OCCURRED DURING STARTUP !!!!!!!!!!")
241
- import traceback
242
- print(traceback.format_exc())
243
- st.error(f"A fatal error occurred during startup. Please check the container logs. Error: {e}")
 
 
 
 
1
  import streamlit as st
2
  import os
3
  os.environ['TOKENIZERS_PARALLELISM'] = 'false'
 
8
  os.makedirs('/app/huggingface_cache', exist_ok=True)
9
 
10
  import langchain
 
11
 
12
  from dotenv import load_dotenv
13
  from pinecone import Pinecone
 
21
  from langchain.retrievers import ContextualCompressionRetriever
22
  from langchain.retrievers.document_compressors import CohereRerank
23
 
 
24
 
25
  try:
26
  print("Step 1: Loading environment variables...")
 
32
  print("Step 1: SUCCESS")
33
 
34
  st.set_page_config(page_title="Advanced RAG Chatbot", page_icon="🚀", layout="wide")
35
+
36
 
 
37
  st.markdown("""
38
  <style>
39
  .chat-container {
 
90
 
91
  @st.cache_resource
92
  def initialize_services():
93
+
94
  print("Step 2: Entering initialize_services function...")
95
  if not all([PINECONE_API_KEY, GROQ_API_KEY, COHERE_API_KEY]):
96
  raise ValueError("An API key is missing!")
97
  embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
98
  pinecone = Pinecone(api_key=PINECONE_API_KEY)
99
+ host = "https://rag-chatbot-sg8t88c.svc.aped-4627-b74a.pinecone.io"
100
  index = pinecone.Index(host=host)
101
  vectorstore = PineconeVectorStore(index=index, embedding=embeddings)
102
  base_retriever = vectorstore.as_retriever(search_kwargs={'k': 10})
 
110
  retriever, llm = initialize_services()
111
  print("Step 3: SUCCESS, services are loaded.")
112
 
113
+ # --- RAG CHAIN
114
  print("Step 4: Defining RAG chain...")
115
  system_prompt = """You are a helpful AI assistant that answers questions based ONLY on the provided context.
116
  Your answer should be concise and directly address the question.
 
143
  )
144
  print("Step 4: SUCCESS")
145
 
146
+ # --- Streamlit Chat UI
147
+ st.title("💬 Document Chatbot Interface")
148
 
149
  if "messages" not in st.session_state:
150
  st.session_state.messages = [{"role": "assistant", "content": "Hello! I'm ready to answer questions about your documents.", "sources": []}]
151
 
152
+
 
153
  st.markdown('<div class="chat-container">', unsafe_allow_html=True)
154
  for message in st.session_state.messages:
155
  if message["role"] == "user":
 
178
  try:
179
  print(f"--- UI DEBUG: Invoking RAG chain with query: {user_query} ---")
180
 
181
+
182
  assistant_response_content = ""
183
  for chunk in rag_chain.stream(user_query):
184
  assistant_response_content += chunk
 
187
  message_placeholder.markdown(f'<div class="chat-bubble bot-bubble">{assistant_response_content}</div>', unsafe_allow_html=True) # Final response
188
  print(f"--- UI DEBUG: Full LLM Answer: {assistant_response_content} ---")
189
 
190
+
191
  retrieved_docs_for_display = retriever.invoke(user_query)
192
  sources_info_for_display = []
193
  if retrieved_docs_for_display:
 
198
  "content_snippet": doc.page_content
199
  })
200
 
201
+
202
  st.session_state.messages.append({
203
  "role": "assistant",
204
  "content": assistant_response_content,
205
+ "sources": sources_info_for_display
206
  })
207
 
208
+
 
 
 
209
  if sources_info_for_display:
210
  with st.expander("Sources for the latest answer"):
211
  for i, doc_info in enumerate(sources_info_for_display):
 
213
  st.markdown(f"> {doc_info['content_snippet'][:300]}...")
214
  st.markdown("---")
215
 
216
+
 
217
 
218
  except Exception as e_invoke:
219
  error_message = f"Error processing your query: {e_invoke}"
 
224
  st.session_state.messages.append({"role": "assistant", "content": f"Sorry, I encountered an error: {error_message}", "sources": []})
225
 
226
  print("--- app.py script finished a run ---")