callmyname commited on
Commit
590eaf7
·
verified ·
1 Parent(s): f143ec9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -33
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  try:
2
  from smolagents import InferenceClientModel as ModelClass
3
  except ImportError:
@@ -6,15 +7,10 @@ except ImportError:
6
  except ImportError:
7
  from smolagents import HfEngine as ModelClass
8
 
9
-
10
-
11
- from smolagents import CodeAgent, DuckDuckGoSearchTool, load_tool, tool
12
  import datetime
13
- import requests
14
  import pytz
15
  import yaml
16
  from tools.final_answer import FinalAnswerTool
17
-
18
  from Gradio_UI import GradioUI
19
 
20
  # Weather search tool using DuckDuckGo
@@ -29,20 +25,13 @@ def get_weather_info(city: str) -> str:
29
  A string containing weather information for the specified city
30
  """
31
  try:
32
- # Initialize DuckDuckGo search tool
33
  search_tool = DuckDuckGoSearchTool()
34
-
35
- # Create weather-specific search query
36
  search_query = f"current weather {city} temperature conditions forecast"
37
-
38
- # Perform search
39
  search_results = search_tool(search_query)
40
 
41
- # Extract weather information from results
42
  if search_results and len(search_results) > 0:
43
- # Combine relevant snippets
44
  weather_info = f"Weather information for {city}:\n"
45
- for i, result in enumerate(search_results[:3]): # Take top 3 results
46
  if 'snippet' in result:
47
  weather_info += f"\n{i+1}. {result['snippet']}"
48
  return weather_info
@@ -59,9 +48,7 @@ def get_current_time_in_timezone(timezone: str) -> str:
59
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
60
  """
61
  try:
62
- # Create timezone object
63
  tz = pytz.timezone(timezone)
64
- # Get current time in that timezone
65
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
66
  return f"The current local time in {timezone} is: {local_time}"
67
  except Exception as e:
@@ -72,31 +59,43 @@ final_answer = FinalAnswerTool()
72
 
73
  # Initialize model
74
  model = ModelClass(
75
- max_tokens=2096,
76
- temperature=0.5,
77
  model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
78
- custom_role_conversions=None,
79
  )
80
 
81
  # Import tool from Hub
82
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
83
 
84
- # Load system prompt from prompt.yaml file
85
- with open("prompts.yaml", 'r') as stream:
86
- prompt_templates = yaml.safe_load(stream)
 
 
87
 
88
- # Create agent with all tools including weather search
89
- agent = CodeAgent(
90
- model=model,
91
- tools=[get_weather_info, get_current_time_in_timezone, image_generation_tool, final_answer],
92
- max_steps=6,
93
- verbosity_level=1,
94
- grammar=None,
95
- planning_interval=None,
96
- name=None,
97
- description=None,
98
- prompt_templates=prompt_templates
99
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
  # Launch Gradio UI
102
  GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, load_tool, tool
2
  try:
3
  from smolagents import InferenceClientModel as ModelClass
4
  except ImportError:
 
7
  except ImportError:
8
  from smolagents import HfEngine as ModelClass
9
 
 
 
 
10
  import datetime
 
11
  import pytz
12
  import yaml
13
  from tools.final_answer import FinalAnswerTool
 
14
  from Gradio_UI import GradioUI
15
 
16
  # Weather search tool using DuckDuckGo
 
25
  A string containing weather information for the specified city
26
  """
27
  try:
 
28
  search_tool = DuckDuckGoSearchTool()
 
 
29
  search_query = f"current weather {city} temperature conditions forecast"
 
 
30
  search_results = search_tool(search_query)
31
 
 
32
  if search_results and len(search_results) > 0:
 
33
  weather_info = f"Weather information for {city}:\n"
34
+ for i, result in enumerate(search_results[:3]):
35
  if 'snippet' in result:
36
  weather_info += f"\n{i+1}. {result['snippet']}"
37
  return weather_info
 
48
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
49
  """
50
  try:
 
51
  tz = pytz.timezone(timezone)
 
52
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
53
  return f"The current local time in {timezone} is: {local_time}"
54
  except Exception as e:
 
59
 
60
  # Initialize model
61
  model = ModelClass(
 
 
62
  model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
 
63
  )
64
 
65
  # Import tool from Hub
66
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
67
 
68
+ # Handle prompt templates
69
+ try:
70
+ # Try to load custom prompts
71
+ with open("prompts.yaml", 'r') as stream:
72
+ prompt_templates = yaml.safe_load(stream)
73
 
74
+ # Ensure all required keys are present
75
+ if hasattr(CodeAgent, 'DEFAULT_PROMPTS'):
76
+ for key in CodeAgent.DEFAULT_PROMPTS:
77
+ if key not in prompt_templates:
78
+ prompt_templates[key] = CodeAgent.DEFAULT_PROMPTS[key]
79
+
80
+ # Create agent with custom prompts
81
+ agent = CodeAgent(
82
+ model=model,
83
+ tools=[final_answer, get_weather_info, get_current_time_in_timezone, image_generation_tool],
84
+ max_steps=6,
85
+ verbosity_level=1,
86
+ prompt_templates=prompt_templates
87
+ )
88
+ except Exception as e:
89
+ print(f"Error loading custom prompts: {e}")
90
+ print("Using default prompts instead")
91
+
92
+ # Create agent without custom prompts
93
+ agent = CodeAgent(
94
+ model=model,
95
+ tools=[get_weather_info, get_current_time_in_timezone, image_generation_tool, final_answer],
96
+ max_steps=6,
97
+ verbosity_level=1
98
+ )
99
 
100
  # Launch Gradio UI
101
  GradioUI(agent).launch()