happyseas commited on
Commit
7271451
·
verified ·
1 Parent(s): 4df4ea0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -26
app.py CHANGED
@@ -1,36 +1,47 @@
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
 
11
  @tool
12
- def translate_to_spanish (english : str)-> str:
13
  """A tool that translates text from english to spanish using DuckDuckGoSearchTool
14
  Args:
15
  english (str): the text in english to be translated to spanish
16
  Returns:
17
  str: the text translated into spanish
18
-
19
  """
20
- query = "Please translate the following text into Spanish {english}"
21
-
22
- # Use a temporary agent for the search (reuse model for efficiency)
 
23
  temp_agent = CodeAgent(
24
  tools=[DuckDuckGoSearchTool()],
25
  model=model
26
  )
27
  results = temp_agent.run(query)
28
 
 
 
 
29
  @tool
30
  def get_current_time_in_timezone(timezone: str) -> str:
31
  """A tool that fetches the current local time in a specified timezone.
32
  Args:
33
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
 
 
34
  """
35
  try:
36
  # Create timezone object
@@ -41,29 +52,35 @@ def get_current_time_in_timezone(timezone: str) -> str:
41
  except Exception as e:
42
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
43
 
44
-
45
  final_answer = FinalAnswerTool()
46
 
47
- # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
48
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
49
-
50
- model = HfApiModel(
51
- max_tokens=2096,
52
- temperature=0.5,
53
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
54
- custom_role_conversions=None,
55
- )
 
56
 
 
 
 
 
 
 
 
 
 
 
57
 
58
- # Import tool from Hub
59
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
60
-
61
- with open("prompts.yaml", 'r') as stream:
62
- prompt_templates = yaml.safe_load(stream)
63
-
64
  agent = CodeAgent(
65
  model=model,
66
- tools=[final_answer, translate_to_spanish, get_current_time_in_timezone, DuckDuckGoSearchTool(), image_generation_tool,], ## add your tools here (don't remove final answer)
67
  max_steps=6,
68
  verbosity_level=1,
69
  grammar=None,
@@ -73,5 +90,9 @@ agent = CodeAgent(
73
  prompt_templates=prompt_templates
74
  )
75
 
76
-
77
- 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
 
7
  from Gradio_UI import GradioUI
8
 
9
+ # IMPORTANT: Initialize model BEFORE using it in tools
10
+ model = HfApiModel(
11
+ max_tokens=2096,
12
+ temperature=0.5,
13
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded
14
+ custom_role_conversions=None,
15
+ )
16
 
17
  @tool
18
+ def translate_to_spanish(english: str) -> str:
19
  """A tool that translates text from english to spanish using DuckDuckGoSearchTool
20
  Args:
21
  english (str): the text in english to be translated to spanish
22
  Returns:
23
  str: the text translated into spanish
 
24
  """
25
+ # FIXED: Proper f-string formatting and return statement
26
+ query = f"Please translate the following text into Spanish: {english}"
27
+
28
+ # Use a temporary agent for the search (reuse model for efficiency)
29
  temp_agent = CodeAgent(
30
  tools=[DuckDuckGoSearchTool()],
31
  model=model
32
  )
33
  results = temp_agent.run(query)
34
 
35
+ # FIXED: Actually return the results
36
+ return str(results)
37
+
38
  @tool
39
  def get_current_time_in_timezone(timezone: str) -> str:
40
  """A tool that fetches the current local time in a specified timezone.
41
  Args:
42
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
43
+ Returns:
44
+ str: The current time in the specified timezone
45
  """
46
  try:
47
  # Create timezone object
 
52
  except Exception as e:
53
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
54
 
55
+ # Initialize tools
56
  final_answer = FinalAnswerTool()
57
 
58
+ # FIXED: Added error handling for optional dependencies
59
+ try:
60
+ # Import tool from Hub
61
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
62
+ tools_list = [final_answer, translate_to_spanish, get_current_time_in_timezone,
63
+ DuckDuckGoSearchTool(), image_generation_tool]
64
+ except Exception as e:
65
+ print(f"Warning: Could not load image generation tool: {e}")
66
+ tools_list = [final_answer, translate_to_spanish, get_current_time_in_timezone,
67
+ DuckDuckGoSearchTool()]
68
 
69
+ # FIXED: Added error handling for prompts.yaml
70
+ try:
71
+ with open("prompts.yaml", 'r') as stream:
72
+ prompt_templates = yaml.safe_load(stream)
73
+ except FileNotFoundError:
74
+ print("Warning: prompts.yaml not found, using default templates")
75
+ prompt_templates = None
76
+ except Exception as e:
77
+ print(f"Warning: Error loading prompts.yaml: {e}")
78
+ prompt_templates = None
79
 
80
+ # Create agent
 
 
 
 
 
81
  agent = CodeAgent(
82
  model=model,
83
+ tools=tools_list, # Use the dynamically created tools list
84
  max_steps=6,
85
  verbosity_level=1,
86
  grammar=None,
 
90
  prompt_templates=prompt_templates
91
  )
92
 
93
+ # FIXED: Added error handling for Gradio launch
94
+ try:
95
+ GradioUI(agent).launch()
96
+ except Exception as e:
97
+ print(f"Error launching Gradio UI: {e}")
98
+ print("You may need to install gradio or check your Gradio_UI module")