Sagar S commited on
Commit
dcfa341
·
verified ·
1 Parent(s): 8c5c24b

add country data

Browse files
Files changed (1) hide show
  1. app.py +41 -6
app.py CHANGED
@@ -9,14 +9,49 @@ 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
+ import requests
13
+
14
+ def get_country_data(country_name):
15
+ """
16
+ Fetches and prints data for a country from the REST Countries API.
17
+
18
  Args:
19
+ country_name (str): The common name of the country (e.g., "France").
 
20
  """
21
+ # The API URL to search by country name
22
+ api_url = f"https://restcountries.com/v3.1/name/{country_name}"
23
+
24
+ try:
25
+ # Make the GET request
26
+ response = requests.get(api_url)
27
+
28
+ # Raise an exception for bad status codes (4xx or 5xx)
29
+ response.raise_for_status()
30
+
31
+ # The API returns a list of countries, even for a single-name search
32
+ data = response.json()
33
+
34
+ # We'll work with the first country in the list
35
+ country = data[0]
36
+
37
+ print(f"Country: {country['name']['common']}")
38
+ print(f"Capital: {country['capital'][0]}")
39
+ print(f"Region: {country['region']}")
40
+ print(f"Population: {country['population']:,}")
41
+
42
+ except requests.exceptions.HTTPError as errh:
43
+ print(f"HTTP Error: {errh}")
44
+ except requests.exceptions.ConnectionError as errc:
45
+ print(f"Error Connecting: {errc}")
46
+ except requests.exceptions.Timeout as errt:
47
+ print(f"Timeout Error: {errt}")
48
+ except requests.exceptions.RequestException as err:
49
+ print(f"Something went wrong: {err}")
50
+ except IndexError:
51
+ print(f"No data found for country '{country_name}'. Check the spelling.")
52
+ except Exception as e:
53
+ print(f"An unexpected error occurred: {e}")
54
+
55
 
56
  @tool
57
  def get_current_time_in_timezone(timezone: str) -> str: