DreamStream-1 commited on
Commit
209ed30
·
verified ·
1 Parent(s): 7dd5b9d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -27
app.py CHANGED
@@ -243,34 +243,41 @@ class AdvancedRAG:
243
  self.thread_id = None
244
  self.file_ids = []
245
  if hasattr(self, 'vector_store_id'):
 
 
 
 
246
  self.vector_store_id = None
247
- try:
248
- # file is a bytes object, so assign a default name and extension
249
- if not file:
250
- raise Exception("No file uploaded.")
251
- filename = 'uploaded_file.pdf' # Default extension, or infer from UI if needed
252
- with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(filename)[1]) as tmp:
253
- tmp.write(file)
254
- tmp.flush()
255
- with open(tmp.name, "rb") as file_obj:
256
- file_obj = openai.files.create(
257
- file=file_obj,
258
- purpose="assistants"
259
- )
260
- self.file_ids = [file_obj.id] # Only the new file
261
- # Create a new thread for the new document
262
- thread = openai.beta.threads.create()
263
- self.thread_id = thread.id
264
- # Send a message in the new thread with only the new file as an attachment
265
- openai.beta.threads.messages.create(
266
- thread_id=self.thread_id,
267
- role="user",
268
- content="I have uploaded a document. Please analyze it.",
269
- attachments=[{"file_id": self.file_ids[0], "tools": [{"type": "file_search"}]}]
270
- )
271
- return self.file_ids[0]
272
- except Exception as e:
273
- raise Exception(f"Error uploading document: {str(e)}")
 
 
 
274
 
275
  def ask_question(self, question: str) -> str:
276
  try:
 
243
  self.thread_id = None
244
  self.file_ids = []
245
  if hasattr(self, 'vector_store_id'):
246
+ try:
247
+ openai.beta.vector_stores.delete(self.vector_store_id)
248
+ except Exception as e:
249
+ print(f"Warning: Could not delete vector store: {e}")
250
  self.vector_store_id = None
251
+
252
+ # Wait a moment to ensure deletion is processed
253
+ time.sleep(2)
254
+
255
+ # Upload new file
256
+ if not file:
257
+ raise Exception("No file uploaded.")
258
+ filename = 'uploaded_file.pdf'
259
+ with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(filename)[1]) as tmp:
260
+ tmp.write(file)
261
+ tmp.flush()
262
+ with open(tmp.name, "rb") as file_obj:
263
+ file_obj = openai.files.create(
264
+ file=file_obj,
265
+ purpose="assistants"
266
+ )
267
+ self.file_ids = [file_obj.id]
268
+
269
+ # Create a new thread for the new document
270
+ thread = openai.beta.threads.create()
271
+ self.thread_id = thread.id
272
+
273
+ # Send a message in the new thread with only the new file as an attachment
274
+ openai.beta.threads.messages.create(
275
+ thread_id=self.thread_id,
276
+ role="user",
277
+ content="I have uploaded a document. Please analyze it.",
278
+ attachments=[{"file_id": self.file_ids[0], "tools": [{"type": "file_search"}]}]
279
+ )
280
+ return self.file_ids[0]
281
 
282
  def ask_question(self, question: str) -> str:
283
  try: