akash015 commited on
Commit
c09dbf0
·
verified ·
1 Parent(s): 4002837

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -1
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import re
2
  import PyPDF2
3
  from langchain_community.embeddings import OllamaEmbeddings
@@ -102,13 +103,17 @@ async def on_chat_start():
102
  while files is None:
103
  files = await cl.AskFileMessage(
104
  content="Please upload a pdf file to begin!",
105
- # accept=["application/pdf"],
106
  accept=["application/pdf", "image/jpeg", "image/png", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"],
107
  max_size_mb=100,
108
  timeout=180,
109
  ).send()
110
 
111
  file = files[0] # Get the first uploaded file
 
 
 
 
 
112
 
113
  # Inform the user that processing has started
114
  msg = cl.Message(content=f"Processing `{file.name}`...")
@@ -179,6 +184,17 @@ async def on_chat_start():
179
  # Store the chain in user session
180
  cl.user_session.set("chain", chain)
181
 
 
 
 
 
 
 
 
 
 
 
 
182
 
183
  @cl.on_message
184
  async def main(message: cl.Message):
 
1
+ import os
2
  import re
3
  import PyPDF2
4
  from langchain_community.embeddings import OllamaEmbeddings
 
103
  while files is None:
104
  files = await cl.AskFileMessage(
105
  content="Please upload a pdf file to begin!",
 
106
  accept=["application/pdf", "image/jpeg", "image/png", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"],
107
  max_size_mb=100,
108
  timeout=180,
109
  ).send()
110
 
111
  file = files[0] # Get the first uploaded file
112
+
113
+ # Store file path in user session for later cleanup
114
+ uploaded_files = cl.user_session.get("uploaded_files", [])
115
+ uploaded_files.append(file.path)
116
+ cl.user_session.set("uploaded_files", uploaded_files)
117
 
118
  # Inform the user that processing has started
119
  msg = cl.Message(content=f"Processing `{file.name}`...")
 
184
  # Store the chain in user session
185
  cl.user_session.set("chain", chain)
186
 
187
+ @cl.on_session_end
188
+ async def on_session_end():
189
+ # Get the uploaded files from the user session
190
+ uploaded_files = cl.user_session.get("uploaded_files", [])
191
+ # Delete each file
192
+ for file_path in uploaded_files:
193
+ if os.path.exists(file_path):
194
+ os.remove(file_path)
195
+ logging.info(f"Deleted file: {file_path}")
196
+ # Clear the uploaded files list
197
+ cl.user_session.set("uploaded_files", [])
198
 
199
  @cl.on_message
200
  async def main(message: cl.Message):