Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
|
|
|
|
|
|
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:
|