Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
72 |
-
|
73 |
-
|
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 |
-
#
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
if
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
|
|
|
|
|
|
|
|
108 |
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
st.
|
123 |
-
|
124 |
-
#
|
125 |
-
st.sidebar.
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
|
|
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()
|