TheDawoud commited on
Commit
050e2f3
·
1 Parent(s): c96ee12

- A more safe approach

Browse files
Files changed (1) hide show
  1. app.py +16 -16
app.py CHANGED
@@ -8,26 +8,26 @@ import numpy as np
8
  from Gradio_UI import GradioUI
9
  from bs4 import BeautifulSoup
10
  import random
 
 
 
 
11
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
12
  @tool
13
- def get_animal_image(animal_name:str)-> np.ndarray: #it's import to specify the return type
14
- #Keep this format for the description / args / args description but feel free to modify the tool
15
- """A tool that gets a random animal image from the internet and returns an image as numpy array
16
  Args:
17
- animal_name: the animal name
18
  """
19
- # search for the animal name on the internet in images search
20
- search_url = f"https://www.google.com/search?q={animal_name}&tbm=isch"
21
- #get the image from the internet
22
- response = requests.get(search_url)
23
- #parse the html
24
- soup = BeautifulSoup(response.text, 'html.parser')
25
- # select a random image from the search results
26
- image_url = random.choice(soup.find_all('img'))['src']
27
- #get the image from the internet
28
- response = requests.get(image_url)
29
- #return the image as numpy array
30
- return np.array(response.content)
31
 
32
  @tool
33
  def get_current_time_in_timezone(timezone: str) -> str:
 
8
  from Gradio_UI import GradioUI
9
  from bs4 import BeautifulSoup
10
  import random
11
+ from PIL import Image
12
+ from io import BytesIO
13
+ from typing import Annotated
14
+
15
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
16
  @tool
17
+ def get_animal_image(animal_name: Annotated[str, "The animal name to search for."]) -> Annotated[np.ndarray, "A numpy array of the image."]:
18
+ """Returns a random animal image from an online image API as a numpy array.
 
19
  Args:
20
+ animal_name: The animal name to search for.
21
  """
22
+ # Use a real image API like Unsplash or Wikimedia
23
+ url = f"https://source.unsplash.com/600x400/?{animal_name}"
24
+
25
+ response = requests.get(url)
26
+ if response.status_code != 200:
27
+ raise RuntimeError("Failed to retrieve image.")
28
+
29
+ image = Image.open(BytesIO(response.content)).convert("RGB")
30
+ return np.array(image)
 
 
 
31
 
32
  @tool
33
  def get_current_time_in_timezone(timezone: str) -> str: