Shreyas094 commited on
Commit
cb9f424
·
verified ·
1 Parent(s): f7e2f6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +173 -128
app.py CHANGED
@@ -116,16 +116,13 @@ def update_vectors(files, parser):
116
  label="Select documents to query"
117
  )
118
 
119
- def generate_chunked_response(prompt, model, max_tokens=1000, num_calls=1, temperature=0.2, should_stop=False, continuation=False):
120
  print(f"Starting generate_chunked_response with {num_calls} calls")
121
  full_response = ""
122
  messages = [{"role": "user", "content": prompt}]
123
 
124
- if continuation:
125
- messages.insert(0, {"role": "system", "content": "This is a continuation of a previous response. Please continue from where you left off, maintaining coherence and avoiding repetition."})
126
-
127
  if model == "@cf/meta/llama-3.1-8b-instruct":
128
- # Cloudflare API logic
129
  for i in range(num_calls):
130
  print(f"Starting Cloudflare API call {i+1}")
131
  if should_stop:
@@ -136,12 +133,15 @@ def generate_chunked_response(prompt, model, max_tokens=1000, num_calls=1, tempe
136
  f"https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/@cf/meta/llama-3.1-8b-instruct",
137
  headers={"Authorization": f"Bearer {API_TOKEN}"},
138
  json={
139
- "stream": True,
140
- "messages": messages,
 
 
 
141
  "max_tokens": max_tokens,
142
  "temperature": temperature
143
  },
144
- stream=True
145
  )
146
 
147
  for line in response.iter_lines():
