Amyot commited on
Commit
d022de1
·
verified ·
1 Parent(s): ae7a494

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -17
app.py CHANGED
@@ -1,5 +1,6 @@
1
- from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
 
3
  import requests
4
  import pytz
5
  import yaml
@@ -9,18 +10,20 @@ from Gradio_UI import GradioUI
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
15
  Args:
16
  arg1: the first argument
17
  arg2: the second argument
18
  """
19
  return "What magic will you build ?"
20
 
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
23
- """A tool that fetches the current local time in a specified timezone.
 
24
  Args:
25
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
26
  """
@@ -34,28 +37,81 @@ def get_current_time_in_timezone(timezone: str) -> str:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
 
37
- final_answer = FinalAnswerTool()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- # 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:
40
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  model = HfApiModel(
43
- max_tokens=2096,
44
- temperature=0.5,
45
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
46
- custom_role_conversions=None,
47
  )
48
 
49
-
50
  # Import tool from Hub
51
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
52
 
53
  with open("prompts.yaml", 'r') as stream:
54
  prompt_templates = yaml.safe_load(stream)
55
-
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
 
 
 
 
 
 
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
@@ -65,5 +121,5 @@ agent = CodeAgent(
65
  prompt_templates=prompt_templates
66
  )
67
 
68
-
69
- GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
3
+ from datetime import timedelta
4
  import requests
5
  import pytz
6
  import yaml
 
10
 
11
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
12
  @tool
13
+ def my_custom_tool(arg1: str, arg2: int) -> str:
14
+ """
15
+ A tool that does nothing yet
16
  Args:
17
  arg1: the first argument
18
  arg2: the second argument
19
  """
20
  return "What magic will you build ?"
21
 
22
+
23
  @tool
24
  def get_current_time_in_timezone(timezone: str) -> str:
25
+ """
26
+ A tool that fetches the current local time in a specified timezone.
27
  Args:
28
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
29
  """
 
37
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
38
 
39
 
40
+ @tool
41
+ def get_news_about_rome_last_25_hours() -> str:
42
+ """
43
+ Fetches news articles about Rome from the last 25 hours using NewsAPI.
44
+ No arguments are needed; the API key is hard-coded for demonstration purposes.
45
+ """
46
+ api_key = "3e9552668ced43b8a7c6a9bbfdba8184" # Provided NewsAPI key
47
+ # Calculate the UTC time 25 hours ago
48
+ from_date = datetime.datetime.utcnow() - timedelta(hours=25)
49
+ from_date_iso = from_date.isoformat("T") + "Z" # Convert to ISO 8601 format
50
+
51
+ params = {
52
+ "q": "Rome",
53
+ "from": from_date_iso,
54
+ "sortBy": "publishedAt",
55
+ "apiKey": api_key
56
+ }
57
+ url = "https://newsapi.org/v2/everything"
58
+
59
+ try:
60
+ response = requests.get(url, params=params)
61
+ data = response.json()
62
 
63
+ if data.get("status") != "ok":
64
+ return f"NewsAPI error: {data}"
65
 
66
+ articles = data.get("articles", [])
67
+ if not articles:
68
+ return "No articles found about Rome in the last 25 hours."
69
+
70
+ summary = [f"Found {len(articles)} articles about Rome in the last 25 hours:\n"]
71
+ for i, article in enumerate(articles, start=1):
72
+ title = article.get("title", "No Title")
73
+ source_name = article["source"].get("name", "Unknown Source")
74
+ published_at = article.get("publishedAt", "No Date")
75
+ article_url = article.get("url", "")
76
+ summary.append(
77
+ f"{i}. {title}\n"
78
+ f" Source: {source_name}\n"
79
+ f" Published At: {published_at}\n"
80
+ f" URL: {article_url}\n"
81
+ )
82
+ return "\n".join(summary)
83
+
84
+ except Exception as e:
85
+ return f"Error while fetching news about Rome: {str(e)}"
86
+
87
+
88
+ # Final answer tool (do not remove)
89
+ final_answer = FinalAnswerTool()
90
+
91
+ # Model configuration
92
  model = HfApiModel(
93
+ max_tokens=2096,
94
+ temperature=0.5,
95
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded
96
+ custom_role_conversions=None,
97
  )
98
 
 
99
  # Import tool from Hub
100
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
101
 
102
  with open("prompts.yaml", 'r') as stream:
103
  prompt_templates = yaml.safe_load(stream)
104
+
105
+ # Add your custom tools to the agent
106
  agent = CodeAgent(
107
  model=model,
108
+ tools=[
109
+ final_answer, # Must not remove final_answer
110
+ my_custom_tool, # Your "does-nothing" tool
111
+ get_current_time_in_timezone,
112
+ get_news_about_rome_last_25_hours
113
+ # image_generation_tool, # You can add more as needed
114
+ ],
115
  max_steps=6,
116
  verbosity_level=1,
117
  grammar=None,
 
121
  prompt_templates=prompt_templates
122
  )
123
 
124
+ # Launch the Gradio UI
125
+ GradioUI(agent).launch()