A-Aryam commited on
Commit
d03367b
·
verified ·
1 Parent(s): 7342b08

add new tools for agent

Browse files
Files changed (1) hide show
  1. app.py +77 -24
app.py CHANGED
@@ -69,19 +69,40 @@
69
 
70
  # GradioUI(agent).launch()
71
 
72
- # new code: (by gemini)
 
73
  import datetime
74
  import pytz
75
  import requests
76
  import yaml
77
-
78
- # 1. Import spaces at the top
79
  import spaces
80
-
81
- from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
82
- from tools.final_answer import FinalAnswerTool
83
  from Gradio_UI import GradioUI
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  @tool
86
  def my_custom_tool(arg1: str, arg2: int) -> str:
87
  """A tool that does nothing yet
@@ -91,6 +112,20 @@ def my_custom_tool(arg1: str, arg2: int) -> str:
91
  """
92
  return "What magic will you build ?"
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  @tool
95
  def get_current_time_in_timezone(timezone: str) -> str:
96
  """A tool that fetches the current local time in a specified timezone.
@@ -104,37 +139,55 @@ def get_current_time_in_timezone(timezone: str) -> str:
104
  except Exception as e:
105
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
106
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
- final_answer = FinalAnswerTool()
 
 
109
 
110
- model = HfApiModel(
111
- max_tokens=2096,
112
- temperature=0.5,
113
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
114
- custom_role_conversions=None,
115
- )
116
 
117
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
 
 
 
 
 
118
 
119
- with open("prompts.yaml", 'r') as stream:
120
- prompt_templates = yaml.safe_load(stream)
121
-
 
 
 
 
 
122
  agent = CodeAgent(
123
  model=model,
124
- tools=[final_answer],
125
  max_steps=6,
126
  verbosity_level=1,
127
  grammar=None,
128
  planning_interval=None,
129
  name=None,
130
  description=None,
131
- prompt_templates=prompt_templates
132
  )
133
 
134
- # 2. Decorate the agent execution function for ZeroGPU
135
- @spaces.GPU
136
- def run_agent(prompt):
137
- return agent.run(prompt)
138
-
139
  # Launch Gradio UI
140
  GradioUI(agent).launch()
 
69
 
70
  # GradioUI(agent).launch()
71
 
72
+ # new code: (by gemini + me + Agents course)
73
+ # 1. Import spaces at the top
74
  import datetime
75
  import pytz
76
  import requests
77
  import yaml
 
 
78
  import spaces
79
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool, FinalAnswerTool, InferenceClientModel
80
+ #from tools.final_answer import FinalAnswerTool
 
81
  from Gradio_UI import GradioUI
82
 
83
+ # Gloable vars and important stuff:
84
+
85
+ search_tool = DuckDuckGoSearchTool() #for search tool # the easy way.
86
+ final_answer = FinalAnswerTool() #final answer tool
87
+ # image generation tool
88
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
89
+
90
+ # our llm:
91
+ #model = HfApiModel(
92
+ model = InferenceClientModel(
93
+ max_tokens=2096,
94
+ temperature=0.5,
95
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
96
+ custom_role_conversions=None,
97
+ )
98
+
99
+ # in `prompts.yml` we have sys_prompt.
100
+ with open("prompts.yaml", 'r') as stream:
101
+ prompt_templates = yaml.safe_load(stream)
102
+
103
+
104
+ # Tools (user def tools):
105
+
106
  @tool
107
  def my_custom_tool(arg1: str, arg2: int) -> str:
108
  """A tool that does nothing yet
 
112
  """
113
  return "What magic will you build ?"
114
 
115
+ @tool
116
+ def hbty(b_day: str) -> str: #hbty = Happy Birthday to you
117
+ """
118
+ A tool for wishing Happy Birthday, If use query or inputs have keyword "birthday".
119
+ Args:
120
+ b_day = a string, reprasenting valid question, or statement, if this staement have "birthday" or "bday" then wish "Happy Birthday to you"
121
+ """
122
+ #return "Happy Birthday To You !!!! :)"
123
+ cleaned_input = b_day.lower()
124
+ if "birthday" in cleaned_input or "bday" in cleaned_input:
125
+ return "Happy Birthday To You !!!! :)"
126
+ else:
127
+ return "No birthday mentioned in the statement."
128
+
129
  @tool
130
  def get_current_time_in_timezone(timezone: str) -> str:
131
  """A tool that fetches the current local time in a specified timezone.
 
139
  except Exception as e:
140
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
141
 
142
+ @tool
143
+ def get_search_results(query: str) -> str:
144
+ """A tool that fetches the data from the internet, for a specific search query.
145
+ Args:
146
+ query: A string representing a valid question or request for example, 'What is the capitol of india?'
147
+ """
148
+ try:
149
+ # DuckDuckGoSearchTool is callable directly with the query string
150
+ search_results = _ddg_search(query)
151
+ return search_results
152
+ except Exception as e:
153
+ return f"Error executing search query '{query}': {str(e)}"
154
 
155
+ @tool
156
+ def generate_code_snippet(language: str, task_description: str) -> str:
157
+ """Generates a clean code snippet in a requested programming language for a specific task.
158
 
159
+ Args:
160
+ language: The programming language to write (e.g., 'python', 'cpp', 'javascript', 'sql').
161
+ task_description: A clear description of what the code should do.
162
+ """
163
+ prompt = f"Write a clean, efficient {language} code snippet to fulfill this task: {task_description}. Provide brief comments."
 
164
 
165
+ try:
166
+ # Call the model to generate code
167
+ response = model([{"role": "user", "content": prompt}])
168
+ return response.content
169
+ except Exception as e:
170
+ return f"Error generating code: {str(e)}"
171
 
172
+
173
+ # Runner code:
174
+ # 2. Decorate the agent execution function for ZeroGPU
175
+ @spaces.GPU
176
+ def run_agent(prompt):
177
+ return agent.run(prompt)
178
+
179
+ # our agent:
180
  agent = CodeAgent(
181
  model=model,
182
+ tools=[final_answer,generate_code_snippet,get_search_results,get_current_time_in_timezone,hbty,my_custom_tool,search_tool,image_generation_tool], # add your tools here (don't remove final_answer)
183
  max_steps=6,
184
  verbosity_level=1,
185
  grammar=None,
186
  planning_interval=None,
187
  name=None,
188
  description=None,
189
+ prompt_templates=prompt_templates # Pass system prompt to CodeAgent
190
  )
191
 
 
 
 
 
 
192
  # Launch Gradio UI
193
  GradioUI(agent).launch()