@@ -153,16 +153,11 @@ def generate_chunked_response(prompt, model, max_tokens=1000, num_calls=1, tempe
153
  json_data = json.loads(line.decode('utf-8').split('data: ')[1])
154
  chunk = json_data['response']
155
  full_response += chunk
156
- yield full_response
157
  except json.JSONDecodeError:
158
  continue
159
  print(f"Cloudflare API call {i+1} completed")
160
  except Exception as e:
161
- if "cancelled" in str(e).lower():
162
- print("Generation cancelled")
163
- return
164
- else:
165
- print(f"Error in generating response from Cloudflare: {str(e)}")
166
  else:
167
  # Original Hugging Face API logic
168
  client = InferenceClient(model, token=huggingface_token)
@@ -185,14 +180,9 @@ def generate_chunked_response(prompt, model, max_tokens=1000, num_calls=1, tempe
185
  if message.choices and message.choices[0].delta and message.choices[0].delta.content:
186
  chunk = message.choices[0].delta.content
187
  full_response += chunk
188
- yield full_response
189
  print(f"Hugging Face API call {i+1} completed")
190
  except Exception as e:
191
- if "cancelled" in str(e).lower():
192
- print("Generation cancelled")
193
- return
194
- else:
195
- print(f"Error in generating response from Hugging Face: {str(e)}")
196
 
197
  # Clean up the response
198
  clean_response = re.sub(r'<s>\[INST\].*?\[/INST\]\s*', '', full_response, flags=re.DOTALL)
@@ -214,7 +204,7 @@ def generate_chunked_response(prompt, model, max_tokens=1000, num_calls=1, tempe
214
  final_response = '\n\n'.join(unique_paragraphs)
215
 
216
  print(f"Final clean response: {final_response[:100]}...")
217
- yield final_response
218
 
219
  def duckduckgo_search(query):
220
  with DDGS() as ddgs:
@@ -226,26 +216,6 @@ class CitingSources(BaseModel):
226
  ...,
227
  description="List of sources to cite. Should be an URL of the source."
228
  )
229
- def chatbot_interface(message, history, model, temperature, num_calls, use_web_search, selected_docs):
230
- if not message.strip():
231
- return history
232
-
233
- for response in respond(message, history, model, temperature, num_calls, use_web_search, selected_docs):
234
- yield response
235
-
236
- # Make the Continue Generation button visible after the response is complete
237
- demo.update(visible=True, elem_id="continue_btn")
238
-
239
- try:
240
- for response in respond(message, history, model, temperature, num_calls, use_web_search):
241
- history[-1] = (message, response)
242
- yield history
243
- except gr.CancelledError:
244
- yield history
245
- except Exception as e:
246
- logging.error(f"Unexpected error in chatbot_interface: {str(e)}")
247
- history[-1] = (message, f"An unexpected error occurred: {str(e)}")
248
- yield history
249
 
250
  def retry_last_response(history, use_web_search, model, temperature, num_calls):
251
  if not history:
@@ -257,30 +227,59 @@ def retry_last_response(history, use_web_search, model, temperature, num_calls):
257
  return chatbot_interface(last_user_msg, history, use_web_search, model, temperature, num_calls)
258
 
259
  def respond(message, history, model, temperature, num_calls, use_web_search, selected_docs):
260
- if not message.strip():
261
- return history
 
262
 
263
- history = history + [(message, "")]
264
 
265
  try:
266
  if use_web_search:
267
  for main_content, sources in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature):
268
  response = f"{main_content}\n\n{sources}"
269
- history[-1] = (message, response)
270
- yield history
 
271
  else:
272
- for partial_response in get_response_from_pdf(message, model, selected_docs, num_calls=num_calls, temperature=temperature):
273
- history[-1] = (message, partial_response)
274
- yield history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275
  except Exception as e:
276
- if "cancelled" in str(e).lower():
277
- yield history
 
 
 
278
  else:
279
- logging.error(f"Unexpected error in respond: {str(e)}")
280
- history[-1] = (message, f"An unexpected error occurred: {str(e)}")
281
- yield history
282
-
283
- return history # Ensure we always return the history at the end
284
 
285
  logging.basicConfig(level=logging.DEBUG)
286
 
@@ -338,37 +337,6 @@ After writing the document, please provide a list of sources used in your respon
338
  if not full_response:
339
  yield "I apologize, but I couldn't generate a response at this time. Please try again later."
340
 
341
- def get_response_with_search(query, model, num_calls=3, temperature=0.2):
342
- search_results = duckduckgo_search(query)
343
- context = "\n".join(f"{result['title']}\n{result['body']}\nSource: {result['href']}\n"
344
- for result in search_results if 'body' in result)
345
-
346
- prompt = f"""Using the following context:
347
- {context}
348
- Write a detailed and complete research document that fulfills the following user request: '{query}'
349
- After writing the document, please provide a list of sources used in your response."""
350
-
351
- if model == "@cf/meta/llama-3.1-8b-instruct":
352
- # Use Cloudflare API
353
- for response in get_response_from_cloudflare(prompt="", context=context, query=query, num_calls=num_calls, temperature=temperature, search_type="web"):
354
- yield response, "" # Yield streaming response without sources
355
- else:
356
- # Use Hugging Face API
357
- client = InferenceClient(model, token=huggingface_token)
358
-
359
- main_content = ""
360
- for i in range(num_calls):
361
- for message in client.chat_completion(
362
- messages=[{"role": "user", "content": prompt}],
363
- max_tokens=1000,
364
- temperature=temperature,
365
- stream=True,
366
- ):
367
- if message.choices and message.choices[0].delta and message.choices[0].delta.content:
368
- chunk = message.choices[0].delta.content
369
- main_content += chunk
370
- yield main_content, "" # Yield partial main content without sources
371
-
372
  def get_response_from_pdf(query, model, selected_docs, num_calls=3, temperature=0.2):
373
  logging.info(f"Entering get_response_from_pdf with query: {query}, model: {model}, selected_docs: {selected_docs}")
374
 
@@ -386,7 +354,6 @@ def get_response_from_pdf(query, model, selected_docs, num_calls=3, temperature=
386
  relevant_docs = retriever.get_relevant_documents(query)
387
  logging.info(f"Number of relevant documents retrieved: {len(relevant_docs)}")
388
 
389
- # Filter relevant_docs based on selected documents
390
  filtered_docs = [doc for doc in relevant_docs if doc.metadata["source"] in selected_docs]
391
  logging.info(f"Number of filtered documents: {len(filtered_docs)}")
392
 
@@ -395,28 +362,24 @@ def get_response_from_pdf(query, model, selected_docs, num_calls=3, temperature=
395
  yield "No relevant information found in the selected documents. Please try selecting different documents or rephrasing your query."
396
  return
397
 
398
- for doc in filtered_docs:
399
- logging.info(f"Document source: {doc.metadata['source']}")
400
- logging.info(f"Document content preview: {doc.page_content[:100]}...") # Log first 100 characters of each document
401
-
402
  context_str = "\n".join([doc.page_content for doc in filtered_docs])
403
  logging.info(f"Total context length: {len(context_str)}")
404
 
 
 
405
  if model == "@cf/meta/llama-3.1-8b-instruct":
406
  logging.info("Using Cloudflare API")
407
- # Use Cloudflare API with the retrieved context
408
  for response in get_response_from_cloudflare(prompt="", context=context_str, query=query, num_calls=num_calls, temperature=temperature, search_type="pdf"):
409
- yield response
 
410
  else:
411
  logging.info("Using Hugging Face API")
412
- # Use Hugging Face API
413
  prompt = f"""Using the following context from the PDF documents:
414
  {context_str}
415
  Write a detailed and complete response that answers the following user question: '{query}'"""
416
 
417
  client = InferenceClient(model, token=huggingface_token)
418
 
419
- response = ""
420
  for i in range(num_calls):
421
  logging.info(f"API call {i+1}/{num_calls}")
422
  for message in client.chat_completion(
@@ -427,44 +390,121 @@ Write a detailed and complete response that answers the following user question:
427
  ):
428
  if message.choices and message.choices[0].delta and message.choices[0].delta.content:
429
  chunk = message.choices[0].delta.content
430
- response += chunk
431
- yield response # Yield partial response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
432
 
433
- logging.info("Finished generating response")
 
 
 
 
 
 
 
 
 
 
 
 
434
 
435
  def vote(data: gr.LikeData):
436
  if data.liked:
437
  print(f"You upvoted this response: {data.value}")
438
  else:
439
  print(f"You downvoted this response: {data.value}")
 
 
 
 
 
440
 
441
- def continue_generation(history, model, temperature, num_calls, use_web_search, selected_docs):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
442
  if not history:
443
  return history
444
 
445
  last_user_msg = history[-1][0]
446
- last_ai_response = history[-1][1]
447
-
448
- continuation_prompt = f"""
449
- Previous response: {last_ai_response}
450
-
451
- Original query: {last_user_msg}
452
-
453
- Please continue the response from where you left off, maintaining coherence and avoiding repetition.
454
- """
455
 
456
  try:
457
- for response in respond(continuation_prompt, history[:-1], model, temperature, num_calls, use_web_search, selected_docs):
458
- new_response = f"{last_ai_response}\n\n{response[-1][1]}"
459
- history[-1] = (last_user_msg, new_response)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
460
  yield history
 
 
461
  except Exception as e:
462
- logging.error(f"Error in continue_generation: {str(e)}")
463
- history[-1] = (last_user_msg, f"{last_ai_response}\n\nError continuing generation: {str(e)}")
464
  yield history
465
 
466
- return history
467
-
468
  css = """
469
  /* Add your custom CSS here */
470
  """
@@ -478,9 +518,7 @@ def display_documents():
478
  label="Select documents to query"
479
  )
480
 
481
- # Define the checkbox outside the demo block
482
  document_selector = gr.CheckboxGroup(label="Select documents to query")
483
-
484
  use_web_search = gr.Checkbox(label="Use Web Search", value=False)
485
 
486
  demo = gr.ChatInterface(
@@ -492,6 +530,10 @@ demo = gr.ChatInterface(
492
  use_web_search,
493
  document_selector
494
  ],
 
 
 
 
495
  title="AI-powered Web Search and PDF Chat Assistant",
496
  description="Chat with your PDFs or use web search to answer questions.",
497
  theme=gr.themes.Soft(
@@ -529,19 +571,22 @@ with demo:
529
  with gr.Row():
530
  file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
531
  parser_dropdown = gr.Dropdown(choices=["pypdf", "llamaparse"], label="Select PDF Parser", value="llamaparse")
532
- update_button = gr.Button("Upload Document")
533
 
534
  update_output = gr.Textbox(label="Update Status")
535
 
536
  # Update both the output text and the document selector
537
- update_button.click(update_vectors,
538
- inputs=[file_input, parser_dropdown],
539
- outputs=[update_output, document_selector])
 
 
540
 
541
- continue_btn = gr.Button("Continue Generation", visible=False, elem_id="continue_btn")
542
- continue_btn.click(continue_generation,
543
- inputs=[demo.chatbot] + demo.additional_inputs,
544
- outputs=[demo.chatbot])
 
 
545
 
546
  gr.Markdown(
547
  """
@@ -552,8 +597,8 @@ with demo:
552
  4. Ask questions in the chat interface.
553
  5. Toggle "Use Web Search" to switch between PDF chat and web search.
554
  6. Adjust Temperature and Number of API Calls to fine-tune the response generation.
555
- 7. Use the provided examples or ask your own questions.
556
- 8. If a response is incomplete, click "Continue Generation" for more information.
557
  """
558
  )
559
 
 
116
  label="Select documents to query"
117
  )
118
 
119
+ def generate_chunked_response(prompt, model, max_tokens=1000, num_calls=3, temperature=0.2, should_stop=False):
120
  print(f"Starting generate_chunked_response with {num_calls} calls")
121
  full_response = ""
122
  messages = [{"role": "user", "content": prompt}]
123
 
 
 
 
124
  if model == "@cf/meta/llama-3.1-8b-instruct":
125
+ # Cloudflare API
126
  for i in range(num_calls):
127
  print(f"Starting Cloudflare API call {i+1}")
128
  if should_stop:
 
133
  f"https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/@cf/meta/llama-3.1-8b-instruct",
134
  headers={"Authorization": f"Bearer {API_TOKEN}"},
135
  json={
136
+ "stream": true,
137
+ "messages": [
138
+ {"role": "system", "content": "You are a friendly assistant"},
139
+ {"role": "user", "content": prompt}
140
+ ],
141
  "max_tokens": max_tokens,
142
  "temperature": temperature
143
  },
144
+ stream=true
145
  )
146
 
147
  for line in response.iter_lines():
 
153
  json_data = json.loads(line.decode('utf-8').split('data: ')[1])
154
  chunk = json_data['response']
155
  full_response += chunk
 
156
  except json.JSONDecodeError:
157
  continue
158
  print(f"Cloudflare API call {i+1} completed")
159
  except Exception as e:
160
+ print(f"Error in generating response from Cloudflare: {str(e)}")
 
 
 
 
161
  else:
162
  # Original Hugging Face API logic
163
  client = InferenceClient(model, token=huggingface_token)
 
180
  if message.choices and message.choices[0].delta and message.choices[0].delta.content:
181
  chunk = message.choices[0].delta.content
182
  full_response += chunk
 
183
  print(f"Hugging Face API call {i+1} completed")
184
  except Exception as e:
185
+ print(f"Error in generating response from Hugging Face: {str(e)}")
 
 
 
 
186
 
187
  # Clean up the response
188
  clean_response = re.sub(r'<s>\[INST\].*?\[/INST\]\s*', '', full_response, flags=re.DOTALL)
 
204
  final_response = '\n\n'.join(unique_paragraphs)
205
 
206
  print(f"Final clean response: {final_response[:100]}...")
207
+ return final_response
208
 
209
  def duckduckgo_search(query):
210
  with DDGS() as ddgs:
 
216
  ...,
217
  description="List of sources to cite. Should be an URL of the source."
218
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
219
 
220
  def retry_last_response(history, use_web_search, model, temperature, num_calls):
221
  if not history:
 
227
  return chatbot_interface(last_user_msg, history, use_web_search, model, temperature, num_calls)
228
 
229
  def respond(message, history, model, temperature, num_calls, use_web_search, selected_docs):
230
+ logging.info(f"User Query: {message}")
231
+ logging.info(f"Model Used: {model}")
232
+ logging.info(f"Search Type: {'Web Search' if use_web_search else 'PDF Search'}")
233
 
234
+ logging.info(f"Selected Documents: {selected_docs}")
235
 
236
  try:
237
  if use_web_search:
238
  for main_content, sources in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature):
239
  response = f"{main_content}\n\n{sources}"
240
+ first_line = response.split('\n')[0] if response else ''
241
+ logging.info(f"Generated Response (first line): {first_line}")
242
+ yield response
243
  else:
244
+ embed = get_embeddings()
245
+ if os.path.exists("faiss_database"):
246
+ database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
247
+ retriever = database.as_retriever()
248
+
249
+ # Filter relevant documents based on user selection
250
+ all_relevant_docs = retriever.get_relevant_documents(message)
251
+ relevant_docs = [doc for doc in all_relevant_docs if doc.metadata["source"] in selected_docs]
252
+
253
+ if not relevant_docs:
254
+ yield "No relevant information found in the selected documents. Please try selecting different documents or rephrasing your query."
255
+ return
256
+
257
+ context_str = "\n".join([doc.page_content for doc in relevant_docs])
258
+ else:
259
+ context_str = "No documents available."
260
+ yield "No documents available. Please upload PDF documents to answer questions."
261
+ return
262
+
263
+ if model == "@cf/meta/llama-3.1-8b-instruct":
264
+ # Use Cloudflare API
265
+ for partial_response in get_response_from_cloudflare(prompt="", context=context_str, query=message, num_calls=num_calls, temperature=temperature, search_type="pdf"):
266
+ first_line = partial_response.split('\n')[0] if partial_response else ''
267
+ logging.info(f"Generated Response (first line): {first_line}")
268
+ yield partial_response
269
+ else:
270
+ # Use Hugging Face API
271
+ for partial_response in get_response_from_pdf(message, model, selected_docs, num_calls=num_calls, temperature=temperature):
272
+ first_line = partial_response.split('\n')[0] if partial_response else ''
273
+ logging.info(f"Generated Response (first line): {first_line}")
274
+ yield partial_response
275
  except Exception as e:
276
+ logging.error(f"Error with {model}: {str(e)}")
277
+ if "microsoft/Phi-3-mini-4k-instruct" in model:
278
+ logging.info("Falling back to Mistral model due to Phi-3 error")
279
+ fallback_model = "mistralai/Mistral-7B-Instruct-v0.3"
280
+ yield from respond(message, history, fallback_model, temperature, num_calls, use_web_search, selected_docs)
281
  else:
282
+ yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."
 
 
 
 
283
 
284
  logging.basicConfig(level=logging.DEBUG)
285
 
 
337
  if not full_response:
338
  yield "I apologize, but I couldn't generate a response at this time. Please try again later."
339
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340
  def get_response_from_pdf(query, model, selected_docs, num_calls=3, temperature=0.2):
341
  logging.info(f"Entering get_response_from_pdf with query: {query}, model: {model}, selected_docs: {selected_docs}")
342
 
 
354
  relevant_docs = retriever.get_relevant_documents(query)
355
  logging.info(f"Number of relevant documents retrieved: {len(relevant_docs)}")
356
 
 
357
  filtered_docs = [doc for doc in relevant_docs if doc.metadata["source"] in selected_docs]
358
  logging.info(f"Number of filtered documents: {len(filtered_docs)}")
359
 
 
362
  yield "No relevant information found in the selected documents. Please try selecting different documents or rephrasing your query."
363
  return
364
 
 
 
 
 
365
  context_str = "\n".join([doc.page_content for doc in filtered_docs])
366
  logging.info(f"Total context length: {len(context_str)}")
367
 
368
+ full_response = ""
369
+
370
  if model == "@cf/meta/llama-3.1-8b-instruct":
371
  logging.info("Using Cloudflare API")
 
372
  for response in get_response_from_cloudflare(prompt="", context=context_str, query=query, num_calls=num_calls, temperature=temperature, search_type="pdf"):
373
+ full_response += response
374
+ yield full_response
375
  else:
376
  logging.info("Using Hugging Face API")
 
377
  prompt = f"""Using the following context from the PDF documents:
378
  {context_str}
379
  Write a detailed and complete response that answers the following user question: '{query}'"""
380
 
381
  client = InferenceClient(model, token=huggingface_token)
382
 
 
383
  for i in range(num_calls):
384
  logging.info(f"API call {i+1}/{num_calls}")
385
  for message in client.chat_completion(
 
390
  ):
391
  if message.choices and message.choices[0].delta and message.choices[0].delta.content:
392
  chunk = message.choices[0].delta.content
393
+ full_response += chunk
394
+ yield full_response
395
+
396
+ logging.info("Finished generating initial response")
397
+
398
+ def get_response_with_search(query, model, num_calls=3, temperature=0.2):
399
+ search_results = duckduckgo_search(query)
400
+ context = "\n".join(f"{result['title']}\n{result['body']}\nSource: {result['href']}\n"
401
+ for result in search_results if 'body' in result)
402
+
403
+ prompt = f"""Using the following context:
404
+ {context}
405
+ Write a detailed and complete research document that fulfills the following user request: '{query}'
406
+ After writing the document, please provide a list of sources used in your response."""
407
+
408
+ full_response = ""
409
+
410
+ if model == "@cf/meta/llama-3.1-8b-instruct":
411
+ # Use Cloudflare API
412
+ for response in get_response_from_cloudflare(prompt="", context=context, query=query, num_calls=num_calls, temperature=temperature, search_type="web"):
413
+ full_response += response
414
+ yield full_response, "" # Yield streaming response without sources
415
+ else:
416
+ # Use Hugging Face API
417
+ client = InferenceClient(model, token=huggingface_token)
418
 
419
+ for i in range(num_calls):
420
+ for message in client.chat_completion(
421
+ messages=[{"role": "user", "content": prompt}],
422
+ max_tokens=1000,
423
+ temperature=temperature,
424
+ stream=True,
425
+ ):
426
+ if message.choices and message.choices[0].delta and message.choices[0].delta.content:
427
+ chunk = message.choices[0].delta.content
428
+ full_response += chunk
429
+ yield full_response, "" # Yield partial main content without sources
430
+
431
+ logging.info("Finished generating initial response")
432
 
433
  def vote(data: gr.LikeData):
434
  if data.liked:
435
  print(f"You upvoted this response: {data.value}")
436
  else:
437
  print(f"You downvoted this response: {data.value}")
438
+ def chatbot_interface(message, history, use_web_search, model, temperature, num_calls, selected_docs):
439
+ if not message.strip():
440
+ return "", history
441
+
442
+ history = history + [(message, "")]
443
 
444
+ try:
445
+ if use_web_search:
446
+ for main_content, sources in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature):
447
+ response = f"{main_content}\n\n{sources}"
448
+ history[-1] = (message, response)
449
+ yield history
450
+ else:
451
+ for response in get_response_from_pdf(message, model, selected_docs, num_calls=num_calls, temperature=temperature):
452
+ history[-1] = (message, response)
453
+ yield history
454
+ except gr.CancelledError:
455
+ yield history
456
+ except Exception as e:
457
+ logging.error(f"Unexpected error in chatbot_interface: {str(e)}")
458
+ history[-1] = (message, f"An unexpected error occurred: {str(e)}")
459
+ yield history
460
+
461
+ def continue_generation(history, use_web_search, model, temperature, selected_docs):
462
  if not history:
