Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,14 +9,43 @@ 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 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 9 |
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
+
from langchain.agents import tool
|
| 13 |
+
import requests
|
| 14 |
+
|
| 15 |
+
@tool
|
| 16 |
+
def get_country_capital(country_name: str) -> str:
|
| 17 |
+
"""
|
| 18 |
+
A tool that retrieves the capital of a country based on its name
|
| 19 |
+
from the CountriesNow API.
|
| 20 |
+
|
| 21 |
Args:
|
| 22 |
+
country_name: The name of the country (e.g., "Nigeria").
|
| 23 |
+
|
| 24 |
+
Returns:
|
| 25 |
+
A string describing the capital or an error message if something goes wrong.
|
| 26 |
"""
|
| 27 |
+
url = "https://countriesnow.space/api/v0.1/countries/capital"
|
| 28 |
+
payload = {"country": country_name}
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
response = requests.post(url, json=payload)
|
| 32 |
+
|
| 33 |
+
if response.status_code != 200:
|
| 34 |
+
return f"API call failed with status code {response.status_code}."
|
| 35 |
+
|
| 36 |
+
data = response.json()
|
| 37 |
+
|
| 38 |
+
if data.get("error") is False:
|
| 39 |
+
# Extract the capital from the response
|
| 40 |
+
capital = data["data"].get("capital", "No capital found.")
|
| 41 |
+
return f"The capital of {country_name.capitalize()} is: {capital}"
|
| 42 |
+
else:
|
| 43 |
+
# If the API returned an error message
|
| 44 |
+
return f"Error returned by the API: {data.get('msg')}"
|
| 45 |
+
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return f"An error occurred: {str(e)}"
|
| 48 |
+
|
| 49 |
|
| 50 |
@tool
|
| 51 |
def get_current_time_in_timezone(timezone: str) -> str:
|