Files changed (1) hide show
  1. app.py +70 -32
app.py CHANGED
@@ -1,64 +1,102 @@
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
 
 
 
 
 
 
 
 
 
 
8
  from Gradio_UI import GradioUI
9
 
 
 
10
  @tool
11
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
12
- #Keep this format for the description / args / args description but feel free to modify the tool
13
- """A tool that does nothing yet
 
14
  Args:
15
- arg1: the first argument
16
- arg2: the second argument
17
  """
18
- return "What magic will you build ?"
19
 
 
 
20
  @tool
21
  def get_current_time_in_timezone(timezone: str) -> str:
22
- """A tool that fetches the current local time in a specified timezone.
 
 
23
  Args:
24
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
25
  """
26
  try:
27
- # Create timezone object
28
  tz = pytz.timezone(timezone)
29
- # Get current time in that timezone
30
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
31
- return f"The current local time in {timezone} is: {local_time}"
32
- except Exception as e:
33
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
 
 
 
 
34
 
35
 
 
36
  final_answer = FinalAnswerTool()
 
 
 
37
  model = HfApiModel(
38
- max_tokens=2096,
39
- temperature=0.5,
40
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
41
- custom_role_conversions=None,
42
  )
43
 
44
 
45
- # Import tool from Hub
46
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
 
 
 
 
 
 
 
47
 
48
- with open("prompts.yaml", 'r') as stream:
49
  prompt_templates = yaml.safe_load(stream)
50
-
 
 
51
  agent = CodeAgent(
52
  model=model,
53
- tools=[image_generation_tool,get_current_time_in_timezone,final_answer,DuckDuckGoSearchTool()], ## add or remove tools here
54
- max_steps=6,
 
 
 
 
 
 
55
  verbosity_level=1,
56
- grammar=None,
57
  planning_interval=None,
58
- name=None,
59
- description=None,
60
- prompt_templates=prompt_templates
61
  )
62
 
63
 
64
- GradioUI(agent).launch()
 
 
 
1
+
2
  import datetime
3
+ from pathlib import Path
4
+
5
  import pytz
6
  import yaml
 
7
 
8
+ from smolagents import (
9
+ CodeAgent,
10
+ DuckDuckGoSearchTool,
11
+ HfApiModel,
12
+ load_tool,
13
+ tool,
14
+ )
15
+
16
+ from tools.final_answer import FinalAnswerTool
17
  from Gradio_UI import GradioUI
18
 
19
+
20
+ # 1. Custom tool
21
  @tool
22
+ def my_custom_tool(arg1: str, arg2: int) -> str:
23
+ """
24
+ Demonstration tool that combines two inputs.
25
+
26
  Args:
27
+ arg1: A text message.
28
+ arg2: An integer value.
29
  """
30
+ return f"Message: {arg1}. Number received: {arg2}."
31
 
32
+
33
+ # 2. Time-zone tool
34
  @tool
35
  def get_current_time_in_timezone(timezone: str) -> str:
36
+ """
37
+ Get current local time in a specified timezone.
38
+
39
  Args:
40
+ timezone: Timezone such as Asia/Riyadh.
41
  """
42
  try:
 
43
  tz = pytz.timezone(timezone)
44
+ local_time = datetime.datetime.now(tz)
45
+
46
+ return (
47
+ f"Current time in {timezone}: "
48
+ f"{local_time.strftime('%Y-%m-%d %H:%M:%S')}"
49
+ )
50
+
51
+ except pytz.exceptions.UnknownTimeZoneError:
52
+ return f"Unknown timezone: {timezone}"
53
 
54
 
55
+ # 3. Final answer tool
56
  final_answer = FinalAnswerTool()
57
+
58
+
59
+ # 4. Hugging Face model
60
  model = HfApiModel(
61
+ max_tokens=2096,
62
+ temperature=0.5,
63
+ model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
 
64
  )
65
 
66
 
67
+ # 5. Load image-generation tool
68
+ image_generation_tool = load_tool(
69
+ "agents-course/text-to-image",
70
+ trust_remote_code=True,
71
+ )
72
+
73
+
74
+ # 6. Load prompts
75
+ BASE_DIR = Path(__file__).resolve().parent
76
 
77
+ with open(BASE_DIR / "prompts.yaml", "r", encoding="utf-8") as stream:
78
  prompt_templates = yaml.safe_load(stream)
79
+
80
+
81
+ # 7. Create agent
82
  agent = CodeAgent(
83
  model=model,
84
+ tools=[
85
+ final_answer,
86
+ my_custom_tool,
87
+ get_current_time_in_timezone,
88
+ DuckDuckGoSearchTool(),
89
+ image_generation_tool,
90
+ ],
91
+ max_steps=8,
92
  verbosity_level=1,
 
93
  planning_interval=None,
94
+ name="First Agent",
95
+ description="A general-purpose AI agent with search, time and image tools.",
96
+ prompt_templates=prompt_templates,
97
  )
98
 
99
 
100
+ # 8. Launch application
101
+ if __name__ == "__main__":
102
+ GradioUI(agent).launch()