Update app.py
Browse files
app.py
CHANGED
|
@@ -33,6 +33,33 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
|
@@ -65,5 +92,14 @@ agent = CodeAgent(
|
|
| 65 |
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
GradioUI(agent).launch()
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
| 36 |
+
@tool
|
| 37 |
+
def get_weather_by_city(city: str) -> str:
|
| 38 |
+
"""
|
| 39 |
+
A tool that fetches the current weather for a specified city.
|
| 40 |
+
|
| 41 |
+
Args:
|
| 42 |
+
city: Name of the city (e.g., 'Jakarta').
|
| 43 |
+
"""
|
| 44 |
+
try:
|
| 45 |
+
# Ganti 'YOUR_API_KEY' dengan API key dari OpenWeatherMap
|
| 46 |
+
api_key = "YOUR_API_KEY"
|
| 47 |
+
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
|
| 48 |
+
response = requests.get(url)
|
| 49 |
+
data = response.json()
|
| 50 |
+
|
| 51 |
+
if response.status_code != 200 or "main" not in data:
|
| 52 |
+
return f"Failed to get weather for {city}: {data.get('message', 'Unknown error')}"
|
| 53 |
+
|
| 54 |
+
temp = data['main']['temp']
|
| 55 |
+
weather = data['weather'][0]['description']
|
| 56 |
+
humidity = data['main']['humidity']
|
| 57 |
+
return f"The weather in {city} is {weather} with a temperature of {temp}°C and humidity of {humidity}%."
|
| 58 |
+
|
| 59 |
+
except Exception as e:
|
| 60 |
+
return f"Error fetching weather: {str(e)}"
|
| 61 |
+
|
| 62 |
+
|
| 63 |
|
| 64 |
final_answer = FinalAnswerTool()
|
| 65 |
|
|
|
|
| 92 |
prompt_templates=prompt_templates
|
| 93 |
)
|
| 94 |
|
| 95 |
+
tools = [
|
| 96 |
+
final_answer,
|
| 97 |
+
get_current_time_in_timezone,
|
| 98 |
+
my_custom_tool,
|
| 99 |
+
image_generation_tool,
|
| 100 |
+
get_weather_by_city
|
| 101 |
+
]
|
| 102 |
+
|
| 103 |
+
|
| 104 |
|
| 105 |
GradioUI(agent).launch()
|