Spaces:
Sleeping
Sleeping
VishnuRamDebyez
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -17,27 +17,18 @@ load_dotenv()
|
|
17 |
# Set page configuration
|
18 |
st.set_page_config(page_title="Legal Assistant", layout="wide")
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
]
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
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")
|
@@ -49,6 +40,10 @@ st.sidebar.title("Chat History")
|
|
49 |
groq_api_key = os.getenv('groqapi')
|
50 |
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
|
51 |
|
|
|
|
|
|
|
|
|
52 |
# LLM and Prompt Setup
|
53 |
llm = ChatGroq(groq_api_key=groq_api_key, model_name="Llama3-8b-8192")
|
54 |
prompt = ChatPromptTemplate.from_template(
|
@@ -82,35 +77,30 @@ def add_to_chat_history(question, answer):
|
|
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 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
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 |
-
|
106 |
-
|
107 |
)
|
108 |
|
109 |
# Process question if exists
|
110 |
-
if
|
111 |
try:
|
112 |
# Store current question
|
113 |
-
st.session_state
|
114 |
|
115 |
# Create document and retrieval chains
|
116 |
document_chain = create_stuff_documents_chain(llm, prompt)
|
@@ -119,21 +109,21 @@ def main():
|
|
119 |
|
120 |
# Generate response
|
121 |
start = time.process_time()
|
122 |
-
response = retrieval_chain.invoke({'input':
|
123 |
response_time = time.process_time() - start
|
124 |
|
125 |
# Store and display response
|
126 |
-
st.session_state
|
127 |
|
128 |
# Add to chat history
|
129 |
-
add_to_chat_history(
|
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
|
137 |
|
138 |
# Sidebar content
|
139 |
# Clear chat history button
|
|
|
17 |
# Set page configuration
|
18 |
st.set_page_config(page_title="Legal Assistant", layout="wide")
|
19 |
|
20 |
+
# Create a unique key for the session state to help with resetting
|
21 |
+
if 'reset_key' not in st.session_state:
|
22 |
+
st.session_state['reset_key'] = 0
|
23 |
+
|
24 |
+
# Function to reset the entire session state
|
25 |
+
def reset_session_state():
|
26 |
+
# Increment reset key to force a complete reset
|
27 |
+
st.session_state['reset_key'] += 1
|
|
|
28 |
|
29 |
+
# Reset specific session state variables
|
30 |
+
st.session_state['last_response'] = None
|
31 |
+
st.session_state['current_question'] = ''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
# Title
|
34 |
st.title("Legal Assistant")
|
|
|
40 |
groq_api_key = os.getenv('groqapi')
|
41 |
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
|
42 |
|
43 |
+
# Initialize chat history if not exists
|
44 |
+
if 'chat_history' not in st.session_state:
|
45 |
+
st.session_state['chat_history'] = []
|
46 |
+
|
47 |
# LLM and Prompt Setup
|
48 |
llm = ChatGroq(groq_api_key=groq_api_key, model_name="Llama3-8b-8192")
|
49 |
prompt = ChatPromptTemplate.from_template(
|
|
|
77 |
'answer': answer
|
78 |
})
|
79 |
|
80 |
+
# Main content area
|
81 |
def main():
|
82 |
# Clear chat button
|
83 |
clear_button = st.button("Clear Chat")
|
84 |
|
85 |
+
# Handle clear chat functionality
|
86 |
if clear_button:
|
87 |
+
reset_session_state()
|
88 |
+
|
89 |
+
# Create a unique key for the text input to force reset
|
90 |
+
text_input_key = f'question_input_{st.session_state["reset_key"]}'
|
91 |
+
|
92 |
+
# Text input with reset mechanism
|
93 |
+
prompt1 = st.text_input(
|
|
|
|
|
|
|
|
|
|
|
94 |
"Enter Your Question From Documents",
|
95 |
+
key=text_input_key,
|
96 |
+
value=st.session_state.get('current_question', '')
|
97 |
)
|
98 |
|
99 |
# Process question if exists
|
100 |
+
if prompt1:
|
101 |
try:
|
102 |
# Store current question
|
103 |
+
st.session_state['current_question'] = prompt1
|
104 |
|
105 |
# Create document and retrieval chains
|
106 |
document_chain = create_stuff_documents_chain(llm, prompt)
|
|
|
109 |
|
110 |
# Generate response
|
111 |
start = time.process_time()
|
112 |
+
response = retrieval_chain.invoke({'input': prompt1})
|
113 |
response_time = time.process_time() - start
|
114 |
|
115 |
# Store and display response
|
116 |
+
st.session_state['last_response'] = response['answer']
|
117 |
|
118 |
# Add to chat history
|
119 |
+
add_to_chat_history(prompt1, response['answer'])
|
120 |
|
121 |
except Exception as e:
|
122 |
st.error(f"An error occurred: {e}")
|
123 |
|
124 |
# Display the last response if exists
|
125 |
+
if st.session_state.get('last_response'):
|
126 |
+
st.write(st.session_state['last_response'])
|
127 |
|
128 |
# Sidebar content
|
129 |
# Clear chat history button
|