umair1ali2 commited on
Commit
de9f892
·
verified ·
1 Parent(s): 280574f

update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -30
app.py CHANGED
@@ -4,24 +4,25 @@ import pytz
4
  import yaml
5
  from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
6
 
7
- # --- STEP 1: CONFIGURATION & MOCK FILES ---
8
- # This creates a prompts.yaml so the script doesn't crash
 
 
 
 
9
  prompt_content = {
10
- "system_prompt": "You are a helpful AI agent. Answer the question using tools if necessary."
 
 
 
 
 
11
  }
 
12
  with open("prompts.yaml", "w") as f:
13
  yaml.dump(prompt_content, f)
14
 
15
- # --- STEP 2: DEFINE YOUR TOOLS ---
16
-
17
- @tool
18
- def my_custom_tool(arg1: str, arg2: int) -> str:
19
- """A tool that does nothing yet
20
- Args:
21
- arg1: the first argument
22
- arg2: the second argument
23
- """
24
- return f"Magic confirmed with {arg1} and {arg2}!"
25
 
26
  @tool
27
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -30,48 +31,72 @@ def get_current_time_in_timezone(timezone: str) -> str:
30
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
31
  """
32
  try:
 
 
 
 
 
 
33
  tz = pytz.timezone(timezone)
34
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
35
  return f"The current local time in {timezone} is: {local_time}"
36
  except Exception as e:
37
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
38
 
39
- # --- STEP 3: INITIALIZE MODEL & AGENT ---
 
 
 
 
 
 
 
 
 
40
 
41
- # Use HfApiModel (The updated name for InferenceClientModel)
42
  model = HfApiModel(
43
  max_tokens=2096,
44
  temperature=0.5,
45
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
46
  )
47
 
48
- # Load the image tool from the Hub
49
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
50
 
51
- # Load the prompt template
52
  with open("prompts.yaml", 'r') as stream:
53
  prompt_templates = yaml.safe_load(stream)
54
 
55
- # Create the Agent
 
56
  agent = CodeAgent(
57
  model=model,
58
  tools=[
59
  get_current_time_in_timezone,
60
- my_custom_tool,
61
- image_generation_tool,
62
- DuckDuckGoSearchTool()
63
  ],
64
  max_steps=6,
65
  verbosity_level=1,
66
  prompt_templates=prompt_templates
67
  )
68
 
69
- # --- STEP 4: RUNNING THE AGENT ---
70
 
71
- # If you are in the Hugging Face Space/Course environment, use:
72
- # from Gradio_UI import GradioUI
73
- # GradioUI(agent).launch()
 
 
 
 
 
 
 
 
74
 
75
- # If you are testing in a standard Python script/Colab:
76
- print("--- Starting Agent Test ---")
77
- agent.run("What is the current time in New York and then create an image of a futuristic clock.")
 
4
  import yaml
5
  from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
6
 
7
+ # --- STEP 1: SECURITY & CONFIGURATION ---
8
+ # Replace with your NEW token.
9
+ # Best practice: Set this in your system environment variables as HF_TOKEN
10
+ os.environ["HF_TOKEN"] = "PASTE_YOUR_NEW_TOKEN_HERE"
11
+
12
+ # Creating a proper prompts.yaml to guide the agent's logic
13
  prompt_content = {
14
+ "system_prompt": (
15
+ "You are an expert AI assistant that can use python code to solve tasks. "
16
+ "You have access to tools. For every step, explain your thought process in 'Thought:', "
17
+ "then write the code in a 'Code:' block. Once you have the final answer, "
18
+ "provide it clearly."
19
+ )
20
  }
21
+
22
  with open("prompts.yaml", "w") as f:
23
  yaml.dump(prompt_content, f)
24
 
25
+ # --- STEP 2: DEFINE TOOLS ---
 
 
 
 
 
 
 
 
 
26
 
27
  @tool
28
  def get_current_time_in_timezone(timezone: str) -> str:
 
31
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
32
  """
33
  try:
34
+ # Normalize common inputs
35
+ if timezone.lower() == "new york":
36
+ timezone = "America/New_York"
37
+ elif timezone.lower() == "london":
38
+ timezone = "Europe/London"
39
+
40
  tz = pytz.timezone(timezone)
41
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
42
  return f"The current local time in {timezone} is: {local_time}"
43
  except Exception as e:
44
+ return f"Error: '{timezone}' is not a recognized timezone. Please use format like 'Europe/Paris'."
45
 
46
+ @tool
47
+ def my_custom_tool(arg1: str, arg2: int) -> str:
48
+ """A placeholder tool for future custom logic.
49
+ Args:
50
+ arg1: A descriptive string.
51
+ arg2: An integer multiplier or count.
52
+ """
53
+ return f"Logic processed: {arg1} received with value {arg2}."
54
+
55
+ # --- STEP 3: INITIALIZE MODEL & TOOLS ---
56
 
57
+ # Use a highly available model. If Qwen 32B gives a 403, try 'meta-llama/Llama-3.3-70B-Instruct'
58
  model = HfApiModel(
59
  max_tokens=2096,
60
  temperature=0.5,
61
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
62
  )
63
 
64
+ # Load the image generation tool
65
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
66
 
67
+ # Load the prompt template from the file we just created
68
  with open("prompts.yaml", 'r') as stream:
69
  prompt_templates = yaml.safe_load(stream)
70
 
71
+ # --- STEP 4: INITIALIZE AGENT ---
72
+
73
  agent = CodeAgent(
74
  model=model,
75
  tools=[
76
  get_current_time_in_timezone,
77
+ DuckDuckGoSearchTool(),
78
+ image_generation_tool,
79
+ my_custom_tool
80
  ],
81
  max_steps=6,
82
  verbosity_level=1,
83
  prompt_templates=prompt_templates
84
  )
85
 
86
+ # --- STEP 5: EXECUTION ---
87
 
88
+ if __name__ == "__main__":
89
+ print("--- Starting Agent Test ---")
90
+
91
+ task = "What is the current time in New York? After you find the time, generate a beautiful image of a clock that reflects the style of that city."
92
+
93
+ try:
94
+ result = agent.run(task)
95
+ print("\n--- FINAL ANSWER ---")
96
+ print(result)
97
+ except Exception as e:
98
+ print(f"\nAn error occurred during execution: {e}")
99
 
100
+ # Optional: If you want to launch a UI (requires 'gradio' installed)
101
+ # from Gradio_UI import GradioUI
102
+ # GradioUI(agent).launch()