xenchai commited on
Commit
8b50d03
·
verified ·
1 Parent(s): dbe6002

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -35
app.py CHANGED
@@ -1,63 +1,49 @@
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
  @tool
11
  def get_current_time_in_timezone(timezone: str) -> str:
12
- """A tool that fetches the current local time in a specified timezone.
13
- Args:
14
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
15
- """
16
  try:
17
- # Create timezone object
18
  tz = pytz.timezone(timezone)
19
- # Get current time in that timezone
20
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
21
  return f"The current local time in {timezone} is: {local_time}"
22
  except Exception as e:
23
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
24
-
 
25
  duckduckgo_search_tool = DuckDuckGoSearchTool()
26
-
27
- # Initialize the final answer tool
28
  final_answer = FinalAnswerTool()
 
29
 
30
- # Configure the language model using Hugging Face API
 
 
 
 
 
 
 
 
 
31
  model = HfApiModel(
32
  max_tokens=2096,
33
  temperature=0.5,
34
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
35
- custom_role_conversions=None,
36
  )
37
 
38
- # Import the image generation tool from Hub
39
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
40
-
41
- # Optionally, if you need a search tool, you could also add the DuckDuckGoSearchTool:
42
- # duckduckgo_search_tool = DuckDuckGoSearchTool()
43
-
44
- # Load system prompt templates from a YAML file
45
- with open("prompts.yaml", 'r') as stream:
46
- prompt_templates = yaml.safe_load(stream)
47
-
48
- # Create the CodeAgent with all available tools.
49
- # It is important to keep final_answer as part of the tools.
50
  agent = CodeAgent(
51
  model=model,
52
- tools=[final_answer, get_current_time_in_timezone, image_generation_tool],
53
  max_steps=6,
54
  verbosity_level=1,
55
- grammar=None,
56
- planning_interval=None,
57
- name=None,
58
- description=None,
59
- prompt_templates=prompt_templates # Pass system prompt to CodeAgent
60
  )
61
 
62
- # Launch the Gradio-based user interface for interactive use
63
- GradioUI(agent).launch()
 
 
1
  import datetime
 
2
  import pytz
3
  import yaml
4
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
5
  from tools.final_answer import FinalAnswerTool
 
6
  from Gradio_UI import GradioUI
7
 
8
  @tool
9
  def get_current_time_in_timezone(timezone: str) -> str:
10
+ """A tool that fetches the current local time in a specified timezone."""
 
 
 
11
  try:
 
12
  tz = pytz.timezone(timezone)
 
13
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
14
  return f"The current local time in {timezone} is: {local_time}"
15
  except Exception as e:
16
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
17
+
18
+ # Initialize tools
19
  duckduckgo_search_tool = DuckDuckGoSearchTool()
 
 
20
  final_answer = FinalAnswerTool()
21
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
22
 
23
+ # Load system prompt templates
24
+ try:
25
+ with open("prompts.yaml", 'r') as stream:
26
+ prompt_templates = yaml.safe_load(stream)
27
+ except FileNotFoundError:
28
+ raise FileNotFoundError("The file 'prompts.yaml' was not found.")
29
+ except yaml.YAMLError as e:
30
+ raise ValueError(f"Error parsing YAML file: {str(e)}")
31
+
32
+ # Configure the language model
33
  model = HfApiModel(
34
  max_tokens=2096,
35
  temperature=0.5,
36
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
 
37
  )
38
 
39
+ # Create the CodeAgent
 
 
 
 
 
 
 
 
 
 
 
40
  agent = CodeAgent(
41
  model=model,
42
+ tools=[final_answer, get_current_time_in_timezone, image_generation_tool, duckduckgo_search_tool],
43
  max_steps=6,
44
  verbosity_level=1,
45
+ prompt_templates=prompt_templates
 
 
 
 
46
  )
47
 
48
+ # Launch the Gradio-based user interface
49
+ GradioUI(agent).launch()