kakimtched commited on
Commit
8d26650
·
verified ·
1 Parent(s): 6d2e5d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -49
app.py CHANGED
@@ -3,54 +3,40 @@ import datetime
3
  import pytz
4
  import yaml
5
  from tools.final_answer import FinalAnswerTool
6
- from Gradio_UI import GradioUI
7
 
 
8
 
9
- # A simple example tool that currently does nothing but shows how to define a tool.
10
  @tool
11
- def my_custom_tool(arg1: str, arg2: int) -> str:
12
- """
13
- A placeholder tool that returns a fun message.
14
-
15
  Args:
16
- arg1: The first argument as a string.
17
- arg2: The second argument as an integer.
18
-
19
- Returns:
20
- A string message.
21
  """
22
- return "What magic will you build with this tool?"
23
-
24
 
25
  @tool
26
  def get_current_time_in_timezone(timezone: str) -> str:
27
- """
28
- Returns the current local time in the specified timezone.
29
-
30
  Args:
31
- timezone: A valid timezone string (e.g., 'America/New_York').
32
-
33
- Returns:
34
- A formatted string with the current local time or an error message.
35
  """
36
  try:
 
37
  tz = pytz.timezone(timezone)
 
38
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
39
  return f"The current local time in {timezone} is: {local_time}"
40
  except Exception as e:
41
- return f"Error: could not fetch time for timezone '{timezone}'. Details: {e}"
42
-
43
 
44
  @tool
45
  def emoji_summarizer(text: str) -> str:
46
  """
47
- Summarizes the input text using emojis representing key ideas.
48
-
49
  Args:
50
- text: Input text string.
51
-
52
- Returns:
53
- A string of emojis or a default emoji if no matches are found.
54
  """
55
  mapping = {
56
  "happy": "😊",
@@ -67,52 +53,41 @@ def emoji_summarizer(text: str) -> str:
67
  "error": "❌",
68
  "question": "❓",
69
  }
70
-
71
- # Basic tokenization: lowercase and split on whitespace
72
  words = text.lower().split()
73
  emojis = [mapping[word] for word in words if word in mapping]
74
-
75
  if not emojis:
76
  return "🤔 (no matching emojis found)"
77
  return "".join(emojis)
78
 
79
-
80
- # Initialize the final answer tool
81
  final_answer = FinalAnswerTool()
82
 
83
- # Initialize the language model (make sure model_id is correct and accessible)
 
 
84
  model = HfApiModel(
85
  max_tokens=2096,
86
  temperature=0.5,
87
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
88
  custom_role_conversions=None,
89
  )
90
 
91
- # Load the text-to-image generation tool from the Hugging Face hub
92
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
93
 
94
- # Load prompt templates from YAML file (make sure 'prompts.yaml' is present and valid)
95
- with open("prompts.yaml", "r") as stream:
96
  prompt_templates = yaml.safe_load(stream)
97
-
98
- # Initialize the code agent with all desired tools
99
  agent = CodeAgent(
100
  model=model,
101
- tools=[
102
- final_answer,
103
- emoji_summarizer,
104
- image_generation_tool,
105
- my_custom_tool,
106
- get_current_time_in_timezone,
107
- ],
108
  max_steps=6,
109
  verbosity_level=1,
110
  grammar=None,
111
  planning_interval=None,
112
  name=None,
113
  description=None,
114
- prompt_templates=prompt_templates,
115
  )
116
 
117
- # Launch the Gradio UI to interact with the agent
118
  GradioUI(agent).launch()
 
3
  import pytz
4
  import yaml
5
  from tools.final_answer import FinalAnswerTool
 
6
 
7
+ from Gradio_UI import GradioUI
8
 
9
+ # Below is an example of a tool that does nothing. Amaze us with your creativity !
10
  @tool
11
+ def my_custom_tool(arg1: str, arg2: int) -> str: # it's import to specify the return type
12
+ """A tool that does nothing yet
 
 
13
  Args:
14
+ arg1: the first argument
15
+ arg2: the second argument
 
 
 
16
  """
17
+ return "What magic will you build ?"
 
18
 
19
  @tool
20
  def get_current_time_in_timezone(timezone: str) -> str:
21
+ """A tool that fetches the current local time in a specified timezone.
 
 
22
  Args:
23
+ timezone: A string representing a valid timezone (e.g., 'America/New_York').
 
 
 
24
  """
25
  try:
26
+ # Create timezone object
27
  tz = pytz.timezone(timezone)
28
+ # Get current time in that timezone
29
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
30
  return f"The current local time in {timezone} is: {local_time}"
31
  except Exception as e:
32
+ return f"Error fetching time for timezone '{timezone}': {str(e)}"
 
33
 
34
  @tool
35
  def emoji_summarizer(text: str) -> str:
36
  """
37
+ Summarizes the input text into a sequence of emojis that capture the main ideas.
 
38
  Args:
39
+ text: Input string to summarize
 
 
 
40
  """
41
  mapping = {
42
  "happy": "😊",
 
53
  "error": "❌",
54
  "question": "❓",
55
  }
56
+
 
57
  words = text.lower().split()
58
  emojis = [mapping[word] for word in words if word in mapping]
 
59
  if not emojis:
60
  return "🤔 (no matching emojis found)"
61
  return "".join(emojis)
62
 
 
 
63
  final_answer = FinalAnswerTool()
64
 
65
+ # 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:
66
+ # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
67
+
68
  model = HfApiModel(
69
  max_tokens=2096,
70
  temperature=0.5,
71
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded
72
  custom_role_conversions=None,
73
  )
74
 
75
+ # Import tool from Hub
76
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
77
 
78
+ with open("prompts.yaml", 'r') as stream:
 
79
  prompt_templates = yaml.safe_load(stream)
80
+
 
81
  agent = CodeAgent(
82
  model=model,
83
+ tools=[final_answer, emoji_summarizer, image_generation_tool], ## add your tools here (don't remove final answer)
 
 
 
 
 
 
84
  max_steps=6,
85
  verbosity_level=1,
86
  grammar=None,
87
  planning_interval=None,
88
  name=None,
89
  description=None,
90
+ prompt_templates=prompt_templates
91
  )
92
 
 
93
  GradioUI(agent).launch()