DreamStream-1 commited on
Commit
8fbb8bd
·
verified ·
1 Parent(s): 3150019

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -33
app.py CHANGED
@@ -222,9 +222,10 @@ def create_audio_recorder():
222
  class AdvancedRAG:
223
  def __init__(self):
224
  self.thread_id: Optional[str] = None
 
225
  self.assistant_id: Optional[str] = os.getenv("ASSISTANT_ID")
226
- if not self.assistant_id:
227
- raise ValueError("ASSISTANT_ID environment variable is not set")
228
 
229
  def create_thread(self) -> str:
230
  thread = openai.beta.threads.create()
@@ -232,49 +233,35 @@ class AdvancedRAG:
232
  return self.thread_id
233
 
234
  def upload_document(self, file) -> str:
 
 
 
 
 
235
  try:
236
- if file is None:
237
- raise ValueError("No file provided")
238
-
239
- # The file is now a bytes object directly
240
- file_bytes = file
241
- if not file_bytes:
242
- raise ValueError("Empty file")
243
-
244
- # Create a temporary file with a generic extension
245
- with tempfile.NamedTemporaryFile(delete=False, suffix=".txt") as tmp:
246
- tmp.write(file_bytes)
247
  tmp.flush()
248
-
249
- # Upload the file to OpenAI
250
  with open(tmp.name, "rb") as file_obj:
251
  file_obj = openai.files.create(
252
  file=file_obj,
253
  purpose="assistants"
254
  )
255
-
256
- # Create a new thread if one doesn't exist
257
- if not self.thread_id:
258
- self.create_thread()
259
-
260
- # Add the file to the thread with the required tools object
261
  openai.beta.threads.messages.create(
262
  thread_id=self.thread_id,
263
  role="user",
264
  content="I have uploaded a document. Please analyze it.",
265
  attachments=[{"file_id": file_obj.id, "tools": [{"type": "file_search"}]}]
266
  )
267
-
268
  return file_obj.id
269
  except Exception as e:
270
  raise Exception(f"Error uploading document: {str(e)}")
271
- finally:
272
- # Clean up temporary file
273
- if 'tmp' in locals():
274
- try:
275
- os.unlink(tmp.name)
276
- except:
277
- pass
278
 
279
  def ask_question(self, question: str) -> str:
280
  try:
@@ -428,8 +415,6 @@ with gr.Blocks(css=custom_css, title="Document Q&A System") as demo:
428
  """)
429
 
430
  chatbot = gr.Chatbot(height=400, elem_classes="gradio-chatbot", label=None, type="messages")
431
- file_output = gr.Textbox(label="Upload Status", interactive=False, elem_classes="textbox")
432
- question = gr.Textbox(label="Type your question and press Enter", placeholder="Ask a question about your document...", elem_classes="textbox")
433
  audio_input = gr.Audio(type="filepath", label="Record or Upload Audio", elem_classes="gradio-audio", visible=False)
434
  tts_output = gr.Audio(label="Assistant Voice Reply", interactive=False, visible=False)
435
 
@@ -443,9 +428,16 @@ with gr.Blocks(css=custom_css, title="Document Q&A System") as demo:
443
  audio_input
444
  send_voice_btn = gr.Button("Send Voice Note", elem_classes="send-btn", visible=False)
445
  reset_btn = gr.Button("Reset Chat & Upload New Document", elem_classes="reset-btn")
446
- file_output
447
- question
448
  file_input.change(process_file, file_input, file_output)
 
 
 
 
 
 
 
449
  reset_btn.click(reset_all, None, [file_output, chatbot, question, audio_input, tts_output])
450
  def show_audio():
451
  return {audio_input: gr.update(visible=True), send_voice_btn: gr.update(visible=True)}
 
222
  class AdvancedRAG:
223
  def __init__(self):
224
  self.thread_id: Optional[str] = None
225
+ self.file_ids: List[str] = []
226
  self.assistant_id: Optional[str] = os.getenv("ASSISTANT_ID")
227
+ if hasattr(self, 'vector_store_id'):
228
+ self.vector_store_id = None
229
 
230
  def create_thread(self) -> str:
231
  thread = openai.beta.threads.create()
 
233
  return self.thread_id
234
 
235
  def upload_document(self, file) -> str:
236
+ # Always reset state for a new document
237
+ self.thread_id = None
238
+ self.file_ids = []
239
+ if hasattr(self, 'vector_store_id'):
240
+ self.vector_store_id = None
241
  try:
242
+ # Create a new file
243
+ with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.name)[1]) as tmp:
244
+ tmp.write(file.read())
 
 
 
 
 
 
 
 
245
  tmp.flush()
 
 
246
  with open(tmp.name, "rb") as file_obj:
247
  file_obj = openai.files.create(
248
  file=file_obj,
249
  purpose="assistants"
250
  )
251
+ self.file_ids.append(file_obj.id)
252
+ # Create a new thread for the new document
253
+ thread = openai.beta.threads.create()
254
+ self.thread_id = thread.id
255
+ # Add the file to the thread
 
256
  openai.beta.threads.messages.create(
257
  thread_id=self.thread_id,
258
  role="user",
259
  content="I have uploaded a document. Please analyze it.",
260
  attachments=[{"file_id": file_obj.id, "tools": [{"type": "file_search"}]}]
261
  )
 
262
  return file_obj.id
263
  except Exception as e:
264
  raise Exception(f"Error uploading document: {str(e)}")
 
 
 
 
 
 
 
265
 
266
  def ask_question(self, question: str) -> str:
267
  try:
 
415
  """)
416
 
417
  chatbot = gr.Chatbot(height=400, elem_classes="gradio-chatbot", label=None, type="messages")
 
 
418
  audio_input = gr.Audio(type="filepath", label="Record or Upload Audio", elem_classes="gradio-audio", visible=False)
419
  tts_output = gr.Audio(label="Assistant Voice Reply", interactive=False, visible=False)
420
 
 
428
  audio_input
429
  send_voice_btn = gr.Button("Send Voice Note", elem_classes="send-btn", visible=False)
430
  reset_btn = gr.Button("Reset Chat & Upload New Document", elem_classes="reset-btn")
431
+ file_output = gr.Textbox(label="Upload Status", interactive=False, elem_classes="textbox")
432
+ question = gr.Textbox(label="Type your question and press Enter", placeholder="Ask a question about your document...", elem_classes="textbox")
433
  file_input.change(process_file, file_input, file_output)
434
+ def reset_all():
435
+ rag.thread_id = None
436
+ if hasattr(rag, 'file_ids'):
437
+ rag.file_ids = []
438
+ if hasattr(rag, 'vector_store_id'):
439
+ rag.vector_store_id = None
440
+ return "", [], "", None, None
441
  reset_btn.click(reset_all, None, [file_output, chatbot, question, audio_input, tts_output])
442
  def show_audio():
443
  return {audio_input: gr.update(visible=True), send_voice_btn: gr.update(visible=True)}