jcleee commited on
Commit
80ca425
·
verified ·
1 Parent(s): bbfcb87

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -55
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,84 +6,63 @@ import yaml
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,5 +72,5 @@ agent = CodeAgent(
93
  prompt_templates=prompt_templates
94
  )
95
 
96
-
97
- 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
  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
  search_tool = DuckDuckGoSearchTool()
15
+ results = search_tool(query)
16
  return "\n".join(results)
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  @tool
19
  def visit_webpage(url: str) -> str:
20
+ """Fetches the content of a web page."""
 
 
21
  try:
22
  response = requests.get(url, timeout=5)
23
+ return response.text[:5000]
24
  except Exception as e:
25
  return f"[ERROR fetching {url}]: {str(e)}"
26
 
27
  @tool
28
  def text_splitter(text: str) -> List[str]:
29
+ """Splits text into chunks using langchain's CharacterTextSplitter."""
30
+ splitter = CharacterTextSplitter(chunk_size=450, chunk_overlap=10)
31
+ return splitter.split_text(text)
 
 
 
 
 
 
 
 
 
32
 
33
+ @tool
34
+ def get_current_time_in_timezone(timezone: str) -> str:
35
+ """Gets current time in a given timezone."""
36
+ try:
37
+ tz = pytz.timezone(timezone)
38
+ return datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
39
+ except Exception as e:
40
+ return f"Error fetching time: {e}"
41
 
42
+ # Load prompts.yaml
43
+ with open("prompts.yaml", "r") as stream:
44
+ prompt_templates = yaml.safe_load(stream)
 
45
 
46
+ # Define model
47
  model = HfApiModel(
48
+ max_tokens=2096,
49
+ temperature=0.5,
50
+ model_id="meta-llama/Llama-3.2-3B-Instruct",
51
+ last_input_token_count=0,
52
+ last_output_token_count=0,
53
+ custom_role_conversions=None,
54
  )
55
 
56
+ # Required final answer tool
57
+ final_answer = FinalAnswerTool()
58
 
59
+ # Optional additional tool
60
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
61
 
62
+ # Build agent
 
 
63
  agent = CodeAgent(
64
  model=model,
65
+ tools=[final_answer, web_search, visit_webpage, text_splitter, image_generation_tool],
66
  max_steps=6,
67
  verbosity_level=1,
68
  grammar=None,
 
72
  prompt_templates=prompt_templates
73
  )
74
 
75
+ # Launch UI with visible errors
76
+ GradioUI(agent).launch(show_error=True)