Shreyas094 commited on
Commit
d282619
·
verified ·
1 Parent(s): 83a84c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -31
app.py CHANGED
@@ -271,24 +271,38 @@ def generate_chunked_response(prompt, model, max_tokens=10000, num_calls=3, temp
271
  print(f"Final clean response: {final_response[:100]}...")
272
  return final_response
273
 
274
- def duckduckgo_search(query):
275
- with DDGS() as ddgs:
276
- results = ddgs.text(query, max_results=5)
277
- return results
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
278
 
279
  class CitingSources(BaseModel):
280
  sources: List[str] = Field(
281
  ...,
282
  description="List of sources to cite. Should be an URL of the source."
283
  )
284
- def chatbot_interface(message, history, use_web_search, model, temperature, num_calls):
 
285
  if not message.strip():
286
  return "", history
287
-
288
  history = history + [(message, "")]
289
-
290
  try:
291
- for response in respond(message, history, model, temperature, num_calls, use_web_search):
292
  history[-1] = (message, response)
293
  yield history
294
  except gr.CancelledError:
@@ -307,21 +321,19 @@ def retry_last_response(history, use_web_search, model, temperature, num_calls):
307
 
308
  return chatbot_interface(last_user_msg, history, use_web_search, model, temperature, num_calls)
309
 
310
- def respond(message, history, model, temperature, num_calls, use_web_search, selected_docs):
311
  logging.info(f"User Query: {message}")
312
  logging.info(f"Model Used: {model}")
313
  logging.info(f"Search Type: {'Web Search' if use_web_search else 'PDF Search'}")
314
-
315
- logging.info(f"Selected Documents: {selected_docs}")
316
-
317
  try:
318
  if use_web_search:
319
- for main_content, sources in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature):
320
  response = f"{main_content}\n\n{sources}"
321
  first_line = response.split('\n')[0] if response else ''
322
- # logging.info(f"Generated Response (first line): {first_line}")
323
  yield response
324
- else:
325
  embed = get_embeddings()
326
  if os.path.exists("faiss_database"):
327
  database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
@@ -419,16 +431,20 @@ After writing the document, please provide a list of sources used in your respon
419
  if not full_response:
420
  yield "I apologize, but I couldn't generate a response at this time. Please try again later."
421
 
422
- def get_response_with_search(query, model, num_calls=3, temperature=0.2):
423
- search_results = duckduckgo_search(query)
424
- context = "\n".join(f"{result['title']}\n{result['body']}\nSource: {result['href']}\n"
425
- for result in search_results if 'body' in result)
426
-
427
- prompt = f"""Using the following context:
 
 
 
 
428
  {context}
429
  Write a detailed and complete research document that fulfills the following user request: '{query}'
430
  After writing the document, please provide a list of sources used in your response."""
431
-
432
  if model == "@cf/meta/llama-3.1-8b-instruct":
433
  # Use Cloudflare API
434
  for response in get_response_from_cloudflare(prompt="", context=context, query=query, num_calls=num_calls, temperature=temperature, search_type="web"):
@@ -444,7 +460,7 @@ After writing the document, please provide a list of sources used in your respon
444
  max_tokens=10000,
445
  temperature=temperature,
446
  stream=True,
447
- ):
448
  if message.choices and message.choices[0].delta and message.choices[0].delta.content:
449
  chunk = message.choices[0].delta.content
450
  main_content += chunk
@@ -576,10 +592,23 @@ demo = gr.ChatInterface(
576
  gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
577
  gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
578
  use_web_search,
579
- document_selector
 
 
 
 
 
 
 
 
 
 
 
 
 
580
  ],
581
  title="AI-powered Web Search and PDF Chat Assistant",
582
- description="Chat with your PDFs or use web search to answer questions. Toggle between Web Search and PDF Chat in Additional Inputs below.",
583
  theme=gr.themes.Soft(
584
  primary_hue="orange",
585
  secondary_hue="amber",
@@ -608,12 +637,12 @@ demo = gr.ChatInterface(
608
  analytics_enabled=False,
609
  textbox=gr.Textbox(placeholder=custom_placeholder, container=False, scale=7),
610
  chatbot = gr.Chatbot(
611
- show_copy_button=True,
612
- likeable=True,
613
- layout="bubble",
614
- height=400,
615
- value=initial_conversation()
616
- )
617
  )
618
 
619
  # Add file upload functionality
 
271
  print(f"Final clean response: {final_response[:100]}...")
272
  return final_response
273
 
274
+ def duckduckgo_search(query, safe="on", region=None, language=None, time=None, filetype=None, site=None, inurl=None, intitle=None, intext=None, related=None, info=None, define=None):
275
+ params = {
276
+ "q": query,
277
+ "safe": safe,
278
+ "region": region,
279
+ "language": language,
280
+ "time": time,
281
+ "filetype": filetype,
282
+ "site": site,
283
+ "inurl": inurl,
284
+ "intitle": intitle,
285
+ "intext": intext,
286
+ "related": related,
287
+ "info": info,
288
+ "define": define
289
+ }
290
+ with DDGS() as ddgs:
291
+ results = ddgs.text(query, params=params, max_results=5)
292
+ return results
293
 
294
  class CitingSources(BaseModel):
295
  sources: List[str] = Field(
296
  ...,
297
  description="List of sources to cite. Should be an URL of the source."
298
  )
299
+
300
+ def chatbot_interface(message, history, use_web_search, model, temperature, num_calls, safe, region, language, time, filetype, site, inurl, intitle, intext, related, info, define):
301
  if not message.strip():
302
  return "", history
 
303
  history = history + [(message, "")]
 
304
  try:
305
+ for response in respond(message, history, model, temperature, num_calls, use_web_search, safe, region, language, time, filetype, site, inurl, intitle, intext, related, info, define):
306
  history[-1] = (message, response)
307
  yield history
308
  except gr.CancelledError:
 
321
 
322
  return chatbot_interface(last_user_msg, history, use_web_search, model, temperature, num_calls)
323
 
324
+ def respond(message, history, model, temperature, num_calls, use_web_search, safe, region, language, time, filetype, site, inurl, intitle, intext, related, info, define):
325
  logging.info(f"User Query: {message}")
326
  logging.info(f"Model Used: {model}")
327
  logging.info(f"Search Type: {'Web Search' if use_web_search else 'PDF Search'}")
328
+
 
 
329
  try:
330
  if use_web_search:
331
+ for main_content, sources in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature, safe=safe, region=region, language=language, time=time, filetype=filetype, site=site, inurl=inurl, intitle=intitle, intext=intext, related=related, info=info, define=define):
332
  response = f"{main_content}\n\n{sources}"
