VishnuRamDebyez commited on
Commit
09ed89b
·
verified ·
1 Parent(s): 3f58e3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -64
app.py CHANGED
@@ -17,13 +17,27 @@ load_dotenv()
17
  # Set page configuration
18
  st.set_page_config(page_title="Legal Assistant", layout="wide")
19
 
20
- # Initialize session state for chat history and response
21
- 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
- if 'last_question' not in st.session_state:
26
- st.session_state.last_question = ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  # Title
29
  st.title("Legal Assistant")
@@ -68,63 +82,71 @@ def add_to_chat_history(question, answer):
68
  'answer': answer
69
  })
70
 
71
- # Main input area with clear button
72
- col1, col2 = st.columns([3, 1])
73
- with col1:
74
- # Text input with dynamic value management
75
- prompt1 = st.text_input("Enter Your Question From Documents",
76
- value=st.session_state.last_question,
77
- key="user_question")
78
-
79
- with col2:
80
- # Add clear chat button next to the input
81
- st.text("") # Add some vertical space to align with input
82
  clear_button = st.button("Clear Chat")
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
- # Clear chat functionality
85
- if clear_button:
86
- # Reset last question and response
87
- st.session_state.last_question = ""
88
- st.session_state.last_response = None
89
-
90
- # Process question and generate response
91
- if prompt1:
92
- try:
93
- # Store the current question
94
- st.session_state.last_question = prompt1
95
-
96
- # Create document and retrieval chains
97
- document_chain = create_stuff_documents_chain(llm, prompt)
98
- retriever = st.session_state.vectors.as_retriever()
99
- retrieval_chain = create_retrieval_chain(retriever, document_chain)
100
-
101
- # Generate response
102
- start = time.process_time()
103
- response = retrieval_chain.invoke({'input': prompt1})
104
- response_time = time.process_time() - start
105
-
106
- # Store and display response
107
- st.session_state.last_response = response['answer']
 
 
 
 
108
 
109
- # Add to chat history
110
- add_to_chat_history(prompt1, response['answer'])
111
-
112
- except Exception as e:
113
- st.error(f"An error occurred: {e}")
114
-
115
- # Display the last response if exists
116
- if st.session_state.last_response:
117
- st.write(st.session_state.last_response)
118
-
119
- # Sidebar content
120
- # Clear chat history button
121
- if st.sidebar.button("Clear Chat History"):
122
- st.session_state.chat_history = []
123
-
124
- # Display chat history
125
- st.sidebar.write("### Previous Questions")
126
- for idx, chat in enumerate(reversed(st.session_state.chat_history), 1):
127
- # Expander for each chat history item
128
- with st.sidebar.expander(f"Question {len(st.session_state.chat_history) - idx + 1}"):
129
- st.write(f"**Question:** {chat['question']}")
130
- st.write(f"**Answer:** {chat['answer']}")
 
17
  # Set page configuration
18
  st.set_page_config(page_title="Legal Assistant", layout="wide")
19
 
20
+ # Initialize session state
21
+ def init_session_state():
22
+ # Initialize session state variables if they don't exist
23
+ session_vars = [
24
+ 'chat_history',
25
+ 'last_response',
26
+ 'current_question',
27
+ 'should_clear'
28
+ ]
29
+
30
+ for var in session_vars:
31
+ if var not in st.session_state:
32
+ if var == 'chat_history':
33
+ st.session_state[var] = []
34
+ elif var == 'should_clear':
35
+ st.session_state[var] = False
36
+ else:
37
+ st.session_state[var] = None
38
+
39
+ # Call initialization
40
+ init_session_state()
41
 
42
  # Title
43
  st.title("Legal Assistant")
 
82
  'answer': answer
83
  })
84
 
85
+ # Main content
86
+ def main():
87
+ # Clear chat button
 
 
 
 
 
 
 
 
88
  clear_button = st.button("Clear Chat")
89
+
90
+ # Handle clear chat
91
+ if clear_button:
92
+ # Set clear flag
93
+ st.session_state.should_clear = True
94
+ # Reset current question and last response
95
+ st.session_state.current_question = ""
96
+ st.session_state.last_response = None
97
+
98
+ # Reset clear flag after processing
99
+ if st.session_state.should_clear:
100
+ st.session_state.should_clear = False
101
 
102
+ # Text input with dynamic value management
103
+ current_question = st.text_input(
104
+ "Enter Your Question From Documents",
105
+ value="" if st.session_state.should_clear else st.session_state.current_question,
106
+ key="question_input"
107
+ )
108
+
109
+ # Process question if exists
110
+ if current_question:
111
+ try:
112
+ # Store current question
113
+ st.session_state.current_question = current_question
114
+
115
+ # Create document and retrieval chains
116
+ document_chain = create_stuff_documents_chain(llm, prompt)
117
+ retriever = st.session_state.vectors.as_retriever()
118
+ retrieval_chain = create_retrieval_chain(retriever, document_chain)
119
+
120
+ # Generate response
121
+ start = time.process_time()
122
+ response = retrieval_chain.invoke({'input': current_question})
123
+ response_time = time.process_time() - start
124
+
125
+ # Store and display response
126
+ st.session_state.last_response = response['answer']
127
+
128
+ # Add to chat history
129
+ add_to_chat_history(current_question, response['answer'])
130
 
131
+ except Exception as e:
132
+ st.error(f"An error occurred: {e}")
133
+
134
+ # Display the last response if exists
135
+ if st.session_state.last_response:
136
+ st.write(st.session_state.last_response)
137
+
138
+ # Sidebar content
139
+ # Clear chat history button
140
+ if st.sidebar.button("Clear Chat History"):
141
+ st.session_state.chat_history = []
142
+
143
+ # Display chat history
144
+ st.sidebar.write("### Previous Questions")
145
+ for idx, chat in enumerate(reversed(st.session_state.chat_history), 1):
146
+ # Expander for each chat history item
147
+ with st.sidebar.expander(f"Question {len(st.session_state.chat_history) - idx + 1}"):
148
+ st.write(f"**Question:** {chat['question']}")
149
+ st.write(f"**Answer:** {chat['answer']}")
150
+
151
+ # Run the main function
152
+ main()