Spaces:
Sleeping
Sleeping
Hadnle multiple images request
Browse files
app.py
CHANGED
|
@@ -19,24 +19,27 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
|
|
| 19 |
return "What magic will you build ?"
|
| 20 |
|
| 21 |
@tool
|
| 22 |
-
def
|
| 23 |
-
"""A tool that
|
| 24 |
Args:
|
| 25 |
limit: Number of images to return between 1 and 100
|
| 26 |
"""
|
| 27 |
-
#
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Send the GET request to the API
|
| 31 |
response = requests.get(url)
|
| 32 |
|
| 33 |
# Check if the request was successful
|
| 34 |
if response.status_code == 200:
|
| 35 |
-
# Extract the
|
| 36 |
data = response.json() # Parse the response as JSON
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
return image_url
|
| 40 |
else:
|
| 41 |
return f"Failed to fetch data. Status code: {response.status_code}"
|
| 42 |
|
|
|
|
| 19 |
return "What magic will you build ?"
|
| 20 |
|
| 21 |
@tool
|
| 22 |
+
def get_cat_images(limit: int = 1) -> list:
|
| 23 |
+
"""A tool that gets random cat images
|
| 24 |
Args:
|
| 25 |
limit: Number of images to return between 1 and 100
|
| 26 |
"""
|
| 27 |
+
# Ensure the limit is within the allowed range
|
| 28 |
+
if limit < 1 or limit > 100:
|
| 29 |
+
return "Limit must be between 1 and 100."
|
| 30 |
+
|
| 31 |
+
# URL for the cat image API with the limit parameter
|
| 32 |
+
url = f"https://api.thecatapi.com/v1/images/search?limit={limit}"
|
| 33 |
|
| 34 |
# Send the GET request to the API
|
| 35 |
response = requests.get(url)
|
| 36 |
|
| 37 |
# Check if the request was successful
|
| 38 |
if response.status_code == 200:
|
| 39 |
+
# Extract the URLs of the images from the response JSON
|
| 40 |
data = response.json() # Parse the response as JSON
|
| 41 |
+
image_urls = [item['url'] for item in data] # Extract URLs for all images
|
| 42 |
+
return image_urls
|
|
|
|
| 43 |
else:
|
| 44 |
return f"Failed to fetch data. Status code: {response.status_code}"
|
| 45 |
|