Jack200133 commited on
Commit
8eefbef
·
verified ·
1 Parent(s): f79425a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -84
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
3
  import requests
4
  import pytz
@@ -7,6 +7,17 @@ from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
9
 
 
 
 
 
 
 
 
 
 
 
 
10
  @tool
11
  def get_current_time_in_timezone(timezone: str) -> str:
12
  """A tool that fetches the current local time in a specified timezone.
@@ -22,106 +33,37 @@ def get_current_time_in_timezone(timezone: str) -> str:
22
  except Exception as e:
23
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
24
 
25
- @tool
26
- def fetch_weather_data(location: str) -> str:
27
- """A tool that fetches current weather information for a specified location.
28
- Args:
29
- location: A string representing a city or location (e.g., 'London, UK').
30
- """
31
- try:
32
- # Using a free weather API (no API key required for demo)
33
- api_url = f"https://wttr.in/{location}?format=j1"
34
- response = requests.get(api_url)
35
- response.raise_for_status() # Raise an exception for bad responses
36
-
37
- weather_data = response.json()
38
- current = weather_data.get("current_condition", [{}])[0]
39
-
40
- # Extract key information
41
- temp_c = current.get("temp_C", "N/A")
42
- temp_f = current.get("temp_F", "N/A")
43
- description = current.get("weatherDesc", [{}])[0].get("value", "N/A")
44
- humidity = current.get("humidity", "N/A")
45
-
46
- return f"Weather in {location}: {description}, Temperature: {temp_c}°C ({temp_f}°F), Humidity: {humidity}%"
47
- except Exception as e:
48
- return f"Error fetching weather for '{location}': {str(e)}"
49
-
50
- # First, let's make sure we have the correct prompts.yaml file
51
- with open("prompts.yaml", 'w') as f:
52
- f.write("""
53
- # Core prompts for the agent system
54
- system: |
55
- You are an AI assistant that helps users solve problems step-by-step.
56
- You have access to several tools that you can use to assist the user.
57
- Always think carefully about which tool is most appropriate for the task.
58
-
59
- thinking: |
60
- Let me think through this step by step to determine the best approach to solving the problem.
61
-
62
- I need to consider:
63
- 1. What information do I already have?
64
- 2. What information do I still need?
65
- 3. Which tools would be most appropriate to use?
66
- 4. What is the logical sequence of steps I should follow?
67
-
68
- tool_use: |
69
- I'll use the {tool_name} tool to help with this task.
70
-
71
- Parameters I need to provide:
72
- {parameters_thinking}
73
-
74
- Let me execute this now.
75
 
76
- final_answer: |
77
- Based on my analysis and the information gathered, here is my final answer:
78
-
79
- {answer}
80
-
81
- Is there anything else you'd like me to help with or explain?
82
- """)
83
-
84
- # Get tools ready
85
  final_answer = FinalAnswerTool()
86
- search_tool = DuckDuckGoSearchTool()
87
 
88
- # Try loading the image generation tool
89
- try:
90
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
91
- all_tools = [get_current_time_in_timezone, fetch_weather_data, search_tool, image_generation_tool, final_answer]
92
- except Exception as e:
93
- print(f"Warning: Could not load image generation tool: {e}")
94
- all_tools = [get_current_time_in_timezone, fetch_weather_data, search_tool, final_answer]
95
 
96
- # Initialize the model
97
  model = HfApiModel(
98
- max_tokens=2096,
99
- temperature=0.5,
100
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
101
- custom_role_conversions=None,
102
  )
103
 
104
- # Load the prompt templates
 
 
 
105
  with open("prompts.yaml", 'r') as stream:
106
  prompt_templates = yaml.safe_load(stream)
107
-
108
- # Initialize the agent
109
  agent = CodeAgent(
110
  model=model,
111
- tools=all_tools, # Using all tools we've defined
112
  max_steps=6,
113
  verbosity_level=1,
114
  grammar=None,
115
  planning_interval=None,
116
- name="AssistantAgent",
117
- description="A helpful AI assistant with various tools",
118
  prompt_templates=prompt_templates
119
  )
120
 
121
- # Launch the Gradio UI
122
- print("===== Application Startup =====")
123
- print(f"Starting agent with {len(all_tools)} tools")
124
- print(f"Current date and time: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
125
- print("============================")
126
 
127
  GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
  import requests
4
  import pytz
 
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 my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
+ #Keep this format for the description / args / args description but feel free to modify the tool
14
+ """A tool that does nothing yet
15
+ Args:
16
+ arg1: the first argument
17
+ arg2: the second argument
18
+ """
19
+ return "What magic will you build ?"
20
+
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
23
  """A tool that fetches the current local time in a specified timezone.
 
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
 
 
 
 
 
 
 
 
 
37
  final_answer = FinalAnswerTool()
 
38
 
39
+ # 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:
40
+ # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
 
 
 
 
 
41
 
 
42
  model = HfApiModel(
43
+ max_tokens=2096,
44
+ temperature=0.5,
45
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
46
+ custom_role_conversions=None,
47
  )
48
 
49
+
50
+ # Import tool from Hub
51
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
52
+
53
  with open("prompts.yaml", 'r') as stream:
54
  prompt_templates = yaml.safe_load(stream)
55
+
 
56
  agent = CodeAgent(
57
  model=model,
58
+ tools=[final_answer], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
62
  planning_interval=None,
63
+ name=None,
64
+ description=None,
65
  prompt_templates=prompt_templates
66
  )
67
 
 
 
 
 
 
68
 
69
  GradioUI(agent).launch()