kakimtched commited on
Commit
6d2e5d8
·
verified ·
1 Parent(s): 4e05ef4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -27
app.py CHANGED
@@ -1,44 +1,56 @@
1
- import string
2
- from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
3
  import datetime
4
- import requests
5
  import pytz
6
  import yaml
7
  from tools.final_answer import FinalAnswerTool
8
-
9
  from Gradio_UI import GradioUI
10
 
11
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
 
12
  @tool
13
- def my_custom_tool(arg1: str, arg2: int) -> str: # it's import to specify the return type
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:
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
  @tool
37
  def emoji_summarizer(text: str) -> str:
38
  """
39
- Summarizes the input text into a sequence of emojis that capture the main ideas.
 
40
  Args:
41
- text: Input string to summarize
 
 
 
42
  """
43
  mapping = {
44
  "happy": "😊",
@@ -55,41 +67,52 @@ def emoji_summarizer(text: str) -> str:
55
  "error": "❌",
56
  "question": "❓",
57
  }
58
-
 
59
  words = text.lower().split()
60
  emojis = [mapping[word] for word in words if word in mapping]
 
61
  if not emojis:
62
  return "🤔 (no matching emojis found)"
63
  return "".join(emojis)
64
 
65
- final_answer = FinalAnswerTool()
66
 
67
- # 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:
68
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
69
 
 
70
  model = HfApiModel(
71
  max_tokens=2096,
72
  temperature=0.5,
73
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded
74
  custom_role_conversions=None,
75
  )
76
 
77
- # Import tool from Hub
78
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
79
 
80
- with open("prompts.yaml", 'r') as stream:
 
81
  prompt_templates = yaml.safe_load(stream)
82
-
 
83
  agent = CodeAgent(
84
  model=model,
85
- tools=[final_answer, emoji_summarizer, image_generation_tool], ## add your tools here (don't remove final answer)
 
 
 
 
 
 
86
  max_steps=6,
87
  verbosity_level=1,
88
  grammar=None,
89
  planning_interval=None,
90
  name=None,
91
  description=None,
92
- prompt_templates=prompt_templates
93
  )
94
 
 
95
  GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, HfApiModel, load_tool, tool
 
2
  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
  "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()