sayed99 commited on
Commit
5ab5f5e
·
1 Parent(s): c1b4094

new tools

Browse files
Files changed (2) hide show
  1. app.py +55 -16
  2. prompts.yaml +4 -2
app.py CHANGED
@@ -1,22 +1,45 @@
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
  # 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:
@@ -35,24 +58,40 @@ def get_current_time_in_timezone(timezone: str) -> str:
35
 
36
 
37
  final_answer = FinalAnswerTool()
 
 
 
 
38
  model = HfApiModel(
39
- max_tokens=2096,
40
- temperature=0.5,
41
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
42
- custom_role_conversions=None,
 
43
  )
44
 
 
 
 
 
45
 
46
  # Import tool from Hub
47
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
 
 
 
48
 
49
  with open("prompts.yaml", 'r') as stream:
50
  prompt_templates = yaml.safe_load(stream)
51
-
52
  agent = CodeAgent(
53
  model=model,
54
- tools=[final_answer], ## add your tools here (don't remove final answer)
55
- max_steps=6,
 
 
 
 
56
  verbosity_level=1,
57
  grammar=None,
58
  planning_interval=None,
@@ -62,4 +101,4 @@ agent = CodeAgent(
62
  )
63
 
64
 
65
- 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
+ import os
8
+ import uuid
9
+ from dotenv import load_dotenv
10
+ from tools.image_caption import ImageCaptionTool
11
+ from tools.image_generation import ImageGenerationTool
12
 
13
  from Gradio_UI import GradioUI
14
 
15
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
16
+
17
+
18
  @tool
19
+ def basic_calculator(operation: str, num1: float, num2: float) -> str:
20
+ """A tool that performs basic arithmetic operations.
 
21
  Args:
22
+ operation: The arithmetic operation to perform ('add', 'subtract', 'multiply', 'divide').
23
+ num1: The first number.
24
+ num2: The second number.
25
  """
26
+ try:
27
+ if operation == 'add':
28
+ result = num1 + num2
29
+ elif operation == 'subtract':
30
+ result = num1 - num2
31
+ elif operation == 'multiply':
32
+ result = num1 * num2
33
+ elif operation == 'divide':
34
+ if num2 == 0:
35
+ return "Error: Division by zero is not allowed."
36
+ result = num1 / num2
37
+ else:
38
+ return "Error: Invalid operation. Please use 'add', 'subtract', 'multiply', or 'divide'."
39
+ return f"The result of {operation}ing {num1} and {num2} is: {result}"
40
+ except Exception as e:
41
+ return f"Error performing calculation: {str(e)}"
42
+
43
 
44
  @tool
45
  def get_current_time_in_timezone(timezone: str) -> str:
 
58
 
59
 
60
  final_answer = FinalAnswerTool()
61
+
62
+ # 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:
63
+ # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
64
+
65
  model = HfApiModel(
66
+ max_tokens=2096,
67
+ temperature=0.5,
68
+ # model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
69
+ model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
70
+ custom_role_conversions=None,
71
  )
72
 
73
+ load_dotenv(override=True)
74
+ token = os.getenv("HF_API_KEY")
75
+ if not token:
76
+ raise ValueError("HF_API_KEY environment variable not set")
77
 
78
  # Import tool from Hub
79
+ # image_generation_tool = load_tool(
80
+ # "agents-course/text-to-image", trust_remote_code=True, token=token
81
+ # )
82
+
83
 
84
  with open("prompts.yaml", 'r') as stream:
85
  prompt_templates = yaml.safe_load(stream)
86
+
87
  agent = CodeAgent(
88
  model=model,
89
+ # add your tools here (don't remove final answer)
90
+ tools=[final_answer, get_current_time_in_timezone, basic_calculator,
91
+ ImageGenerationTool(), ImageCaptionTool(),
92
+ DuckDuckGoSearchTool()
93
+ ],
94
+ max_steps=15,
95
  verbosity_level=1,
96
  grammar=None,
97
  planning_interval=None,
 
101
  )
102
 
103
 
104
+ GradioUI(agent).launch()
prompts.yaml CHANGED
@@ -21,11 +21,13 @@
21
  ```<end_code>
22
  Observation: "The oldest person in the document is John Doe, a 55 year old lumberjack living in Newfoundland."
23
 
24
- Thought: I will now generate an image showcasing the oldest person.
25
  Code:
26
  ```py
27
- image = image_generator("A portrait of John Doe, a 55-year-old man living in Canada.")
 
28
  final_answer(image)
 
29
  ```<end_code>
30
 
31
  ---
 
21
  ```<end_code>
22
  Observation: "The oldest person in the document is John Doe, a 55 year old lumberjack living in Newfoundland."
23
 
24
+ Thought: I will now generate an image showcasing the oldest person and save it in generations dir.
25
  Code:
26
  ```py
27
+ import uuid
28
+ image, img_path = image_generator("A portrait of John Doe, a 55-year-old man living in Canada.")
29
  final_answer(image)
30
+ final_answer(img_path)
31
  ```<end_code>
32
 
33
  ---