Diogogp24 commited on
Commit
bece47b
·
verified ·
1 Parent(s): 4982450

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -25
app.py CHANGED
@@ -1,14 +1,14 @@
1
- from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
  from bs4 import BeautifulSoup
8
-
9
  from Gradio_UI import GradioUI
10
 
11
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
 
12
  @tool
13
  def get_football_team_info(team_name: str, info_type: str) -> str:
14
  """A tool that fetches football team information (results or upcoming matches) from ZeroZero.
@@ -19,7 +19,6 @@ def get_football_team_info(team_name: str, info_type: str) -> str:
19
  try:
20
  search_query = team_name.replace(" ", "+")
21
  base_url = "https://www.zerozero.pt"
22
- # O ZeroZero redireciona muitas vezes a busca direta para a página da equipa
23
  search_url = f"{base_url}/search.php?input_search={search_query}"
24
 
25
  headers = {
@@ -29,10 +28,9 @@ def get_football_team_info(team_name: str, info_type: str) -> str:
29
  response = requests.get(search_url, headers=headers, timeout=10)
30
 
31
  if response.status_code == 200:
32
- # Retornamos o link e uma instrução para o Agente
33
  return f"Encontrei informações para '{team_name}'. Podes consultar os {info_type} aqui: {response.url}"
34
  else:
35
- return f"Não foi possível aceder ao ZeroZero (Status: {response.status_code})."
36
 
37
  except Exception as e:
38
  return f"Erro ao procurar a equipa: {str(e)}"
@@ -44,51 +42,45 @@ def get_current_time_in_timezone(timezone: str) -> str:
44
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
45
  """
46
  try:
47
- # Create timezone object
48
  tz = pytz.timezone(timezone)
49
- # Get current time in that timezone
50
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
51
  return f"The current local time in {timezone} is: {local_time}"
52
  except Exception as e:
53
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
54
 
 
55
 
 
56
  final_answer = FinalAnswerTool()
57
-
58
- # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
59
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
60
 
61
  model = HfApiModel(
62
- max_tokens=2096,
63
- temperature=0.5,
64
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
65
- custom_role_conversions=None,
66
  )
67
 
68
-
69
- # Import tool from Hub
70
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
71
-
72
  with open("prompts.yaml", 'r') as stream:
73
  prompt_templates = yaml.safe_load(stream)
74
-
 
 
75
  agent = CodeAgent(
76
  model=model,
77
  tools=[
78
  final_answer,
79
  get_football_team_info,
80
  get_current_time_in_timezone,
81
- search_tool,
82
  image_generation_tool
83
  ],
84
  max_steps=6,
85
  verbosity_level=1,
86
- grammar=None,
87
- planning_interval=None,
88
  name="FootballTimeAgent",
89
  description="Um agente que sabe horas e resultados de futebol.",
90
  prompt_templates=prompt_templates
91
  )
92
 
93
-
94
- GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
  from bs4 import BeautifulSoup
 
8
  from Gradio_UI import GradioUI
9
 
10
+ # --- FERRAMENTAS PERSONALIZADAS ---
11
+
12
  @tool
13
  def get_football_team_info(team_name: str, info_type: str) -> str:
14
  """A tool that fetches football team information (results or upcoming matches) from ZeroZero.
 
19
  try:
20
  search_query = team_name.replace(" ", "+")
21
  base_url = "https://www.zerozero.pt"
 
22
  search_url = f"{base_url}/search.php?input_search={search_query}"
23
 
24
  headers = {
 
28
  response = requests.get(search_url, headers=headers, timeout=10)
29
 
30
  if response.status_code == 200:
 
31
  return f"Encontrei informações para '{team_name}'. Podes consultar os {info_type} aqui: {response.url}"
32
  else:
33
+ return f"O ZeroZero não permitiu o acesso direto (Status: {response.status_code}). Tenta usar a ferramenta de busca geral."
34
 
35
  except Exception as e:
36
  return f"Erro ao procurar a equipa: {str(e)}"
 
42
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
43
  """
44
  try:
 
45
  tz = pytz.timezone(timezone)
 
46
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
47
  return f"The current local time in {timezone} is: {local_time}"
48
  except Exception as e:
49
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
50
 
51
+ # --- INICIALIZAÇÃO ---
52
 
53
+ # Importante: Definir as ferramentas antes de criar o Agente
54
  final_answer = FinalAnswerTool()
55
+ search_tool = DuckDuckGoSearchTool() # Criar a instância que faltava
56
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
 
57
 
58
  model = HfApiModel(
59
+ max_tokens=2096,
60
+ temperature=0.5,
61
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
 
62
  )
63
 
 
 
 
 
64
  with open("prompts.yaml", 'r') as stream:
65
  prompt_templates = yaml.safe_load(stream)
66
+
67
+ # --- CRIAÇÃO DO AGENTE ---
68
+
69
  agent = CodeAgent(
70
  model=model,
71
  tools=[
72
  final_answer,
73
  get_football_team_info,
74
  get_current_time_in_timezone,
75
+ search_tool, # Agora já está definido!
76
  image_generation_tool
77
  ],
78
  max_steps=6,
79
  verbosity_level=1,
 
 
80
  name="FootballTimeAgent",
81
  description="Um agente que sabe horas e resultados de futebol.",
82
  prompt_templates=prompt_templates
83
  )
84
 
85
+ if __name__ == "__main__":
86
+ GradioUI(agent).launch()