463
  return history
464
 
465
  last_user_msg = history[-1][0]
466
+ previous_response = history[-1][1]
 
 
 
 
 
 
 
 
467
 
468
  try:
469
+ if use_web_search:
470
+ search_results = duckduckgo_search(last_user_msg)
471
+ context = "\n".join(f"{result['title']}\n{result['body']}\nSource: {result['href']}\n"
472
+ for result in search_results if 'body' in result)
473
+ else:
474
+ embed = get_embeddings()
475
+ if os.path.exists("faiss_database"):
476
+ database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
477
+ retriever = database.as_retriever()
478
+ relevant_docs = retriever.get_relevant_documents(last_user_msg)
479
+ filtered_docs = [doc for doc in relevant_docs if doc.metadata["source"] in selected_docs]
480
+ context = "\n".join([doc.page_content for doc in filtered_docs])
481
+ else:
482
+ return history
483
+
484
+ prompt = f"""Using the following context and partial response, please continue and complete the response:
485
+
486
+ Context:
487
+ {context}
488
+
489
+ Query: {last_user_msg}
490
+
491
+ Partial Response:
492
+ {previous_response}
493
+
494
+ Please continue the response from where it was cut off:"""
495
+
496
+ continued_response = previous_response
497
+ for chunk in get_response_from_cloudflare(prompt=prompt, context="", query="", num_calls=1, temperature=temperature, search_type="continuation"):
498
+ continued_response += chunk
499
+ history[-1] = (last_user_msg, continued_response)
500
  yield history
