Shreyas094 commited on
Commit
3274c71
·
verified ·
1 Parent(s): 7efff23

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -58
app.py CHANGED
@@ -323,7 +323,7 @@ def respond(message, history, model, temperature, num_calls, use_web_search, sel
323
  embed = get_embeddings()
324
  if os.path.exists("faiss_database"):
325
  database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
326
- retriever = database.as_retriever(search_kwargs={"k": 5})
327
 
328
  # Filter relevant documents based on user selection
329
  all_relevant_docs = retriever.get_relevant_documents(message)
@@ -455,33 +455,20 @@ def get_response_from_pdf(query, model, selected_docs, num_calls=3, temperature=
455
  if os.path.exists("faiss_database"):
456
  logging.info("Loading FAISS database")
457
  database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
458
-
459
- # Log the contents of the FAISS database
460
- logging.info(f"FAISS database contains {len(database.docstore._dict)} documents")
461
- for doc_id, doc in database.docstore._dict.items():
462
- logging.info(f"Document ID: {doc_id}, Source: {doc.metadata['source']}")
463
  else:
464
  logging.warning("No FAISS database found")
465
  yield "No documents available. Please upload PDF documents to answer questions."
466
  return
467
 
468
- retriever = database.as_retriever(search_kwargs={"k": 20})
469
  logging.info(f"Retrieving relevant documents for query: {query}")
470
  relevant_docs = retriever.get_relevant_documents(query)
471
  logging.info(f"Number of relevant documents retrieved: {len(relevant_docs)}")
472
 
473
- # Log details of retrieved documents
474
- for i, doc in enumerate(relevant_docs):
475
- logging.info(f"Relevant doc {i}: Source: {doc.metadata['source']}, Content preview: {doc.page_content[:100]}...")
476
-
477
  # Filter relevant_docs based on selected documents
478
  filtered_docs = [doc for doc in relevant_docs if doc.metadata["source"] in selected_docs]
479
  logging.info(f"Number of filtered documents: {len(filtered_docs)}")
480
 
481
- # Log details of filtered documents
482
- for i, doc in enumerate(filtered_docs):
483
- logging.info(f"Filtered doc {i}: Source: {doc.metadata['source']}, Content preview: {doc.page_content[:100]}...")
484
-
485
  if not filtered_docs:
486
  logging.warning(f"No relevant information found in the selected documents: {selected_docs}")
487
  yield "No relevant information found in the selected documents. Please try selecting different documents or rephrasing your query."
@@ -489,56 +476,40 @@ def get_response_from_pdf(query, model, selected_docs, num_calls=3, temperature=
489
 
490
  for doc in filtered_docs:
491
  logging.info(f"Document source: {doc.metadata['source']}")
492
- logging.info(f"Document content preview: {doc.page_content[:100]}...")
493
-
494
- # Implement a sliding window approach for context
495
- max_context_length = 4000 # Adjust based on your model's capacity
496
- context_chunks = []
497
- current_chunk = ""
498
- for doc in filtered_docs:
499
- if len(current_chunk) + len(doc.page_content) > max_context_length:
500
- context_chunks.append(current_chunk)
501
- current_chunk = doc.page_content
502
- else:
503
- current_chunk += "\n" + doc.page_content
504
- if current_chunk:
505
- context_chunks.append(current_chunk)
506
 
507
- logging.info(f"Number of context chunks: {len(context_chunks)}")
 
508
 
509
- for i, context_str in enumerate(context_chunks):
510
- logging.info(f"Processing context chunk {i+1}/{len(context_chunks)}")
511
- logging.info(f"Context chunk length: {len(context_str)}")
512
-
513
- if model == "@cf/meta/llama-3.1-8b-instruct":
514
- logging.info("Using Cloudflare API")
515
- for response in get_response_from_cloudflare(prompt="", context=context_str, query=query, num_calls=num_calls, temperature=temperature, search_type="pdf"):
516
- yield response
517
- else:
518
- logging.info("Using Hugging Face API")
519
- prompt = f"""Using the following context from the PDF documents:
520
  {context_str}
521
  Write a detailed and complete response that answers the following user question: '{query}'"""
522
 
523
- client = InferenceClient(model, token=huggingface_token)
524
 
525
- response = ""
526
- for j in range(num_calls):
527
- logging.info(f"API call {j+1}/{num_calls}")
528
- for message in client.chat_completion(
529
- messages=[{"role": "user", "content": prompt}],
530
- max_tokens=10000,
531
- temperature=temperature,
532
- stream=True,
533
- ):
534
- if message.choices and message.choices[0].delta and message.choices[0].delta.content:
535
- chunk = message.choices[0].delta.content
536
- response += chunk
537
- yield response # Yield partial response
538
 
539
- logging.info("Finished generating response for this context chunk")
540
-
541
- logging.info("Finished processing all context chunks")
542
 
543
  def vote(data: gr.LikeData):
544
  if data.liked:
 
323
  embed = get_embeddings()
324
  if os.path.exists("faiss_database"):
325
  database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
326
+ retriever = database.as_retriever(search_kwargs={"k": 10})
327
 
328
  # Filter relevant documents based on user selection
329
  all_relevant_docs = retriever.get_relevant_documents(message)
 
455
  if os.path.exists("faiss_database"):
456
  logging.info("Loading FAISS database")
457
  database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
 
 
 
 
 
458
  else:
459
  logging.warning("No FAISS database found")
460
  yield "No documents available. Please upload PDF documents to answer questions."
461
  return
462
 
463
+ retriever = database.as_retriever(search_kwargs={"k": 10})
464
  logging.info(f"Retrieving relevant documents for query: {query}")
465
  relevant_docs = retriever.get_relevant_documents(query)
466
  logging.info(f"Number of relevant documents retrieved: {len(relevant_docs)}")
467
 
 
 
 
 
468
  # Filter relevant_docs based on selected documents
469
  filtered_docs = [doc for doc in relevant_docs if doc.metadata["source"] in selected_docs]
470
  logging.info(f"Number of filtered documents: {len(filtered_docs)}")
471
 
 
 
 
 
472
  if not filtered_docs:
473
  logging.warning(f"No relevant information found in the selected documents: {selected_docs}")
474
  yield "No relevant information found in the selected documents. Please try selecting different documents or rephrasing your query."
 
476
 
477
  for doc in filtered_docs:
478
  logging.info(f"Document source: {doc.metadata['source']}")
479
+ logging.info(f"Document content preview: {doc.page_content[:100]}...") # Log first 100 characters of each document
 
 
 
 
 
 
 
 
 
 
 
 
 
480
 
481
+ context_str = "\n".join([doc.page_content for doc in filtered_docs])
482
+ logging.info(f"Total context length: {len(context_str)}")
483
 
484
+ if model == "@cf/meta/llama-3.1-8b-instruct":
485
+ logging.info("Using Cloudflare API")
486
+ # Use Cloudflare API with the retrieved context
487
+ for response in get_response_from_cloudflare(prompt="", context=context_str, query=query, num_calls=num_calls, temperature=temperature, search_type="pdf"):
488
+ yield response
489
+ else:
490
+ logging.info("Using Hugging Face API")
491
+ # Use Hugging Face API
492
+ prompt = f"""Using the following context from the PDF documents:
 
 
493
  {context_str}
494
  Write a detailed and complete response that answers the following user question: '{query}'"""
495
 
496
+ client = InferenceClient(model, token=huggingface_token)
497
 
498
+ response = ""
499
+ for i in range(num_calls):
500
+ logging.info(f"API call {i+1}/{num_calls}")
501
+ for message in client.chat_completion(
502
+ messages=[{"role": "user", "content": prompt}],
503
+ max_tokens=10000,
504
+ temperature=temperature,
505
+ stream=True,
506
+ ):
507
+ if message.choices and message.choices[0].delta and message.choices[0].delta.content:
508
+ chunk = message.choices[0].delta.content
509
+ response += chunk
510
+ yield response # Yield partial response
511
 
512
+ logging.info("Finished generating response")
 
 
513
 
514
  def vote(data: gr.LikeData):
515
  if data.liked: