real-jiakai commited on
Commit
6cc760e
·
verified ·
1 Parent(s): 9a8c112

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -26
app.py CHANGED
@@ -1,16 +1,20 @@
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_cutom_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
@@ -18,48 +22,51 @@ def my_cutom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
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
  """
27
  try:
28
- # Create timezone object
29
  tz = pytz.timezone(timezone)
30
- # Get current time in that timezone
31
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
32
  return f"The current local time in {timezone} is: {local_time}"
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
-
37
  final_answer = FinalAnswerTool()
 
 
38
  model = HfApiModel(
39
- max_tokens=2096,
40
- temperature=0.5,
41
- model_id='https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud',# 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,
59
- name=None,
60
- description=None,
61
- prompt_templates=prompt_templates
62
  )
63
 
64
-
65
  GradioUI(agent).launch()
 
1
+ # 导入所需的模块和工具类
2
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
3
+ # 导入日期时间处理模块
4
  import datetime
5
+ # 导入网络请求模块
6
  import requests
7
+ # 导入时区处理模块
8
  import pytz
9
+ # 导入YAML配置文件处理模块
10
  import yaml
11
+ # 导入最终答案工具类
12
  from tools.final_answer import FinalAnswerTool
13
+ # 导入Gradio用户界面类
14
  from Gradio_UI import GradioUI
15
 
16
+ # 定义一个自定义工具函数,接收两个参数并返回字符串
17
+ def my_custom_tool(arg1:str, arg2:int)-> str:
 
 
18
  """A tool that does nothing yet
19
  Args:
20
  arg1: the first argument
 
22
  """
23
  return "What magic will you build ?"
24
 
25
+ # 定义一个获取指定时区当前时间的工具函数
26
  def get_current_time_in_timezone(timezone: str) -> str:
27
  """A tool that fetches the current local time in a specified timezone.
28
  Args:
29
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
30
  """
31
  try:
32
+ # 创建时区对象
33
  tz = pytz.timezone(timezone)
34
+ # 获取该时区的当前时间并格式化
35
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
36
  return f"The current local time in {timezone} is: {local_time}"
37
  except Exception as e:
38
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
39
 
40
+ # 创建最终答案工具实例
41
  final_answer = FinalAnswerTool()
42
+
43
+ # 配置AI模型参数
44
  model = HfApiModel(
45
+ max_tokens=2096, # 设置最大令牌数
46
+ temperature=0.5, # 设置温度参数
47
+ model_id='meta-llama/Meta-Llama-3-8B-Instruct', # 设置模型ID
48
+ custom_role_conversions=None, # 设置自定义角色转换
49
  )
50
 
51
+ # 从Hub加载图像生成工具
 
52
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
53
 
54
+ # 读取YAML格式的提示模板文件
55
  with open("prompts.yaml", 'r') as stream:
56
  prompt_templates = yaml.safe_load(stream)
57
 
58
+ # 创建代码智能体实例
59
  agent = CodeAgent(
60
+ model=model, # 设置使用的模型
61
+ tools=[final_answer], # 设置要使用的工具列表
62
+ max_steps=6, # 设置最大步骤数
63
+ verbosity_level=1, # 设置详细程度级别
64
+ grammar=None, # 设置语法规则
65
+ planning_interval=None, # 设置规划间隔
66
+ name=None, # 设置名称
67
+ description=None, # 设置描述
68
+ prompt_templates=prompt_templates # 设置提示模板
69
  )
70
 
71
+ # 启动Gradio用户界面
72
  GradioUI(agent).launch()