501
+ except gr.CancelledError:
502
+ yield history
503
  except Exception as e:
504
+ logging.error(f"Unexpected error in continue_generation: {str(e)}")
505
+ history[-1] = (last_user_msg, f"{previous_response}\n\nAn error occurred while continuing generation: {str(e)}")
506
  yield history
507
 
 
 
508
  css = """
509
  /* Add your custom CSS here */
510
  """
 
518
  label="Select documents to query"
519
  )
520
 
 
521
  document_selector = gr.CheckboxGroup(label="Select documents to query")
 
522
  use_web_search = gr.Checkbox(label="Use Web Search", value=False)
523
 
524
  demo = gr.ChatInterface(
 
530
  use_web_search,
531
  document_selector
532
  ],
533
+ additional_buttons=[
534
+ gr.Button("Continue Generation"),
535
+ gr.Button("Upload Document")
536
+ ],
537
  title="AI-powered Web Search and PDF Chat Assistant",
538
  description="Chat with your PDFs or use web search to answer questions.",
539
  theme=gr.themes.Soft(
 
571
  with gr.Row():
572
  file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
573
  parser_dropdown = gr.Dropdown(choices=["pypdf", "llamaparse"], label="Select PDF Parser", value="llamaparse")
 
574
 
575
  update_output = gr.Textbox(label="Update Status")
576
 
577
  # Update both the output text and the document selector
578
+ demo.additional_buttons[1].click(
579
+ update_vectors,
580
+ inputs=[file_input, parser_dropdown],
581
+ outputs=[update_output, document_selector]
582
+ )
583
 
584
+ # Set up the continue generation button
585
+ demo.additional_buttons[0].click(
586
+ continue_generation,
587
+ inputs=[demo.chatbot, use_web_search, demo.additional_inputs[0], demo.additional_inputs[1], document_selector],
588
+ outputs=demo.chatbot
589
+ )
590
 
591
  gr.Markdown(
592
  """
 
597
  4. Ask questions in the chat interface.
598
  5. Toggle "Use Web Search" to switch between PDF chat and web search.
599
  6. Adjust Temperature and Number of API Calls to fine-tune the response generation.
600
+ 7. Use the "Continue Generation" button if you want to extend the last response.
601
+ 8. Use the provided examples or ask your own questions.
602
  """
603
  )
604