SolshineMisfit commited on
Commit
92a8aaa
·
verified ·
1 Parent(s): ae7a494

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -24
app.py CHANGED
@@ -1,26 +1,73 @@
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_custom_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
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
  """
@@ -33,37 +80,66 @@ def get_current_time_in_timezone(timezone: str) -> str:
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
-
37
  final_answer = FinalAnswerTool()
38
 
39
- # 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:
40
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
41
 
 
42
  model = HfApiModel(
43
- max_tokens=2096,
44
- temperature=0.5,
45
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
46
- custom_role_conversions=None,
47
  )
48
 
49
-
50
- # Import tool from Hub
51
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
52
-
53
  with open("prompts.yaml", 'r') as stream:
54
  prompt_templates = yaml.safe_load(stream)
55
-
56
- agent = CodeAgent(
 
 
 
 
 
 
 
 
 
 
 
 
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  max_steps=6,
60
  verbosity_level=1,
 
61
  grammar=None,
62
  planning_interval=None,
63
- name=None,
64
- description=None,
65
  prompt_templates=prompt_templates
66
  )
67
 
68
-
69
- GradioUI(agent).launch()
 
1
+ from smolagents import (
2
+ CodeAgent,
3
+ ToolCallingAgent,
4
+ HfApiModel,
5
+ DuckDuckGoSearchTool,
6
+ LiteLLMModel,
7
+ tool,
8
+ load_tool,
9
+ )
10
  import datetime
11
+ import re
12
  import requests
13
+ from markdownify import markdownify
14
+ from requests.exceptions import RequestException
15
  import pytz
16
  import yaml
17
  from tools.final_answer import FinalAnswerTool
 
18
  from Gradio_UI import GradioUI
19
+ from huggingface_hub import login
20
+
21
+ # Login to Hugging Face
22
+ login()
23
+
24
+ # Set model ID
25
+ model_id = "Qwen/Qwen2.5-Coder-32B-Instruct"
26
 
27
+ # Web browsing tool
28
  @tool
29
+ def visit_webpage(url: str) -> str:
30
+ """Visits a webpage at the given URL and returns its content as a markdown string.
31
+
32
+ Args:
33
+ url: The URL of the webpage to visit.
34
+
35
+ Returns:
36
+ The content of the webpage converted to Markdown, or an error message if the request fails.
37
+ """
38
+ try:
39
+ # Send a GET request to the URL
40
+ response = requests.get(url)
41
+ response.raise_for_status() # Raise an exception for bad status codes
42
+
43
+ # Convert the HTML content to Markdown
44
+ markdown_content = markdownify(response.text).strip()
45
+
46
+ # Remove multiple line breaks
47
+ markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
48
+
49
+ return markdown_content
50
+ except RequestException as e:
51
+ return f"Error fetching the webpage: {str(e)}"
52
+ except Exception as e:
53
+ return f"An unexpected error occurred: {str(e)}"
54
+
55
+ # Custom tool example
56
+ @tool
57
+ def my_custom_tool(arg1: str, arg2: int) -> str:
58
  """A tool that does nothing yet
59
+
60
  Args:
61
  arg1: the first argument
62
  arg2: the second argument
63
  """
64
+ return "This tool is currently a work in progress"
65
 
66
+ # Timezone tool
67
  @tool
68
  def get_current_time_in_timezone(timezone: str) -> str:
69
  """A tool that fetches the current local time in a specified timezone.
70
+
71
  Args:
72
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
73
  """
 
80
  except Exception as e:
81
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
82
 
83
+ # Initialize final answer tool
84
  final_answer = FinalAnswerTool()
85
 
86
+ # Import image generation tool from Hub
87
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
88
 
89
+ # Initialize model
90
  model = HfApiModel(
91
+ max_tokens=2096,
92
+ temperature=0.5,
93
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
94
+ custom_role_conversions=None,
95
  )
96
 
97
+ # Load prompt templates
 
 
 
98
  with open("prompts.yaml", 'r') as stream:
99
  prompt_templates = yaml.safe_load(stream)
100
+
101
+ # ===== MULTI-AGENT SYSTEM SETUP =====
102
+ # 1. Create specialized web search agent
103
+ web_agent = ToolCallingAgent(
104
+ tools=[DuckDuckGoSearchTool(), visit_webpage],
105
+ model=model,
106
+ max_steps=10,
107
+ name="web_search_agent",
108
+ description="Runs web searches for you and can visit webpages to extract information.",
109
+ )
110
+
111
+ # 2. Create time agent for timezone operations
112
+ time_agent = ToolCallingAgent(
113
+ tools=[get_current_time_in_timezone],
114
  model=model,
115
+ max_steps=3,
116
+ name="time_agent",
117
+ description="Handles time-related queries and timezone conversions.",
118
+ )
119
+
120
+ # 3. Create creative agent for image generation
121
+ creative_agent = ToolCallingAgent(
122
+ tools=[image_generation_tool, my_custom_tool],
123
+ model=model,
124
+ max_steps=5,
125
+ name="creative_agent",
126
+ description="Creates images and handles other creative tasks.",
127
+ )
128
+
129
+ # 4. Create manager agent (main agent)
130
+ manager_agent = CodeAgent(
131
+ model=model,
132
+ tools=[final_answer], # The manager can directly use the final_answer tool
133
+ managed_agents=[web_agent, time_agent, creative_agent], # Manages three specialized agents
134
  max_steps=6,
135
  verbosity_level=1,
136
+ additional_authorized_imports=["time", "numpy", "pandas"], # For data calculations
137
  grammar=None,
138
  planning_interval=None,
139
+ name="manager_agent",
140
+ description="Coordinates specialized agents to solve complex problems.",
141
  prompt_templates=prompt_templates
142
  )
143
 
144
+ # Initialize Gradio UI with the manager agent
145
+ GradioUI(manager_agent).launch()