333
  first_line = response.split('\n')[0] if response else ''
334
+ logging.info(f"Generated Response (first line): {first_line}")
335
  yield response
336
+ else:
337
  embed = get_embeddings()
338
  if os.path.exists("faiss_database"):
339
  database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
 
431
  if not full_response:
432
  yield "I apologize, but I couldn't generate a response at this time. Please try again later."
433
 
434
+ def get_response_with_search(query, model, num_calls=3, temperature=0.2, safe="on", region=None, language=None, time=None, filetype=None, site=None, inurl=None, intitle=None, intext=None, related=None, info=None, define=None):
435
+ search_results = duckduckgo_search(query, safe=safe, region=region, language=language, time=time, filetype=filetype, site=site, inurl=inurl, intitle=intitle, intext=intext, related=related, info=info, define=define)
436
+ web_search_database = create_web_search_vectors(search_results)
437
+ if not web_search_database:
438
+ yield "No web search results available. Please try again.", ""
439
+ return
440
+ retriever = web_search_database.as_retriever(search_kwargs={"k": 5})
441
+ relevant_docs = retriever.get_relevant_documents(query)
442
+ context = "\n".join([doc.page_content for doc in relevant_docs])
443
+ prompt = f"""Using the following context from web search results:
444
  {context}
445
  Write a detailed and complete research document that fulfills the following user request: '{query}'
446
  After writing the document, please provide a list of sources used in your response."""
447
+
448
  if model == "@cf/meta/llama-3.1-8b-instruct":
449
  # Use Cloudflare API
450
  for response in get_response_from_cloudflare(prompt="", context=context, query=query, num_calls=num_calls, temperature=temperature, search_type="web"):
 
460
  max_tokens=10000,
461
  temperature=temperature,
462
  stream=True,
463
+ ):
464
  if message.choices and message.choices[0].delta and message.choices[0].delta.content:
465
  chunk = message.choices[0].delta.content
466
  main_content += chunk
 
592
  gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
593
  gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
594
  use_web_search,
595
+ document_selector,
596
+ gr.Dropdown(choices=instruction_choices, label="Select Entity Type for Summary", value="None"),
597
+ gr.Checkbox(label="Safe Search", value=True),
598
+ gr.Textbox(label="Region", placeholder="US"),
599
+ gr.Textbox(label="Language", placeholder="en"),
600
+ gr.Textbox(label="Time", placeholder="d"),
601
+ gr.Textbox(label="Filetype", placeholder="pdf"),
602
+ gr.Textbox(label="Site", placeholder="github.com"),
603
+ gr.Textbox(label="Inurl", placeholder="github.com"),
604
+ gr.Textbox(label="Intitle", placeholder="github"),
605
+ gr.Textbox(label="Intext", placeholder="github"),
606
+ gr.Textbox(label="Related", placeholder="github.com"),
607
+ gr.Textbox(label="Info", placeholder="github.com"),
608
+ gr.Textbox(label="Define", placeholder="github")
609
  ],
610
  title="AI-powered Web Search and PDF Chat Assistant",
611
+ description="Chat with your PDFs, use web search to answer questions, or generate summaries. Select an Entity Type for Summary to generate a specific summary.",
612
  theme=gr.themes.Soft(
613
  primary_hue="orange",
614
  secondary_hue="amber",
 
637
  analytics_enabled=False,
638
  textbox=gr.Textbox(placeholder=custom_placeholder, container=False, scale=7),
639
  chatbot = gr.Chatbot(
640
+ show_copy_button=True,
641
+ likeable=True,
642
+ layout="bubble",
643
+ height=400,
644
+ value=initial_conversation()
645
+ )
646
  )
647
 
648
  # Add file upload functionality