zerito commited on
Commit
f213f36
·
verified ·
1 Parent(s): db9c20c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -36
app.py CHANGED
@@ -3,63 +3,97 @@ 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 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.
24
  Args:
25
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
 
 
26
  """
27
  try:
28
- # Create timezone object
29
- tz = pytz.timezone(timezone)
30
- # Get current time in that timezone
31
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
32
- return f"The current local time in {timezone} is: {local_time}"
 
 
33
  except Exception as e:
34
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
-
36
 
 
37
  final_answer = FinalAnswerTool()
 
 
 
 
38
  model = HfApiModel(
39
- max_tokens=2096,
40
- temperature=0.5,
41
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
42
- custom_role_conversions=None,
43
  )
44
 
45
-
46
- # Import tool from Hub
47
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
48
-
49
  with open("prompts.yaml", 'r') as stream:
50
  prompt_templates = yaml.safe_load(stream)
51
-
 
52
  agent = CodeAgent(
53
  model=model,
54
- tools=[final_answer], ## add your tools here (don't remove final answer)
55
- max_steps=6,
56
- verbosity_level=1,
57
- grammar=None,
58
- planning_interval=None,
59
- name=None,
60
- description=None,
 
 
 
 
 
 
61
  prompt_templates=prompt_templates
62
  )
63
 
64
-
65
- GradioUI(agent).launch()
 
 
 
 
 
 
 
 
3
  import requests
4
  import pytz
5
  import yaml
6
+ import os
7
  from tools.final_answer import FinalAnswerTool
 
8
  from Gradio_UI import GradioUI
9
 
10
+ # Weather API Tool
11
  @tool
12
+ def get_current_weather(city: str) -> str:
13
+ """Get the current weather for a specified city.
 
14
  Args:
15
+ city: The city name (e.g., 'Paris', 'New York')
 
16
  """
17
+ api_key = os.getenv("OPENWEATHER_API_KEY")
18
+ if not api_key:
19
+ return "Error: API key missing. Set OPENWEATHER_API_KEY environment variable."
20
+
21
+ try:
22
+ url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
23
+ response = requests.get(url)
24
+ data = response.json()
25
+ if response.status_code != 200:
26
+ return f"Error: {data.get('message', 'Unknown error')}"
27
+
28
+ weather = data['weather'][0]['description']
29
+ temp = data['main']['temp']
30
+ humidity = data['main']['humidity']
31
+ return f"Weather in {city}: {weather}, {temp}°C, Humidity: {humidity}%"
32
+ except Exception as e:
33
+ return f"Error fetching weather: {str(e)}"
34
 
35
+ # Currency Conversion Tool
36
  @tool
37
+ def convert_currency(amount: float, from_cur: str, to_cur: str) -> str:
38
+ """Convert amount between currencies using current exchange rates.
39
  Args:
40
+ amount: Amount to convert
41
+ from_cur: Currency code to convert from (e.g., 'USD')
42
+ to_cur: Currency code to convert to (e.g., 'EUR')
43
  """
44
  try:
45
+ url = f"https://api.exchangerate-api.com/v4/latest/{from_cur}"
46
+ response = requests.get(url)
47
+ data = response.json()
48
+ rate = data['rates'].get(to_cur)
49
+ if not rate:
50
+ return f"Error: Currency {to_cur} not found"
51
+ return f"{amount} {from_cur} = {amount * rate:.2f} {to_cur}"
52
  except Exception as e:
53
+ return f"Error converting currency: {str(e)}"
 
54
 
55
+ # Initialize tools
56
  final_answer = FinalAnswerTool()
57
+ ddg_search = DuckDuckGoSearchTool()
58
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
59
+
60
+ # Load model with updated parameters
61
  model = HfApiModel(
62
+ max_tokens=4096,
63
+ temperature=0.7,
64
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
65
+ custom_role_conversions={"system": "You are a helpful AI assistant"},
66
  )
67
 
68
+ # Load prompts
 
 
 
69
  with open("prompts.yaml", 'r') as stream:
70
  prompt_templates = yaml.safe_load(stream)
71
+
72
+ # Configure agent with all tools
73
  agent = CodeAgent(
74
  model=model,
75
+ tools=[
76
+ final_answer,
77
+ get_current_weather,
78
+ convert_currency,
79
+ get_current_time_in_timezone,
80
+ ddg_search,
81
+ image_generation_tool
82
+ ],
83
+ max_steps=8,
84
+ verbosity_level=2,
85
+ planning_interval="auto",
86
+ name="SuperAssistant",
87
+ description="A multi-functional assistant capable of handling various tasks",
88
  prompt_templates=prompt_templates
89
  )
90
 
91
+ # Launch enhanced UI
92
+ GradioUI(
93
+ agent,
94
+ theme="soft",
95
+ additional_components=[
96
+ {"type": "markdown", "content": "## SuperAssistant Portal"},
97
+ {"type": "image", "source": "header.png"}
98
+ ]
99
+ ).launch()