lamhieu commited on
Commit
3b99fcf
1 Parent(s): 5324b14

chore: update something

Browse files
Files changed (1) hide show
  1. app.py +28 -7
app.py CHANGED
@@ -279,7 +279,7 @@ if torch.cuda.is_available():
279
  )
280
  model.generation_config.max_length = 123392
281
 
282
- waiting_tools_timeout = 7.5
283
  supported_tools = json.dumps(
284
  [
285
  {
@@ -302,6 +302,12 @@ supported_tools = json.dumps(
302
  "default": "google",
303
  "required": True,
304
  },
 
 
 
 
 
 
305
  },
306
  },
307
  },
@@ -320,10 +326,16 @@ def extract_text_from_webpage(html_content):
320
  return visible_text
321
 
322
 
323
- def search_with_wikipedia(query: str):
 
 
 
324
  all_results = []
325
  try:
326
- all_results.append(wikipedia.summary(query))
 
 
 
327
  except Exception as e:
328
  pass
329
  return all_results
@@ -333,10 +345,11 @@ def search_with_google(
333
  query: str,
334
  num_results: int = 3,
335
  timeout: int = 5,
 
336
  ssl_verify: bool = None,
337
  ):
338
  all_results = []
339
- max_chars_per_page = 8192
340
  with requests.Session() as session:
341
  resp = session.get(
342
  url="https://www.google.com/search",
@@ -347,6 +360,7 @@ def search_with_google(
347
  "q": query,
348
  "num": num_results,
349
  "udm": 14,
 
350
  },
351
  timeout=timeout,
352
  verify=ssl_verify,
@@ -461,12 +475,20 @@ def generate(
461
  ):
462
  keyword = scheduled_tools_runs["arguments"]["keyword"]
463
  search_type = scheduled_tools_runs["arguments"]["type"]
 
464
  if search_type == "wikipedia":
465
  gr.Info("Searching for information on the Wikipedia.")
466
- document_references = search_with_wikipedia(keyword)
 
 
467
  else:
468
  gr.Info("Searching for information on the Google.")
469
- document_references = search_with_google(keyword)
 
 
 
 
 
470
 
471
  apply_tools = (
472
  True if allow_used_tools is True and previous_response is None else False
@@ -516,7 +538,6 @@ def generate(
516
  and state["respond"] is False
517
  and state["mark"] + waiting_tools_timeout > time.time()
518
  ):
519
- gr.Info("Searching for information on the internet.")
520
  previous_response = "".join(outputs)
521
  yield from generate_chat_responses(previous_response=previous_response)
522
 
 
279
  )
280
  model.generation_config.max_length = 123392
281
 
282
+ waiting_tools_timeout = 10
283
  supported_tools = json.dumps(
284
  [
285
  {
 
302
  "default": "google",
303
  "required": True,
304
  },
305
+ "language": {
306
+ "type": "string",
307
+ "description": "Search language, is the user language code with 2 letters, e.g: vi = vietnamese, en = english.",
308
+ "default": "en",
309
+ "required": True,
310
+ },
311
  },
312
  },
313
  },
 
326
  return visible_text
327
 
328
 
329
+ def search_with_wikipedia(
330
+ query: str,
331
+ language: str = "en",
332
+ ):
333
  all_results = []
334
  try:
335
+ wikipedia.set_lang(language)
336
+ page = wikipedia.page(query)
337
+ content = page.content[:5120]
338
+ all_results.append(content)
339
  except Exception as e:
340
  pass
341
  return all_results
 
345
  query: str,
346
  num_results: int = 3,
347
  timeout: int = 5,
348
+ language: str = "en",
349
  ssl_verify: bool = None,
350
  ):
351
  all_results = []
352
+ max_chars_per_page = 4096
353
  with requests.Session() as session:
354
  resp = session.get(
355
  url="https://www.google.com/search",
 
360
  "q": query,
361
  "num": num_results,
362
  "udm": 14,
363
+ "hl": language,
364
  },
365
  timeout=timeout,
366
  verify=ssl_verify,
 
475
  ):
476
  keyword = scheduled_tools_runs["arguments"]["keyword"]
477
  search_type = scheduled_tools_runs["arguments"]["type"]
478
+ language = scheduled_tools_runs["arguments"]["language"]
479
  if search_type == "wikipedia":
480
  gr.Info("Searching for information on the Wikipedia.")
481
+ document_references = search_with_wikipedia(
482
+ query=keyword, language=language
483
+ )
484
  else:
485
  gr.Info("Searching for information on the Google.")
486
+ document_references = search_with_google(
487
+ query=keyword, language=language
488
+ )
489
+ print(
490
+ "scheduled_tools_runs:", scheduled_tools_runs, document_references
491
+ )
492
 
493
  apply_tools = (
494
  True if allow_used_tools is True and previous_response is None else False
 
538
  and state["respond"] is False
539
  and state["mark"] + waiting_tools_timeout > time.time()
540
  ):
 
541
  previous_response = "".join(outputs)
542
  yield from generate_chat_responses(previous_response=previous_response)
543