Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -17,9 +17,11 @@ 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
|
21 |
if 'chat_history' not in st.session_state:
|
22 |
st.session_state.chat_history = []
|
|
|
|
|
23 |
|
24 |
# Title
|
25 |
st.title("Legal Assistant")
|
@@ -57,9 +59,6 @@ def vector_embedding():
|
|
57 |
# Perform vector embedding
|
58 |
vector_embedding()
|
59 |
|
60 |
-
# Main input area
|
61 |
-
prompt1 = st.text_input("Enter Your Question From Documents")
|
62 |
-
|
63 |
# Function to add to chat history
|
64 |
def add_to_chat_history(question, answer):
|
65 |
st.session_state.chat_history.append({
|
@@ -67,6 +66,29 @@ def add_to_chat_history(question, answer):
|
|
67 |
'answer': answer
|
68 |
})
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
# Process question and generate response
|
71 |
if prompt1:
|
72 |
try:
|
@@ -80,8 +102,8 @@ if prompt1:
|
|
80 |
response = retrieval_chain.invoke({'input': prompt1})
|
81 |
response_time = time.process_time() - start
|
82 |
|
83 |
-
#
|
84 |
-
st.
|
85 |
|
86 |
# Add to chat history
|
87 |
add_to_chat_history(prompt1, response['answer'])
|
@@ -89,6 +111,10 @@ if prompt1:
|
|
89 |
except Exception as e:
|
90 |
st.error(f"An error occurred: {e}")
|
91 |
|
|
|
|
|
|
|
|
|
92 |
# Sidebar content
|
93 |
# Clear chat history button
|
94 |
if st.sidebar.button("Clear Chat History"):
|
|
|
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 |
|
26 |
# Title
|
27 |
st.title("Legal Assistant")
|
|
|
59 |
# Perform vector embedding
|
60 |
vector_embedding()
|
61 |
|
|
|
|
|
|
|
62 |
# Function to add to chat history
|
63 |
def add_to_chat_history(question, answer):
|
64 |
st.session_state.chat_history.append({
|
|
|
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
|
84 |
+
st.text("") # Add some vertical space to align with input
|
85 |
+
clear_button = st.button("Clear Chat")
|
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:
|
|
|
102 |
response = retrieval_chain.invoke({'input': prompt1})
|
103 |
response_time = time.process_time() - start
|
104 |
|
105 |
+
# Store and display response
|
106 |
+
st.session_state.last_response = response['answer']
|
107 |
|
108 |
# Add to chat history
|
109 |
add_to_chat_history(prompt1, response['answer'])
|
|
|
111 |
except Exception as e:
|
112 |
st.error(f"An error occurred: {e}")
|
113 |
|
114 |
+
# Display the last response if exists
|
115 |
+
if st.session_state.last_response:
|
116 |
+
st.write(st.session_state.last_response)
|
117 |
+
|
118 |
# Sidebar content
|
119 |
# Clear chat history button
|
120 |
if st.sidebar.button("Clear Chat History"):
|