Matthightech commited on
Commit
d6481e4
·
verified ·
1 Parent(s): f7e00bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -36
app.py CHANGED
@@ -2,7 +2,6 @@ import os
2
  from huggingface_hub import login
3
  import datetime
4
  import pytz
5
- import yaml
6
 
7
  from smolagents import (
8
  CodeAgent,
@@ -15,7 +14,7 @@ from tools.final_answer import FinalAnswerTool
15
  from Gradio_UI import GradioUI
16
 
17
  # ——————————————————————————————————————————————
18
- # 1) Login to Hugging Face using your HF_HUB_TOKEN secret
19
  # ——————————————————————————————————————————————
20
  hf_token = os.getenv("HF_HUB_TOKEN")
21
  if not hf_token:
@@ -23,74 +22,64 @@ if not hf_token:
23
  login(hf_token)
24
 
25
  # ——————————————————————————————————————————————
26
- # 2) Define your custom tools
27
  # ——————————————————————————————————————————————
28
  @tool
29
  def my_custom_tool(arg1: str, arg2: int) -> str:
30
  """
31
- A tool that does nothing yet.
32
 
33
  Args:
34
- arg1: The first argument (a string).
35
- arg2: The second argument (an integer).
36
 
37
  Returns:
38
- A placeholder response.
39
  """
40
  return "What magic will you build?"
41
 
42
  @tool
43
  def get_current_time_in_timezone(timezone: str) -> str:
44
  """
45
- Fetch the current local time in a specified timezone.
46
 
47
  Args:
48
- timezone: A valid timezone name, e.g., 'America/New_York'.
49
 
50
  Returns:
51
- The current local time formatted as 'YYYY-MM-DD HH:MM:SS',
52
- or an error message if the timezone is invalid.
53
  """
54
  try:
55
  tz = pytz.timezone(timezone)
56
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
57
- return f"The current local time in {timezone} is: {local_time}"
58
  except Exception as e:
59
- return f"Error fetching time for timezone '{timezone}': {e}"
60
 
 
61
  final_answer = FinalAnswerTool()
62
 
63
  # ——————————————————————————————————————————————
64
- # 3) Load your model via InferenceClientModel
65
  # ——————————————————————————————————————————————
66
  model = InferenceClientModel(
67
  model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
68
  max_tokens=2096,
69
  temperature=0.5,
70
  api_key=hf_token,
71
- provider="huggingface"
72
  )
73
 
74
  # ——————————————————————————————————————————————
75
- # 4) Import additional tools from the Hub
76
  # ——————————————————————————————————————————————
77
- image_generation_tool = load_tool(
78
- "agents-course/text-to-image", trust_remote_code=True
79
- )
80
- duck_search = DuckDuckGoSearchTool()
81
-
82
- # ——————————————————————————————————————————————
83
- # 5) Load & sanitize your prompt templates
84
- # ——————————————————————————————————————————————
85
- with open("prompts.yaml", "r") as f:
86
- prompt_templates = yaml.safe_load(f)
87
- prompt_templates.setdefault(
88
- "final_answer",
89
- "Final answer:\n{{answer}}"
90
- )
91
 
92
  # ——————————————————————————————————————————————
93
- # 6) Instantiate the CodeAgent
 
 
94
  # ——————————————————————————————————————————————
95
  agent = CodeAgent(
96
  model=model,
@@ -98,15 +87,16 @@ agent = CodeAgent(
98
  final_answer,
99
  my_custom_tool,
100
  get_current_time_in_timezone,
101
- image_generation_tool,
102
- duck_search,
103
  ],
104
  max_steps=6,
105
  verbosity_level=1,
106
- prompt_templates=prompt_templates,
 
107
  )
108
 
109
  # ——————————————————————————————————————————————
110
- # 7) Launch the Gradio UI
111
  # ——————————————————————————————————————————————
112
  GradioUI(agent).launch()
 
2
  from huggingface_hub import login
3
  import datetime
4
  import pytz
 
5
 
6
  from smolagents import (
7
  CodeAgent,
 
14
  from Gradio_UI import GradioUI
15
 
16
  # ——————————————————————————————————————————————
17
+ # 1) Authenticate to HF
18
  # ——————————————————————————————————————————————
19
  hf_token = os.getenv("HF_HUB_TOKEN")
20
  if not hf_token:
 
22
  login(hf_token)
23
 
24
  # ——————————————————————————————————————————————
25
+ # 2) Custom tools (with proper Args/Returns sections)
26
  # ——————————————————————————————————————————————
27
  @tool
28
  def my_custom_tool(arg1: str, arg2: int) -> str:
29
  """
30
+ A placeholder tool for demonstration.
31
 
32
  Args:
33
+ arg1: A sample string.
34
+ arg2: A sample integer.
35
 
36
  Returns:
37
+ A placeholder response string.
38
  """
39
  return "What magic will you build?"
40
 
41
  @tool
42
  def get_current_time_in_timezone(timezone: str) -> str:
43
  """
44
+ Fetch the current time in a given timezone.
45
 
46
  Args:
47
+ timezone: A valid tz database name (e.g., 'Europe/Paris').
48
 
49
  Returns:
50
+ The local time formatted as 'YYYY-MM-DD HH:MM:SS', or an error message.
 
51
  """
52
  try:
53
  tz = pytz.timezone(timezone)
54
+ now = datetime.datetime.now(tz)
55
+ return now.strftime("The current local time in %Z (%z) is %Y-%m-%d %H:%M:%S")
56
  except Exception as e:
57
+ return f"Error: {e}"
58
 
59
+ # the required final answer tool
60
  final_answer = FinalAnswerTool()
61
 
62
  # ——————————————————————————————————————————————
63
+ # 3) Load the model (current class name)
64
  # ——————————————————————————————————————————————
65
  model = InferenceClientModel(
66
  model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
67
  max_tokens=2096,
68
  temperature=0.5,
69
  api_key=hf_token,
70
+ provider="huggingface" # bypasses any external routing errors
71
  )
72
 
73
  # ——————————————————————————————————————————————
74
+ # 4) Load Hub-provided tools
75
  # ——————————————————————————————————————————————
76
+ image_gen = load_tool("agents-course/text-to-image", trust_remote_code=True)
77
+ web_search = DuckDuckGoSearchTool()
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  # ——————————————————————————————————————————————
80
+ # 5) Instantiate agent *without* custom prompt_templates
81
+ # → uses the library’s defaults, so you’ll never hit:
82
+ # AttributeError: 'str' object has no attribute 'keys'
83
  # ——————————————————————————————————————————————
84
  agent = CodeAgent(
85
  model=model,
 
87
  final_answer,
88
  my_custom_tool,
89
  get_current_time_in_timezone,
90
+ image_gen,
91
+ web_search,
92
  ],
93
  max_steps=6,
94
  verbosity_level=1,
95
+ add_base_tools=False, # keep it minimal; you’ve already added web_search
96
+ # note: no `prompt_templates=` here :contentReference[oaicite:0]{index=0}
97
  )
98
 
99
  # ——————————————————————————————————————————————
100
+ # 6) Launch your Gradio interface
101
  # ——————————————————————————————————————————————
102
  GradioUI(agent).launch()