geraldaton20 commited on
Commit
b844589
·
verified ·
1 Parent(s): 77d5211

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -23
app.py CHANGED
@@ -1,26 +1,25 @@
1
- from smolagents import CodeAgent, HfApiModel, load_tool, tool
2
- from tools.final_answer import FinalAnswerTool
 
 
3
  import yaml
4
- from Gradio_UI import GradioUI
5
-
6
- # Load the image generation tool from the Hugging Face Hub
7
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
8
-
9
- # Define the final answer tool
10
- final_answer = FinalAnswerTool()
11
-
12
- # (Optional) Load prompt templates
13
- with open("prompts.yaml", 'r') as stream:
14
- prompt_templates = yaml.safe_load(stream)
15
 
16
- # Load the language model
17
- model = HfApiModel(
18
- max_tokens=2048,
19
- temperature=0.5,
20
- model_id="Qwen/Qwen2.5-Coder-32B-Instruct"
21
- )
22
 
23
- # Wrap the image generation function as a tool
 
 
 
 
 
 
 
 
 
 
 
 
24
  @tool
25
  def generate_image(prompt: str) -> str:
26
  """Generate an image from a text prompt using the image generation tool.
@@ -33,14 +32,54 @@ def generate_image(prompt: str) -> str:
33
  except Exception as e:
34
  return f"Image generation failed: {str(e)}"
35
 
36
- # Define the agent with the tools
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  agent = CodeAgent(
38
  model=model,
39
- tools=[final_answer, generate_image],
 
40
  max_steps=6,
41
  verbosity_level=1,
 
 
 
 
42
  prompt_templates=prompt_templates
43
  )
44
 
45
- # Launch the UI
46
  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
 
8
+ from Gradio_UI import GradioUI
 
 
 
 
 
9
 
10
+ # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
+ @tool
12
+ def search_duckduckgo(query: str) -> str:
13
+ """Performs a web search using DuckDuckGo for real-time information.
14
+
15
+ Args:
16
+ query: A search phrase or question (e.g., 'latest AI research news').
17
+ """
18
+ try:
19
+ return duckduckgo(query)
20
+ except Exception as e:
21
+ return f"Search failed: {str(e)}"
22
+
23
  @tool
24
  def generate_image(prompt: str) -> str:
25
  """Generate an image from a text prompt using the image generation tool.
 
32
  except Exception as e:
33
  return f"Image generation failed: {str(e)}"
34
 
35
+ @tool
36
+ def get_current_time_in_timezone(timezone: str) -> str:
37
+ """A tool that fetches the current local time in a specified timezone.
38
+ Args:
39
+ timezone: A string representing a valid timezone (e.g., 'America/New_York').
40
+ """
41
+ try:
42
+ # Create timezone object
43
+ tz = pytz.timezone(timezone)
44
+ # Get current time in that timezone
45
+ local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
46
+ return f"The current local time in {timezone} is: {local_time}"
47
+ except Exception as e:
48
+ return f"Error fetching time for timezone '{timezone}': {str(e)}"
49
+
50
+
51
+
52
+ final_answer = FinalAnswerTool()
53
+
54
+ # 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:
55
+ # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
56
+
57
+ model = HfApiModel(
58
+ max_tokens=2096,
59
+ temperature=0.5,
60
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
61
+ custom_role_conversions=None,
62
+ )
63
+
64
+
65
+ # Import tool from Hub
66
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
67
+
68
+ with open("prompts.yaml", 'r') as stream:
69
+ prompt_templates = yaml.safe_load(stream)
70
+
71
  agent = CodeAgent(
72
  model=model,
73
+ tools=[final_answer,search_duckduckgo,
74
+ get_current_time_in_timezone,image_generation_tool], ## add your tools here (don't remove final answer)
75
  max_steps=6,
76
  verbosity_level=1,
77
+ grammar=None,
78
+ planning_interval=None,
79
+ name=None,
80
+ description=None,
81
  prompt_templates=prompt_templates
82
  )
83
 
84
+
85
  GradioUI(agent).launch()