barunsaha commited on
Commit
603bfd7
·
unverified ·
2 Parent(s): 18733f2 fe84593

Merge pull request #131 from AdiBak/main

Browse files
Files changed (2) hide show
  1. .gitignore +1 -1
  2. app.py +35 -6
.gitignore CHANGED
@@ -144,4 +144,4 @@ dmypy.json
144
  # Cython debug symbols
145
  cython_debug/
146
 
147
- .idea
 
144
  # Cython debug symbols
145
  cython_debug/
146
 
147
+ .idea.DS_Store
app.py CHANGED
@@ -134,6 +134,24 @@ def reset_api_key():
134
  st.session_state.api_key_input = ''
135
 
136
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
  APP_TEXT = _load_strings()
138
 
139
  # Session variables
@@ -141,6 +159,7 @@ CHAT_MESSAGES = 'chat_messages'
141
  DOWNLOAD_FILE_KEY = 'download_file_name'
142
  IS_IT_REFINEMENT = 'is_it_refinement'
143
  ADDITIONAL_INFO = 'additional_info'
 
144
 
145
 
146
  logger = logging.getLogger(__name__)
@@ -148,7 +167,14 @@ logger = logging.getLogger(__name__)
148
  texts = list(GlobalConfig.PPTX_TEMPLATE_FILES.keys())
149
  captions = [GlobalConfig.PPTX_TEMPLATE_FILES[x]['caption'] for x in texts]
150
 
 
151
  with st.sidebar:
 
 
 
 
 
 
152
  # The PPT templates
153
  pptx_template = st.sidebar.radio(
154
  '1: Select a presentation template:',
@@ -285,29 +311,32 @@ def set_up_chat_ui():
285
  for msg in history.messages:
286
  st.chat_message(msg.type).code(msg.content, language='json')
287
 
288
- if prompt := st.chat_input(
 
289
  placeholder=APP_TEXT['chat_placeholder'],
290
  max_chars=GlobalConfig.LLM_MODEL_MAX_INPUT_LENGTH,
291
  accept_file=True,
292
  file_type=['pdf', ],
293
- ):
 
 
294
  prompt_text = prompt.text or ''
295
  if prompt['files']:
296
  # Store uploaded pdf in session state
297
  uploaded_pdf = prompt['files'][0]
298
- st.session_state['pdf_file'] = uploaded_pdf
299
  # Apparently, Streamlit stores uploaded files in memory and clears on browser close
300
  # https://docs.streamlit.io/knowledge-base/using-streamlit/where-file-uploader-store-when-deleted
301
 
302
  # Check if pdf file is uploaded
303
  # (we can use the same file if the user doesn't upload a new one)
304
- if 'pdf_file' in st.session_state:
305
  # Get validated page range
306
  (
307
  st.session_state['start_page'],
308
  st.session_state['end_page']
309
  ) = filem.validate_page_range(
310
- st.session_state['pdf_file'],
311
  st.session_state['start_page'],
312
  st.session_state['end_page']
313
  )
@@ -326,7 +355,7 @@ def set_up_chat_ui():
326
 
327
  # Get pdf contents
328
  st.session_state[ADDITIONAL_INFO] = filem.get_pdf_contents(
329
- st.session_state['pdf_file'],
330
  (st.session_state['start_page'], st.session_state['end_page'])
331
  )
332
  provider, llm_name = llm_helper.get_provider_model(
 
134
  st.session_state.api_key_input = ''
135
 
136
 
137
+ def reset_chat_history():
138
+ """
139
+ Clear the chat history and related session state variables.
140
+ """
141
+ # Clear session state variables using pop with None default
142
+ st.session_state.pop(CHAT_MESSAGES, None)
143
+ st.session_state.pop(IS_IT_REFINEMENT, None)
144
+ st.session_state.pop(ADDITIONAL_INFO, None)
145
+ st.session_state.pop(PDF_FILE_KEY, None)
146
+
147
+ # Remove previously generated temp PPTX file
148
+ temp_pptx_path = st.session_state.pop(DOWNLOAD_FILE_KEY, None)
149
+ if temp_pptx_path:
150
+ pptx_path = pathlib.Path(temp_pptx_path)
151
+ if pptx_path.exists() and pptx_path.is_file():
152
+ pptx_path.unlink()
153
+
154
+
155
  APP_TEXT = _load_strings()
156
 
157
  # Session variables
 
159
  DOWNLOAD_FILE_KEY = 'download_file_name'
160
  IS_IT_REFINEMENT = 'is_it_refinement'
161
  ADDITIONAL_INFO = 'additional_info'
162
+ PDF_FILE_KEY = 'pdf_file'
163
 
164
 
165
  logger = logging.getLogger(__name__)
 
167
  texts = list(GlobalConfig.PPTX_TEMPLATE_FILES.keys())
168
  captions = [GlobalConfig.PPTX_TEMPLATE_FILES[x]['caption'] for x in texts]
169
 
170
+
171
  with st.sidebar:
172
+ # New Chat button at the top of sidebar
173
+ col1, col2, col3 = st.columns([.17, 0.8, .1])
174
+ with col2:
175
+ if st.button('New Chat 💬', help='Start a new conversation', key='new_chat_button'):
176
+ reset_chat_history() # Reset the chat history when the button is clicked
177
+
178
  # The PPT templates
179
  pptx_template = st.sidebar.radio(
180
  '1: Select a presentation template:',
 
311
  for msg in history.messages:
312
  st.chat_message(msg.type).code(msg.content, language='json')
313
 
314
+ # Chat input at the bottom
315
+ prompt = st.chat_input(
316
  placeholder=APP_TEXT['chat_placeholder'],
317
  max_chars=GlobalConfig.LLM_MODEL_MAX_INPUT_LENGTH,
318
  accept_file=True,
319
  file_type=['pdf', ],
320
+ )
321
+
322
+ if prompt:
323
  prompt_text = prompt.text or ''
324
  if prompt['files']:
325
  # Store uploaded pdf in session state
326
  uploaded_pdf = prompt['files'][0]
327
+ st.session_state[PDF_FILE_KEY] = uploaded_pdf
328
  # Apparently, Streamlit stores uploaded files in memory and clears on browser close
329
  # https://docs.streamlit.io/knowledge-base/using-streamlit/where-file-uploader-store-when-deleted
330
 
331
  # Check if pdf file is uploaded
332
  # (we can use the same file if the user doesn't upload a new one)
333
+ if PDF_FILE_KEY in st.session_state:
334
  # Get validated page range
335
  (
336
  st.session_state['start_page'],
337
  st.session_state['end_page']
338
  ) = filem.validate_page_range(
339
+ st.session_state[PDF_FILE_KEY],
340
  st.session_state['start_page'],
341
  st.session_state['end_page']
342
  )
 
355
 
356
  # Get pdf contents
357
  st.session_state[ADDITIONAL_INFO] = filem.get_pdf_contents(
358
+ st.session_state[PDF_FILE_KEY],
359
  (st.session_state['start_page'], st.session_state['end_page'])
360
  )
361
  provider, llm_name = llm_helper.get_provider_model(