ashishja commited on
Commit
92cbc2f
·
verified ·
1 Parent(s): 5064e4b

fixed bugs

Browse files
Files changed (1) hide show
  1. app.py +34 -27
app.py CHANGED
@@ -1,22 +1,19 @@
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
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
- @tool
12
- def my_cutom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
15
- Args:
16
- arg1: the first argument
17
- arg2: the second argument
18
- """
19
- return "What magic will you build ?"
20
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -25,41 +22,51 @@ def get_current_time_in_timezone(timezone: str) -> str:
25
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
26
  """
27
  try:
28
- # Create timezone object
29
  tz = pytz.timezone(timezone)
30
- # Get current time in that timezone
31
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
32
  return f"The current local time in {timezone} is: {local_time}"
 
 
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
 
37
  final_answer = FinalAnswerTool()
 
 
38
  model = HfApiModel(
39
- max_tokens=2096,
40
- temperature=0.5,
41
- model_id='meta-llama/Llama-3.2-1B-Instruct',
42
- custom_role_conversions=None,
 
 
 
 
 
43
  )
44
 
45
 
46
- # Import tool from Hub
47
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
 
 
48
 
49
  with open("prompts.yaml", 'r') as stream:
50
  prompt_templates = yaml.safe_load(stream)
51
-
52
  agent = CodeAgent(
53
  model=model,
54
- tools=[final_answer,get_current_time_in_timezone,image_generation_tool], ## add your tools here (don't remove final answer)
55
  max_steps=6,
56
  verbosity_level=1,
57
  grammar=None,
58
  planning_interval=None,
59
- name=None,
60
- description=None,
61
- prompt_templates=prompt_templates
62
  )
63
 
64
 
65
- GradioUI(agent).launch()
 
 
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 # Assuming this is in a 'tools' directory
7
+ from tools.final_answer import FinalAnswerTool # <--- Correct relative import
8
 
9
+ # For Gradio (assuming you have a GradioUI class defined)
10
+ from Gradio_UI import GradioUI # Or wherever you have GradioUI defined
11
+ import os
12
+
13
+ # Set your Hugging Face API token (replace with your actual token)
14
+ # It's BEST to set this as an environment variable:
15
+ os.environ["HUGGINGFACEHUB_API_TOKEN"] = "hf_YOUR_ACTUAL_API_KEY" # Replace!
16
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  @tool
19
  def get_current_time_in_timezone(timezone: str) -> str:
 
22
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
23
  """
24
  try:
 
25
  tz = pytz.timezone(timezone)
 
26
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
27
  return f"The current local time in {timezone} is: {local_time}"
28
+ except pytz.exceptions.UnknownTimeZoneError:
29
+ return f"Error: Unknown timezone '{timezone}'. Please provide a valid timezone."
30
  except Exception as e:
31
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
32
 
33
 
34
  final_answer = FinalAnswerTool()
35
+
36
+ # Use a valid and accessible Llama 3 model ID
37
  model = HfApiModel(
38
+ max_tokens=2096,
39
+ temperature=0.5,
40
+ model_id='meta-llama/Llama-3.2-1B-Instruct', # Corrected Model ID
41
+ # api_key="hf_YOUR_API_KEY", # Use environment variable instead
42
+ custom_role_conversions={ # Correct role conversions for Llama 3
43
+ "system": "<|start_header_id|>system<|end_header_id|>\n\n{content}<|eot_id|>",
44
+ "user": "<|start_header_id|>user<|end_header_id|>\n\n{content}<|eot_id|>",
45
+ "assistant": "<|start_header_id|>assistant<|end_header_id|>\n\n{content}<|eot_id|>",
46
+ },
47
  )
48
 
49
 
50
+ # --- Safer Image Handling (DuckDuckGo Search) ---
51
+ ddg_search = DuckDuckGoSearchTool()
52
+
53
+
54
 
55
  with open("prompts.yaml", 'r') as stream:
56
  prompt_templates = yaml.safe_load(stream)
57
+
58
  agent = CodeAgent(
59
  model=model,
60
+ tools=[final_answer, get_current_time_in_timezone, ddg_search], # Use DDG instead of untrusted remote code
61
  max_steps=6,
62
  verbosity_level=1,
63
  grammar=None,
64
  planning_interval=None,
65
+ name="Time and Image Agent", # Give your agent a name
66
+ description="An agent that can tell you the time and search for images.", # And a description
67
+ prompt_templates=prompt_templates,
68
  )
69
 
70
 
71
+ # --- Launch Gradio UI ---
72
+ GradioUI(agent).launch(debug=True) # Add debug=True for easier debugging