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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -69
app.py CHANGED
@@ -3,97 +3,60 @@ import datetime
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()
 
3
  import requests
4
  import pytz
5
  import yaml
 
6
  from tools.final_answer import FinalAnswerTool
7
  from Gradio_UI import GradioUI
8
 
9
+ # Example custom tool: a magic placeholder for creative ideas!
10
  @tool
11
+ def my_custom_tool(arg1: str, arg2: int) -> str:
12
+ """A tool that does nothing yet.
13
+
14
  Args:
15
+ arg1: the first argument.
16
+ arg2: the second argument.
17
  """
18
+ return "What magic will you build?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
 
20
  @tool
21
+ def get_current_time_in_timezone(timezone: str) -> str:
22
+ """A tool that fetches the current local time in a specified timezone.
23
+
24
  Args:
25
+ timezone: A string representing a valid timezone (e.g., 'America/New_York').
 
 
26
  """
27
  try:
28
+ tz = pytz.timezone(timezone)
29
+ local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
30
+ return f"The current local time in {timezone} is: {local_time}"
 
 
 
 
31
  except Exception as e:
32
+ return f"Error fetching time for timezone '{timezone}': {str(e)}"
33
 
 
34
  final_answer = FinalAnswerTool()
 
 
35
 
36
+ # Using a free model (e.g., 'gpt2') instead of a paid one.
37
  model = HfApiModel(
38
+ max_tokens=2096,
39
+ temperature=0.5,
40
+ model_id='gpt2', # Free/open-source model; change as needed.
41
+ custom_role_conversions=None,
42
  )
43
 
44
+ # Import an image generation tool from the Hub as an example.
45
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
46
+
47
  with open("prompts.yaml", 'r') as stream:
48
  prompt_templates = yaml.safe_load(stream)
49
+
 
50
  agent = CodeAgent(
51
  model=model,
52
+ tools=[final_answer, my_custom_tool, get_current_time_in_timezone, DuckDuckGoSearchTool(), image_generation_tool],
53
+ max_steps=6,
54
+ verbosity_level=1,
55
+ grammar=None,
56
+ planning_interval=None,
57
+ name=None,
58
+ description=None,
 
 
 
 
 
 
59
  prompt_templates=prompt_templates
60
  )
61
 
62
+ GradioUI(agent).launch()