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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -31
app.py CHANGED
@@ -13,74 +13,61 @@ from smolagents import (
13
  from tools.final_answer import FinalAnswerTool
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:
21
  raise RuntimeError("HF_HUB_TOKEN env var not set")
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,
86
  tools=[
@@ -92,11 +79,8 @@ agent = CodeAgent(
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()
 
13
  from tools.final_answer import FinalAnswerTool
14
  from Gradio_UI import GradioUI
15
 
16
+ # 1) Authenticate with your HF token
 
 
17
  hf_token = os.getenv("HF_HUB_TOKEN")
18
  if not hf_token:
19
  raise RuntimeError("HF_HUB_TOKEN env var not set")
20
  login(hf_token)
21
 
22
+ # 2) Define tools with compliant docstrings
 
 
23
  @tool
24
  def my_custom_tool(arg1: str, arg2: int) -> str:
25
  """
26
+ Placeholder tool.
27
 
28
  Args:
29
  arg1: A sample string.
30
  arg2: A sample integer.
31
 
32
  Returns:
33
+ A placeholder response.
34
  """
35
  return "What magic will you build?"
36
 
37
  @tool
38
  def get_current_time_in_timezone(timezone: str) -> str:
39
  """
40
+ Fetch the current time in a specified timezone.
41
 
42
  Args:
43
+ timezone: A tz database name (e.g., 'Europe/Paris').
44
 
45
  Returns:
46
+ Local time as 'YYYY-MM-DD HH:MM:SS', or an error message.
47
  """
48
  try:
49
  tz = pytz.timezone(timezone)
50
  now = datetime.datetime.now(tz)
51
  return now.strftime("The current local time in %Z (%z) is %Y-%m-%d %H:%M:%S")
52
  except Exception as e:
53
+ return f"Error fetching time: {e}"
54
 
 
55
  final_answer = FinalAnswerTool()
56
 
57
+ # 3) Load the model WITHOUT an unsupported provider override
 
 
58
  model = InferenceClientModel(
59
  model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
60
  max_tokens=2096,
61
  temperature=0.5,
62
+ api_key=hf_token # ensure your token is passed here
63
+ # provider omitted to use default "auto"
64
  )
65
 
66
+ # 4) Load built-in tools
 
 
67
  image_gen = load_tool("agents-course/text-to-image", trust_remote_code=True)
68
  web_search = DuckDuckGoSearchTool()
69
 
70
+ # 5) Instantiate CodeAgent using default prompt templates
 
 
 
 
71
  agent = CodeAgent(
72
  model=model,
73
  tools=[
 
79
  ],
80
  max_steps=6,
81
  verbosity_level=1,
82
+ add_base_tools=False
 
83
  )
84
 
85
+ # 6) Launch Gradio UI
 
 
86
  GradioUI(agent).launch()