lp128396 commited on
Commit
6bb9a48
·
verified ·
1 Parent(s): 8c5c24b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py CHANGED
@@ -32,6 +32,26 @@ def get_current_time_in_timezone(timezone: str) -> str:
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()
@@ -39,6 +59,24 @@ final_answer = FinalAnswerTool()
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,
 
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
+ @tool
36
+ def get_weather(city: str) -> str:
37
+ """A tool that fetches current weather in a specified city.
38
+ Args:
39
+ city: Name of the city to fetch weather for.
40
+ """
41
+ try:
42
+ api_key = "your_openweathermap_api_key" # Replace with your real API key
43
+ url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
44
+ response = requests.get(url).json()
45
+
46
+ if response["cod"] != 200:
47
+ return f"Error: {response.get('message', 'Unknown error')}"
48
+
49
+ description = response["weather"][0]["description"]
50
+ temp = response["main"]["temp"]
51
+ humidity = response["main"]["humidity"]
52
+ return f"Weather in {city}: {description}, Temperature: {temp}°C, Humidity: {humidity}%"
53
+ except Exception as e:
54
+ return f"Failed to fetch weather: {str(e)}"
55
 
56
 
57
  final_answer = FinalAnswerTool()
 
59
  # 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:
60
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
61
 
62
+ agent = CodeAgent(
63
+ model=model,
64
+ tools=[
65
+ final_answer,
66
+ get_current_time_in_timezone,
67
+ get_weather,
68
+ # You can also keep my_custom_tool if you want to customize it later
69
+ ],
70
+ max_steps=6,
71
+ verbosity_level=1,
72
+ grammar=None,
73
+ planning_interval=None,
74
+ name=None,
75
+ description=None,
76
+ prompt_templates=prompt_templates
77
+ )
78
+
79
+
80
  model = HfApiModel(
81
  max_tokens=2096,
82
  temperature=0.5,