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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -43
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
- # 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")
@@ -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
- # 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)
@@ -119,21 +109,21 @@ def main():
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
 
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