MallikarjunSonna commited on
Commit
6dee0c8
·
verified ·
1 Parent(s): 3151e4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -12
app.py CHANGED
@@ -1,7 +1,7 @@
1
- from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
3
  import pytz
4
  import yaml
 
5
  from tools.final_answer import FinalAnswerTool
6
  from Gradio_UI import GradioUI
7
 
@@ -10,10 +10,10 @@ from Gradio_UI import GradioUI
10
  def get_current_time_in_timezone(timezone: str) -> str:
11
  """
12
  Fetches the current local time in a specified timezone.
13
-
14
  Args:
15
  timezone (str): The timezone string (e.g., 'America/New_York', 'Asia/Kolkata').
16
-
17
  Returns:
18
  str: The current local time in the specified timezone.
19
  """
@@ -21,6 +21,8 @@ def get_current_time_in_timezone(timezone: str) -> str:
21
  tz = pytz.timezone(timezone)
22
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
23
  return f"The current local time in {timezone} is: {local_time}"
 
 
24
  except Exception as e:
25
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
26
 
@@ -35,22 +37,26 @@ model = HfApiModel(
35
  custom_role_conversions=None,
36
  )
37
 
38
- # Import an image generation tool from Hugging Face Hub
39
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
40
 
41
- # Load prompt templates
42
- with open("prompts.yaml", 'r') as stream:
43
- prompt_templates = yaml.safe_load(stream)
 
 
 
 
44
 
45
  # Create the Agent with properly defined tools
46
  agent = CodeAgent(
47
  model=model,
48
  tools=[
49
- final_answer,
50
- DuckDuckGoSearchTool(),
51
- get_current_time_in_timezone, # Now properly formatted
52
  image_generation_tool
53
- ],
54
  max_steps=6,
55
  verbosity_level=1,
56
  grammar=None,
@@ -61,4 +67,5 @@ agent = CodeAgent(
61
  )
62
 
63
  # Launch UI
64
- 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
 
 
10
  def get_current_time_in_timezone(timezone: str) -> str:
11
  """
12
  Fetches the current local time in a specified timezone.
13
+
14
  Args:
15
  timezone (str): The timezone string (e.g., 'America/New_York', 'Asia/Kolkata').
16
+
17
  Returns:
18
  str: The current local time in the specified timezone.
19
  """
 
21
  tz = pytz.timezone(timezone)
22
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
23
  return f"The current local time in {timezone} is: {local_time}"
24
+ except pytz.UnknownTimeZoneError:
25
+ return f"Error: Invalid timezone '{timezone}'. Please use a valid timezone."
26
  except Exception as e:
27
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
28
 
 
37
  custom_role_conversions=None,
38
  )
39
 
40
+ # Load Hugging Face's image generation tool
41
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
42
 
43
+ # Load prompt templates from YAML
44
+ try:
45
+ with open("prompts.yaml", 'r') as stream:
46
+ prompt_templates = yaml.safe_load(stream)
47
+ except FileNotFoundError:
48
+ prompt_templates = {}
49
+ print("Warning: prompts.yaml not found. Using empty prompt templates.")
50
 
51
  # Create the Agent with properly defined tools
52
  agent = CodeAgent(
53
  model=model,
54
  tools=[
55
+ final_answer,
56
+ DuckDuckGoSearchTool(),
57
+ get_current_time_in_timezone,
58
  image_generation_tool
59
+ ],
60
  max_steps=6,
61
  verbosity_level=1,
62
  grammar=None,
 
67
  )
68
 
69
  # Launch UI
70
+ if __name__ == "__main__":
71
+ GradioUI(agent).launch()