euracle commited on
Commit
cb23120
Β·
verified Β·
1 Parent(s): 21f32e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -22
app.py CHANGED
@@ -205,6 +205,9 @@ if "conversation_memory" not in st.session_state:
205
  memory_key="chat_history",
206
  return_messages=True
207
  )
 
 
 
208
  start_time = time.time()
209
  if st.session_state["documents"] is None or st.session_state["vector_db"] is None:
210
  with st.spinner("Loading data..."):
@@ -217,27 +220,18 @@ else:
217
 
218
  retriever = vector_db.as_retriever()
219
  with st.sidebar:
220
- st.title("Conversation History")
221
 
222
- if st.button("New Session πŸ”„"):
223
- # Clear all conversation related session states
224
- st.session_state.messages = []
225
- st.session_state.messages.append({
226
- "role": "assistant",
227
- "content": "πŸ‘‹ Hello! I'm Dr. Radha, your AI-powered Organic Farming Consultant. How can I assist you today?"
228
- })
229
- st.session_state.conversation_memory.clear()
230
- st.session_state.session_id = str(uuid.uuid4())
231
- st.rerun()
232
- # Display conversation history
233
- st.subheader("Previous Conversations")
234
- for message in st.session_state.messages[1:]: # Skip the initial greeting
235
- if message["role"] == "user":
236
- st.text("You:")
237
- st.text_area("", message["content"], height=70, disabled=True, key=f"hist_{uuid.uuid4()}")
238
- else:
239
- st.text("Dr. Radha:")
240
- st.text_area("", message["content"], height=100, disabled=True, key=f"hist_{uuid.uuid4()}")
241
 
242
  prompt_template = """As an expert organic farming consultant with specialization in Agro-Homeopathy, analyze the following context and question to provide a clear, structured response.
243
  Context: {context}
@@ -348,8 +342,25 @@ with st.form(key='query_form', clear_on_submit=True):
348
  placeholder="Describe your plant issue here...",
349
  label_visibility="collapsed"
350
  )
351
- submit_button = st.form_submit_button(label='Submit πŸ“€')
352
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
353
  human_image = "human.png"
354
  robot_image = "bot.jpg"
355
 
 
205
  memory_key="chat_history",
206
  return_messages=True
207
  )
208
+ if "saved_conversations" not in st.session_state:
209
+ st.session_state.saved_conversations = []
210
+
211
  start_time = time.time()
212
  if st.session_state["documents"] is None or st.session_state["vector_db"] is None:
213
  with st.spinner("Loading data..."):
 
220
 
221
  retriever = vector_db.as_retriever()
222
  with st.sidebar:
223
+ st.title("Past Conversations")
224
 
225
+ # Display saved conversations
226
+ for idx, conv in enumerate(st.session_state.saved_conversations):
227
+ # Get the first message from user in the conversation
228
+ first_user_msg = next((msg["content"] for msg in conv if msg["role"] == "user"), "")
229
+ # Take first 30 characters of the message
230
+ preview = first_user_msg[:30] + "..." if len(first_user_msg) > 30 else first_user_msg
231
+
232
+ if st.button(f"Conversation {idx + 1}: {preview}", key=f"conv_{idx}"):
233
+ st.session_state.messages = conv.copy()
234
+ st.rerun()
 
 
 
 
 
 
 
 
 
235
 
236
  prompt_template = """As an expert organic farming consultant with specialization in Agro-Homeopathy, analyze the following context and question to provide a clear, structured response.
237
  Context: {context}
 
342
  placeholder="Describe your plant issue here...",
343
  label_visibility="collapsed"
344
  )
345
+ col1, col2 = st.columns([1, 1])
346
+ with col1:
347
+ submit_button = st.form_submit_button(label='Submit πŸ“€')
348
+ with col2:
349
+ new_conv_button = st.form_submit_button(label='New Conversation πŸ”„')
350
+
351
+ if new_conv_button and len(st.session_state.messages) > 1:
352
+ # Save current conversation
353
+ st.session_state.saved_conversations.append(st.session_state.messages.copy())
354
+
355
+ # Clear current conversation
356
+ st.session_state.messages = []
357
+ st.session_state.messages.append({
358
+ "role": "assistant",
359
+ "content": "πŸ‘‹ Hello! I'm Dr. Radha, your AI-powered Organic Farming Consultant. How can I assist you today?"
360
+ })
361
+ st.session_state.conversation_memory.clear()
362
+ st.rerun()
363
+
364
  human_image = "human.png"
365
  robot_image = "bot.jpg"
366