tomascufaro commited on
Commit
03844f8
·
verified ·
1 Parent(s): ae7a494

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -6
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
- 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:
 
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: