Shreyas094 commited on
Commit
c5fdf7b
·
verified ·
1 Parent(s): f45437d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -80
app.py CHANGED
@@ -247,42 +247,23 @@ def respond(message, history, model, temperature, num_calls, use_web_search, sel
247
  logging.info(f"User Query: {message}")
248
  logging.info(f"Model Used: {model}")
249
  logging.info(f"Search Type: {'Web Search' if use_web_search else 'PDF Search'}")
 
250
  logging.info(f"Selected Documents: {selected_docs}")
251
 
252
  try:
253
  if use_web_search:
254
- search_results = duckduckgo_search(message)
255
- context = "\n".join(f"{result['title']}\n{result['body']}\nSource: {result['href']}\n"
256
- for result in search_results if 'body' in result)
257
-
258
- prompt = f"""Using the following context:
259
- {context}
260
- Write a detailed and complete research document that fulfills the following user request: '{message}'
261
- After writing the document, please provide a list of sources used in your response."""
262
-
263
- if model == "@cf/meta/llama-3.1-8b-instruct":
264
- for response in get_response_from_cloudflare(prompt="", context=context, query=message, num_calls=num_calls, temperature=temperature, search_type="web"):
265
- yield response
266
- else:
267
- client = InferenceClient(model, token=huggingface_token)
268
- response = ""
269
- for i in range(num_calls):
270
- for msg in client.chat_completion(
271
- messages=[{"role": "user", "content": prompt}],
272
- max_tokens=10000,
273
- temperature=temperature,
274
- stream=True,
275
- ):
276
- if msg.choices and msg.choices[0].delta and msg.choices[0].delta.content:
277
- chunk = msg.choices[0].delta.content
278
- response += chunk
279
- yield response
280
  else:
281
  embed = get_embeddings()
282
  if os.path.exists("faiss_database"):
283
  database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
284
  retriever = database.as_retriever()
285
 
 
286
  all_relevant_docs = retriever.get_relevant_documents(message)
287
  relevant_docs = [doc for doc in all_relevant_docs if doc.metadata["source"] in selected_docs]
288
 
@@ -292,34 +273,30 @@ def respond(message, history, model, temperature, num_calls, use_web_search, sel
292
 
293
  context_str = "\n".join([doc.page_content for doc in relevant_docs])
294
  else:
 
295
  yield "No documents available. Please upload PDF documents to answer questions."
296
  return
297
 
298
  if model == "@cf/meta/llama-3.1-8b-instruct":
299
- for response in get_response_from_cloudflare(prompt="", context=context_str, query=message, num_calls=num_calls, temperature=temperature, search_type="pdf"):
300
- yield response
 
 
 
301
  else:
302
- prompt = f"""Using the following context from the PDF documents:
303
- {context_str}
304
- Write a detailed and complete response that answers the following user question: '{message}'"""
305
-
306
- client = InferenceClient(model, token=huggingface_token)
307
- response = ""
308
- for i in range(num_calls):
309
- for msg in client.chat_completion(
310
- messages=[{"role": "user", "content": prompt}],
311
- max_tokens=10000,
312
- temperature=temperature,
313
- stream=True,
314
- ):
315
- if msg.choices and msg.choices[0].delta and msg.choices[0].delta.content:
316
- chunk = msg.choices[0].delta.content
317
- response += chunk
318
- yield response
319
-
320
  except Exception as e:
321
  logging.error(f"Error with {model}: {str(e)}")
322
- yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."
 
 
 
 
 
323
 
324
  logging.basicConfig(level=logging.DEBUG)
325
 
@@ -504,39 +481,57 @@ document_selector = gr.CheckboxGroup(label="Select documents to query")
504
 
505
  use_web_search = gr.Checkbox(label="Use Web Search", value=True)
506
 
507
- with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
508
- gr.Markdown("# AI-powered Web Search and PDF Chat Assistant")
509
- gr.Markdown("Chat with your PDFs or use web search to answer questions")
510
-
511
- use_web_search = gr.Checkbox(label="Use Web Search", value=True)
512
- document_selector = gr.CheckboxGroup(label="Select documents to query")
513
-
514
- chat_interface = gr.ChatInterface(
515
- respond,
516
- additional_inputs=[
517
- gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),
518
- gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
519
- gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
520
- document_selector # Moved use_web_search out, kept document_selector here
521
- ],
522
- title="Chat Interface",
523
- description="Ask questions about your PDFs or use web search",
524
- examples=[
525
- ["Tell me about the contents of the uploaded PDFs."],
526
- ["What are the main topics discussed in the documents?"],
527
- ["Can you summarize the key points from the PDFs?"]
528
- ],
529
- cache_examples=False,
530
- analytics_enabled=False,
531
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
532
 
533
  with gr.Row():
534
  file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
535
  parser_dropdown = gr.Dropdown(choices=["pypdf", "llamaparse"], label="Select PDF Parser", value="llamaparse")
536
  update_button = gr.Button("Upload Document")
537
-
538
  update_output = gr.Textbox(label="Update Status")
539
-
 
540
  update_button.click(update_vectors,
541
  inputs=[file_input, parser_dropdown],
542
  outputs=[update_output, document_selector])
@@ -544,12 +539,12 @@ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
544
  gr.Markdown(
545
  """
546
  ## How to use
547
- 1. Use the "Use Web Search" checkbox at the top to switch between PDF chat and web search.
548
- 2. Upload PDF documents using the file input at the bottom.
549
- 3. Select the PDF parser (pypdf or llamaparse) and click "Upload Document" to update the vector store.
550
- 4. Select the documents you want to query using the checkboxes.
551
- 5. Adjust Temperature and Number of API Calls in the Additional Inputs to fine-tune the response generation.
552
- 6. Type your questions in the chat interface and press Enter to get responses.
553
  7. Use the provided examples or ask your own questions.
554
  """
555
  )
 
247
  logging.info(f"User Query: {message}")
248
  logging.info(f"Model Used: {model}")
249
  logging.info(f"Search Type: {'Web Search' if use_web_search else 'PDF Search'}")
250
+
251
  logging.info(f"Selected Documents: {selected_docs}")
252
 
253
  try:
254
  if use_web_search:
255
+ for main_content, sources in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature):
256
+ response = f"{main_content}\n\n{sources}"
257
+ first_line = response.split('\n')[0] if response else ''
258
+ # logging.info(f"Generated Response (first line): {first_line}")
259
+ yield response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
260
  else:
261
  embed = get_embeddings()
262
  if os.path.exists("faiss_database"):
263
  database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
264
  retriever = database.as_retriever()
265
 
266
+ # Filter relevant documents based on user selection
267
  all_relevant_docs = retriever.get_relevant_documents(message)
268
  relevant_docs = [doc for doc in all_relevant_docs if doc.metadata["source"] in selected_docs]
269
 
 
273
 
274
  context_str = "\n".join([doc.page_content for doc in relevant_docs])
275
  else:
276
+ context_str = "No documents available."
277
  yield "No documents available. Please upload PDF documents to answer questions."
278
  return
279
 
280
  if model == "@cf/meta/llama-3.1-8b-instruct":
281
+ # Use Cloudflare API
282
+ for partial_response in get_response_from_cloudflare(prompt="", context=context_str, query=message, num_calls=num_calls, temperature=temperature, search_type="pdf"):
283
+ first_line = partial_response.split('\n')[0] if partial_response else ''
284
+ logging.info(f"Generated Response (first line): {first_line}")
285
+ yield partial_response
286
  else:
287
+ # Use Hugging Face API
288
+ for partial_response in get_response_from_pdf(message, model, selected_docs, num_calls=num_calls, temperature=temperature):
289
+ first_line = partial_response.split('\n')[0] if partial_response else ''
290
+ logging.info(f"Generated Response (first line): {first_line}")
291
+ yield partial_response
 
 
 
 
 
 
 
 
 
 
 
 
 
292
  except Exception as e:
293
  logging.error(f"Error with {model}: {str(e)}")
294
+ if "microsoft/Phi-3-mini-4k-instruct" in model:
295
+ logging.info("Falling back to Mistral model due to Phi-3 error")
296
+ fallback_model = "mistralai/Mistral-7B-Instruct-v0.3"
297
+ yield from respond(message, history, fallback_model, temperature, num_calls, use_web_search, selected_docs)
298
+ else:
299
+ yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."
300
 
301
  logging.basicConfig(level=logging.DEBUG)
302
 
 
481
 
482
  use_web_search = gr.Checkbox(label="Use Web Search", value=True)
483
 
484
+ demo = gr.ChatInterface(
485
+ respond,
486
+ additional_inputs=[
487
+ gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),
488
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
489
+ gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
490
+ use_web_search,
491
+ document_selector # Add the document selector to the chat interface
492
+ ],
493
+ title="AI-powered Web Search and PDF Chat Assistant",
494
+ description="Chat with your PDFs or use web search to answer questions (Please use toggle under Additional Inputs to swithc between PDF and Web Search, Default Value Web Search)",
495
+ theme=gr.themes.Soft(
496
+ primary_hue="orange",
497
+ secondary_hue="amber",
498
+ neutral_hue="gray",
499
+ font=[gr.themes.GoogleFont("Exo"), "ui-sans-serif", "system-ui", "sans-serif"]
500
+ ).set(
501
+ body_background_fill_dark="#0c0505",
502
+ block_background_fill_dark="#0c0505",
503
+ block_border_width="1px",
504
+ block_title_background_fill_dark="#1b0f0f",
505
+ input_background_fill_dark="#140b0b",
506
+ button_secondary_background_fill_dark="#140b0b",
507
+ border_color_accent_dark="#1b0f0f",
508
+ border_color_primary_dark="#1b0f0f",
509
+ background_fill_secondary_dark="#0c0505",
510
+ color_accent_soft_dark="transparent",
511
+ code_background_fill_dark="#140b0b"
512
+ ),
513
+ css=css,
514
+ examples=[
515
+ ["Tell me about the contents of the uploaded PDFs."],
516
+ ["What are the main topics discussed in the documents?"],
517
+ ["Can you summarize the key points from the PDFs?"]
518
+ ],
519
+ cache_examples=False,
520
+ analytics_enabled=False,
521
+ )
522
+
523
+ # Add file upload functionality
524
+ with demo:
525
+ gr.Markdown("## Upload PDF Documents")
526
 
527
  with gr.Row():
528
  file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
529
  parser_dropdown = gr.Dropdown(choices=["pypdf", "llamaparse"], label="Select PDF Parser", value="llamaparse")
530
  update_button = gr.Button("Upload Document")
531
+
532
  update_output = gr.Textbox(label="Update Status")
533
+
534
+ # Update both the output text and the document selector
535
  update_button.click(update_vectors,
536
  inputs=[file_input, parser_dropdown],
537
  outputs=[update_output, document_selector])
 
539
  gr.Markdown(
540
  """
541
  ## How to use
542
+ 1. Upload PDF documents using the file input at the top.
543
+ 2. Select the PDF parser (pypdf or llamaparse) and click "Upload Document" to update the vector store.
544
+ 3. Select the documents you want to query using the checkboxes.
545
+ 4. Ask questions in the chat interface.
546
+ 5. Toggle "Use Web Search" to switch between PDF chat and web search.
547
+ 6. Adjust Temperature and Number of API Calls to fine-tune the response generation.
548
  7. Use the provided examples or ask your own questions.
549
  """
550
  )