A1843 commited on
Commit
a2fc957
·
verified ·
1 Parent(s): 6688480

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -55
app.py CHANGED
@@ -4,70 +4,24 @@ import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
-
8
  from Gradio_UI import GradioUI
9
- from bs4 import BeautifulSoup
10
 
11
  @tool
12
- def recherche_google(query: str) -> dict:
13
- """Recherche sur Google un mot-clé donné.
14
 
15
  Args:
16
  query: Le mot-clé à rechercher.
17
-
18
  Returns:
19
  dict: Dictionnaire contenant l'URL et le titre du premier résultat.
20
  """
21
- url_search = "https://www.google.com/search"
22
- headers = {
23
- "User-Agent": (
24
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
25
- "AppleWebKit/537.36 (KHTML, like Gecko) "
26
- "Chrome/98.0.4758.102 Safari/537.36"
27
- )
28
- }
29
- params = {"q": query, "hl": "fr"}
30
-
31
- response = requests.get(url_search, params=params, headers=headers)
32
- if response.status_code != 200:
33
- raise Exception("Erreur lors de la requête vers Google")
34
-
35
- soup = BeautifulSoup(response.text, "html.parser")
36
- # Recherche des blocs de résultats ; la structure de Google pouvant varier,
37
- # nous itérons sur les résultats pour trouver un lien valide.
38
- results = soup.find_all("div", class_="g")
39
- for result in results:
40
- a_tag = result.find("a")
41
- if a_tag:
42
- url_result = a_tag.get("href")
43
- titre = a_tag.get_text(strip=True)
44
- if url_result and titre:
45
- return {"url": url_result, "titre": titre}
46
  return {"url": None, "titre": None}
47
 
48
- @tool
49
- def my_custom_tool(arg1: str, arg2: int) -> str:
50
- """A tool that does nothing yet
51
- Args:
52
- arg1: the first argument
53
- arg2: the second argument
54
- """
55
- return "What magic will you build ?"
56
-
57
- @tool
58
- def get_current_time_in_timezone(timezone: str) -> str:
59
- """A tool that fetches the current local time in a specified timezone.
60
-
61
- Args:
62
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
63
- """
64
- try:
65
- tz = pytz.timezone(timezone)
66
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
67
- return f"The current local time in {timezone} is: {local_time}"
68
- except Exception as e:
69
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
70
-
71
  final_answer = FinalAnswerTool()
72
  model = HfApiModel(
73
  max_tokens=2096,
@@ -76,7 +30,7 @@ model = HfApiModel(
76
  custom_role_conversions=None,
77
  )
78
 
79
- # Import tool from Hub (exemple d'outil de génération d'image)
80
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
81
 
82
  with open("prompts.yaml", 'r') as stream:
@@ -84,7 +38,7 @@ with open("prompts.yaml", 'r') as stream:
84
 
85
  agent = CodeAgent(
86
  model=model,
87
- tools=[final_answer, recherche_google, my_custom_tool, get_current_time_in_timezone],
88
  max_steps=6,
89
  verbosity_level=1,
90
  grammar=None,
 
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
 
7
  from Gradio_UI import GradioUI
 
8
 
9
  @tool
10
+ def duckduckgo_search(query: str) -> dict:
11
+ """Un outil qui effectue une recherche sur DuckDuckGo pour un mot-clé donné.
12
 
13
  Args:
14
  query: Le mot-clé à rechercher.
15
+
16
  Returns:
17
  dict: Dictionnaire contenant l'URL et le titre du premier résultat.
18
  """
19
+ ddg_tool = DuckDuckGoSearchTool()
20
+ result = ddg_tool.search(query)
21
+ if result is not None and isinstance(result, dict):
22
+ return {"url": result.get("url"), "titre": result.get("title")}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  return {"url": None, "titre": None}
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  final_answer = FinalAnswerTool()
26
  model = HfApiModel(
27
  max_tokens=2096,
 
30
  custom_role_conversions=None,
31
  )
32
 
33
+ # Import d'un outil depuis le Hub (exemple d'outil de génération d'image)
34
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
35
 
36
  with open("prompts.yaml", 'r') as stream:
 
38
 
39
  agent = CodeAgent(
40
  model=model,
41
+ tools=[final_answer, duckduckgo_search],
42
  max_steps=6,
43
  verbosity_level=1,
44
  grammar=None,