VishnuRamDebyez commited on
Commit
02ab853
·
verified ·
1 Parent(s): 60d6dcb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -9
app.py CHANGED
@@ -22,6 +22,8 @@ if 'chat_history' not in st.session_state:
22
  st.session_state.chat_history = []
23
  if 'last_response' not in st.session_state:
24
  st.session_state.last_response = None
 
 
25
 
26
  # Title
27
  st.title("Legal Assistant")
@@ -66,18 +68,15 @@ def add_to_chat_history(question, answer):
66
  'answer': answer
67
  })
68
 
69
- # Clear chat function
70
- def clear_chat():
71
- # Clear the last response
72
- st.session_state.last_response = None
73
- # Optional: You can also clear the specific key used for the text input
74
- st.session_state.prompt1 = ""
75
-
76
  # Main input area with clear button
77
  col1, col2 = st.columns([3, 1])
78
  with col1:
79
  # Use a key to manage the text input state
80
- prompt1 = st.text_input("Enter Your Question From Documents", key="prompt1")
 
 
 
 
81
 
82
  with col2:
83
  # Add clear chat button next to the input
@@ -86,12 +85,20 @@ with col2:
86
 
87
  # Clear chat functionality
88
  if clear_button:
89
- clear_chat()
 
 
90
  st.experimental_rerun()
91
 
 
 
 
92
  # Process question and generate response
93
  if prompt1:
94
  try:
 
 
 
95
  # Create document and retrieval chains
96
  document_chain = create_stuff_documents_chain(llm, prompt)
97
  retriever = st.session_state.vectors.as_retriever()
 
22
  st.session_state.chat_history = []
23
  if 'last_response' not in st.session_state:
24
  st.session_state.last_response = None
25
+ if 'clear_chat' not in st.session_state:
26
+ st.session_state.clear_chat = False
27
 
28
  # Title
29
  st.title("Legal Assistant")
 
68
  'answer': answer
69
  })
70
 
 
 
 
 
 
 
 
71
  # Main input area with clear button
72
  col1, col2 = st.columns([3, 1])
73
  with col1:
74
  # Use a key to manage the text input state
75
+ # If clear_chat is True, use an empty default value
76
+ default_text = "" if st.session_state.clear_chat else st.session_state.get('last_question', '')
77
+ prompt1 = st.text_input("Enter Your Question From Documents",
78
+ value=default_text,
79
+ key="user_question")
80
 
81
  with col2:
82
  # Add clear chat button next to the input
 
85
 
86
  # Clear chat functionality
87
  if clear_button:
88
+ # Reset flags and last response
89
+ st.session_state.clear_chat = True
90
+ st.session_state.last_response = None
91
  st.experimental_rerun()
92
 
93
+ # Reset clear_chat flag after processing
94
+ st.session_state.clear_chat = False
95
+
96
  # Process question and generate response
97
  if prompt1:
98
  try:
99
+ # Store the current question
100
+ st.session_state.last_question = prompt1
101
+
102
  # Create document and retrieval chains
103
  document_chain = create_stuff_documents_chain(llm, prompt)
104
  retriever = st.session_state.vectors.as_retriever()