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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -26
app.py CHANGED
@@ -1,34 +1,33 @@
1
  import os
2
  import datetime
3
- import requests
4
  import pytz
5
  import yaml
6
- from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, load_tool, tool
7
 
8
- # --- STEP 1: CREATE NECESSARY FILES (For Colab/VS Code safety) ---
9
- # This creates a basic prompts.yaml if it doesn't exist
10
  prompt_content = {
11
- "system_prompt": "You are a helpful agent that can use tools to answer questions. Solve the task step by step."
12
  }
13
  with open("prompts.yaml", "w") as f:
14
  yaml.dump(prompt_content, f)
15
 
16
- # --- STEP 2: DEFINE TOOLS ---
17
 
18
  @tool
19
  def my_custom_tool(arg1: str, arg2: int) -> str:
20
- """A tool that returns a creative message combining a string and an integer.
21
  Args:
22
- arg1: A descriptive string or name
23
- arg2: A lucky number or quantity
24
  """
25
- return f"Magic is happening! You provided '{arg1}' and the number {arg2}. Your agent is ready!"
26
 
27
  @tool
28
  def get_current_time_in_timezone(timezone: str) -> str:
29
  """A tool that fetches the current local time in a specified timezone.
30
  Args:
31
- timezone: A string representing a valid timezone (e.g., 'America/New_York' or 'Europe/London').
32
  """
33
  try:
34
  tz = pytz.timezone(timezone)
@@ -37,43 +36,42 @@ def get_current_time_in_timezone(timezone: str) -> str:
37
  except Exception as e:
38
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
39
 
40
- # --- STEP 3: SETUP MODEL AND AGENT ---
41
 
42
- # If you have an HF_TOKEN in your env variables, it will be picked up automatically
43
- model = InferenceClientModel(
44
  max_tokens=2096,
45
  temperature=0.5,
46
  model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
47
  )
48
 
49
- # Load external tools
50
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
51
- search_tool = DuckDuckGoSearchTool()
52
 
53
- # Load system prompt from the file we created
54
  with open("prompts.yaml", 'r') as stream:
55
  prompt_templates = yaml.safe_load(stream)
56
 
57
- # Create the Agent with ALL tools included
58
  agent = CodeAgent(
59
  model=model,
60
  tools=[
61
  get_current_time_in_timezone,
62
  my_custom_tool,
63
  image_generation_tool,
64
- search_tool
65
  ],
66
  max_steps=6,
67
  verbosity_level=1,
68
  prompt_templates=prompt_templates
69
  )
70
 
71
- # --- STEP 4: RUN THE AGENT ---
72
 
73
- # In VS Code/Local: This will open a browser window
74
- # In Colab: This will show the UI in the cell output
75
- # Note: Since Gradio_UI is a custom local file in the course,
76
- # we use the standard agent.run() or a basic Gradio interface if that file is missing.
77
 
78
- print("Agent is initialized. You can now ask questions like: 'What time is it in Tokyo?'")
79
- agent.run("What is the current time in Paris?")
 
 
1
  import os
2
  import datetime
 
3
  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:
28
  """A tool that fetches the current local time in a specified timezone.
29
  Args:
30
+ timezone: A string representing a valid timezone (e.g., 'America/New_York').
31
  """
32
  try:
33
  tz = pytz.timezone(timezone)
 
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.")