jcleee commited on
Commit
9d0aa0e
·
verified ·
1 Parent(s): 1707cf1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -56
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
3
  import requests
4
  import pytz
@@ -6,91 +6,84 @@ import yaml
6
  from tools.final_answer import FinalAnswerTool
7
  from langchain.text_splitter import CharacterTextSplitter
8
  from typing import List
 
9
  from Gradio_UI import GradioUI
10
 
 
11
  @tool
12
- def web_search(query: str) -> str:
13
- """Searches DuckDuckGo and returns top results as a string.
14
-
15
  Args:
16
- query (str): The search query.
17
-
18
- Returns:
19
- str: The search results concatenated into a string.
20
  """
21
  search_tool = DuckDuckGoSearchTool()
22
- results = search_tool(query)
23
  return "\n".join(results)
24
 
25
  @tool
26
- def visit_webpage(url: str) -> str:
27
- """Fetches the content of a web page.
28
-
29
  Args:
30
- url (str): The URL to visit.
31
-
32
- Returns:
33
- str: The raw text content of the page (first 5000 characters).
34
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  try:
36
  response = requests.get(url, timeout=5)
37
- return response.text[:5000]
38
  except Exception as e:
39
  return f"[ERROR fetching {url}]: {str(e)}"
40
 
41
  @tool
42
  def text_splitter(text: str) -> List[str]:
43
- """Splits text into chunks using langchain's CharacterTextSplitter.
44
-
45
  Args:
46
- text (str): The input text to split.
47
-
48
- Returns:
49
- List[str]: A list of text chunks.
50
  """
51
- splitter = CharacterTextSplitter(chunk_size=450, chunk_overlap=10)
52
- return splitter.split_text(text)
 
 
 
 
 
 
53
 
54
- @tool
55
- def get_current_time_in_timezone(timezone: str) -> str:
56
- """Gets the current time in a specified timezone.
57
 
58
- Args:
59
- timezone (str): A timezone string, like 'America/New_York'.
60
-
61
- Returns:
62
- str: The local time formatted as a string.
63
- """
64
- try:
65
- tz = pytz.timezone(timezone)
66
- return datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
67
- except Exception as e:
68
- return f"Error fetching time: {e}"
69
 
70
- # Load prompt templates
71
- with open("prompts.yaml", "r") as stream:
72
- prompt_templates = yaml.safe_load(stream)
73
 
74
- # Define the model
75
  model = HfApiModel(
76
- max_tokens=2096,
77
- temperature=0.5,
78
- model_id="meta-llama/Llama-3.2-3B-Instruct",
79
- last_input_token_count=0,
80
- last_output_token_count=0,
81
- custom_role_conversions=None,
82
  )
83
 
84
- # Load the final answer tool
85
- final_answer = FinalAnswerTool()
86
 
87
- # Optional additional tool
88
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
89
 
90
- # Create the agent
 
 
91
  agent = CodeAgent(
92
  model=model,
93
- tools=[final_answer, web_search, visit_webpage, text_splitter, image_generation_tool],
94
  max_steps=6,
95
  verbosity_level=1,
96
  grammar=None,
@@ -100,5 +93,5 @@ agent = CodeAgent(
100
  prompt_templates=prompt_templates
101
  )
102
 
103
- # Launch the UI
104
- GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
  import requests
4
  import pytz
 
6
  from tools.final_answer import FinalAnswerTool
7
  from langchain.text_splitter import CharacterTextSplitter
8
  from typing import List
9
+
10
  from Gradio_UI import GradioUI
11
 
12
+ # Below is an example of a tool that does nothing. Amaze us with your creativity !
13
  @tool
14
+ def web_search(query:str) -> str:
15
+ """Allows search through DuckDuckGo
 
16
  Args:
17
+ query: what you want to search
 
 
 
18
  """
19
  search_tool = DuckDuckGoSearchTool()
20
+ results = search_tool(query)
21
  return "\n".join(results)
22
 
23
  @tool
24
+ def get_current_time_in_timezone(timezone: str) -> str:
25
+ """A tool that fetches the current local time in a specified timezone.
 
26
  Args:
27
+ timezone: A string representing a valid timezone (e.g., 'America/New_York').
 
 
 
28
  """
29
+ try:
30
+ # Create timezone object
31
+ tz = pytz.timezone(timezone)
32
+ # Get current time in that timezone
33
+ local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
34
+ return f"The current local time in {timezone} is: {local_time}"
35
+ except Exception as e:
36
+ return f"Error fetching time for timezone '{timezone}': {str(e)}"
37
+
38
+ @tool
39
+ def visit_webpage(url: str) -> str:
40
+ """Fetches raw HTML content of a web page.
41
+ Args:
42
+ url: The url of the webpage"""
43
  try:
44
  response = requests.get(url, timeout=5)
45
+ return response.text[:5000] # limit output length
46
  except Exception as e:
47
  return f"[ERROR fetching {url}]: {str(e)}"
48
 
49
  @tool
50
  def text_splitter(text: str) -> List[str]:
51
+ """A tool that splits text by character.
 
52
  Args:
53
+ text: A string of text to be split.
 
 
 
54
  """
55
+ c_splitter = CharacterTextSplitter(
56
+ chunk_size=450,
57
+ chunk_overlap=10
58
+ )
59
+ split_text = c_splitter.split_text(text)
60
+ return split_text
61
+ #
62
+
63
 
 
 
 
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='meta-llama/Llama-3.2-3B-Instruct', #'Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
74
+ custom_role_conversions=None,
 
 
75
  )
76
 
 
 
77
 
78
+ # Import tool from Hub
79
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
80
 
81
+ with open("prompts.yaml", 'r') as stream:
82
+ prompt_templates = yaml.safe_load(stream)
83
+
84
  agent = CodeAgent(
85
  model=model,
86
+ tools=[final_answer, web_search, text_splitter, image_generation_tool],
87
  max_steps=6,
88
  verbosity_level=1,
89
  grammar=None,
 
93
  prompt_templates=prompt_templates
94
  )
95
 
96
+
97
+ GradioUI(agent).launch()