Chris4K commited on
Commit
8f64755
·
verified ·
1 Parent(s): ac23069

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -26
app.py CHANGED
@@ -1,17 +1,13 @@
1
  """Run hf agent."""
2
-
3
  # pylint: disable=line-too-long, broad-exception-caught
4
  import datetime
5
-
6
-
7
- # import requests
8
  import pytz
9
  import yaml
 
10
  from Gradio_UI import GradioUI
11
  from loguru import logger
12
  from smolagents import (
13
  CodeAgent,
14
- # DuckDuckGoSearchTool,
15
  HfApiModel,
16
  load_tool,
17
  tool,
@@ -19,32 +15,25 @@ from smolagents import (
19
  from tools.final_answer import FinalAnswerTool
20
  from tools.visit_webpage import VisitWebpageTool
21
 
22
-
23
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
24
  @tool
25
- def my_cutom_tool(arg1: str, arg2: int) -> str: # it's import to specify the return type
26
- # Keep this format for the description / args / args description but feel free to modify the tool
27
  """
28
  Run a tool that does nothing yet.
29
-
30
  Args:
31
  arg1: the first argument
32
  arg2: the second argument
33
-
34
  """
35
  return f"What magic will you build? {arg1=}, {arg2=}"
36
 
37
-
38
  @tool
39
  def get_current_time_in_timezone(timezone: str) -> str:
40
  """
41
  Run a tool that fetches the current local time in a specified timezone.
42
-
43
  Args:
44
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
45
-
46
  """
47
- logger.debug(f" get_current_time_in_timezone {timezone=}")
48
  try:
49
  # Create timezone object
50
  tz = pytz.timezone(timezone)
@@ -55,27 +44,32 @@ def get_current_time_in_timezone(timezone: str) -> str:
55
  logger.error(e)
56
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
57
 
58
-
59
  final_answer = FinalAnswerTool()
60
  visit_webpage = VisitWebpageTool()
61
-
62
  model = HfApiModel()
63
 
64
-
65
  # Import tool from Hub
66
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
67
 
 
68
  with open("prompts.yaml", "r", encoding="utf8") as stream:
69
  prompt_templates = yaml.safe_load(stream)
70
 
 
 
 
 
 
71
  agent = CodeAgent(
72
  model=model,
73
  tools=[
74
- my_cutom_tool,
75
  get_current_time_in_timezone,
76
  final_answer,
77
  visit_webpage,
78
- ], ## add your tools here (don't remove final answer)
 
79
  max_steps=6,
80
  verbosity_level=1,
81
  grammar=None,
@@ -85,11 +79,10 @@ agent = CodeAgent(
85
  prompt_templates=prompt_templates,
86
  )
87
 
88
- # debug = True
89
- # share = True
90
  ssr = True
91
  GradioUI(agent).launch(
92
- # debug=debug, # already set in GradioUI
93
- # share=share,
94
- # ssr=ssr,
95
- )
 
1
  """Run hf agent."""
 
2
  # pylint: disable=line-too-long, broad-exception-caught
3
  import datetime
 
 
 
4
  import pytz
5
  import yaml
6
+ import re
7
  from Gradio_UI import GradioUI
8
  from loguru import logger
9
  from smolagents import (
10
  CodeAgent,
 
11
  HfApiModel,
12
  load_tool,
13
  tool,
 
15
  from tools.final_answer import FinalAnswerTool
16
  from tools.visit_webpage import VisitWebpageTool
17
 
18
+ # Custom tool example
 
19
  @tool
20
+ def my_custom_tool(arg1: str, arg2: int) -> str: # it's important to specify the return type
 
21
  """
22
  Run a tool that does nothing yet.
 
23
  Args:
24
  arg1: the first argument
25
  arg2: the second argument
 
26
  """
27
  return f"What magic will you build? {arg1=}, {arg2=}"
28
 
 
29
  @tool
30
  def get_current_time_in_timezone(timezone: str) -> str:
31
  """
32
  Run a tool that fetches the current local time in a specified timezone.
 
33
  Args:
34
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
 
35
  """
36
+ logger.debug(f"get_current_time_in_timezone {timezone=}")
37
  try:
38
  # Create timezone object
39
  tz = pytz.timezone(timezone)
 
44
  logger.error(e)
45
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
46
 
47
+ # Initialize tools
48
  final_answer = FinalAnswerTool()
49
  visit_webpage = VisitWebpageTool()
 
50
  model = HfApiModel()
51
 
 
52
  # Import tool from Hub
53
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
54
 
55
+ # Load prompt templates
56
  with open("prompts.yaml", "r", encoding="utf8") as stream:
57
  prompt_templates = yaml.safe_load(stream)
58
 
59
+ # Add the missing 'final_answer' template if it doesn't exist
60
+ if 'final_answer' not in prompt_templates:
61
+ prompt_templates['final_answer'] = "{{final_answer}}"
62
+
63
+ # Create the agent
64
  agent = CodeAgent(
65
  model=model,
66
  tools=[
67
+ my_custom_tool,
68
  get_current_time_in_timezone,
69
  final_answer,
70
  visit_webpage,
71
+ image_generation_tool, # Added the image generation tool
72
+ ], # add your tools here
73
  max_steps=6,
74
  verbosity_level=1,
75
  grammar=None,
 
79
  prompt_templates=prompt_templates,
80
  )
81
 
82
+ # Launch the Gradio UI
 
83
  ssr = True
84
  GradioUI(agent).launch(
85
+ # debug=True,
86
+ # share=True,
87
+ ssr=ssr,
88
+ )