Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,95 @@
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
|
|
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 7 |
|
| 8 |
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(
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
+
from bs4 import BeautifulSoup
|
| 3 |
import datetime
|
| 4 |
import requests
|
| 5 |
import pytz
|
| 6 |
import yaml
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
+
import re
|
| 9 |
|
| 10 |
from Gradio_UI import GradioUI
|
| 11 |
|
|
|
|
| 12 |
@tool
|
| 13 |
+
def my_custom_tool(animal_type=None, breed=None):
|
| 14 |
+
"""Finds animals and breeds from Brampton Animal Shelter website."""
|
| 15 |
+
|
| 16 |
+
base_url = "https://www.brampton.ca/EN/residents/Animal-Services/Pages/Adoption.aspx"
|
| 17 |
+
|
| 18 |
+
matching_animals = []
|
| 19 |
+
processed_ids = {}
|
| 20 |
+
|
| 21 |
+
session = requests.Session()
|
| 22 |
+
response = session.get(base_url)
|
| 23 |
+
if response.status_code != 200:
|
| 24 |
+
return f"Failed to access the website. Status code: {response.status_code}"
|
| 25 |
+
|
| 26 |
+
# Only process the main page, no pagination
|
| 27 |
+
process_page(response.text, matching_animals, processed_ids, animal_type, breed)
|
| 28 |
+
|
| 29 |
+
animal_sentences = []
|
| 30 |
+
for animal in matching_animals:
|
| 31 |
+
if (animal.get("name") in [None, "", "Unknown"] or
|
| 32 |
+
animal.get("age") in [None, "", "unknown age"] or
|
| 33 |
+
animal.get("sex") in [None, "", "unknown gender"] or
|
| 34 |
+
animal.get("breed") in [None, "", "unknown breed"] or
|
| 35 |
+
animal.get("type") in [None, "", "animal"]):
|
| 36 |
+
continue
|
| 37 |
+
|
| 38 |
+
name = animal.get("name", "Unknown").title()
|
| 39 |
+
age = animal.get("age", "unknown age").lower().strip()
|
| 40 |
+
sex = animal.get("sex", "unknown gender").lower()
|
| 41 |
+
breed_name = animal.get("breed", "unknown breed").title()
|
| 42 |
+
animal_type_value = animal.get("type", "animal").lower()
|
| 43 |
+
|
| 44 |
+
sentence = f"A {age} old {sex} {breed_name} named {name}, type: {animal_type_value}"
|
| 45 |
+
animal_sentences.append(sentence)
|
| 46 |
+
|
| 47 |
+
if animal_sentences:
|
| 48 |
+
return "\n".join(animal_sentences)
|
| 49 |
+
else:
|
| 50 |
+
return "No matching animals found."
|
| 51 |
+
|
| 52 |
+
def process_page(html_content, matching_animals, processed_ids, animal_type=None, breed=None):
|
| 53 |
+
"""Process a single page of animal listings"""
|
| 54 |
+
soup = BeautifulSoup(html_content, 'html.parser')
|
| 55 |
+
|
| 56 |
+
animal_cards = soup.find_all("div", class_="dvanimalitem")
|
| 57 |
+
|
| 58 |
+
for card in animal_cards:
|
| 59 |
+
animal_info = {}
|
| 60 |
+
|
| 61 |
+
name_element = card.find("span", class_="animalname")
|
| 62 |
+
if name_element:
|
| 63 |
+
animal_info["name"] = name_element.text.strip()
|
| 64 |
+
|
| 65 |
+
for field in ["id", "type", "sex", "breed", "age", "size", "color", "location"]:
|
| 66 |
+
field_element = card.find("span", id=lambda x: x and x.startswith(f"sp{field}"))
|
| 67 |
+
if field_element:
|
| 68 |
+
animal_info[field] = field_element.text.strip()
|
| 69 |
+
|
| 70 |
+
if animal_info.get("id") in processed_ids:
|
| 71 |
+
continue
|
| 72 |
+
|
| 73 |
+
if "id" in animal_info:
|
| 74 |
+
processed_ids[animal_info["id"]] = True
|
| 75 |
+
|
| 76 |
+
# Matching logic
|
| 77 |
+
type_match = True
|
| 78 |
+
breed_match = True
|
| 79 |
+
|
| 80 |
+
if animal_type:
|
| 81 |
+
animal_type_lower = animal_type.lower()
|
| 82 |
+
current_type_lower = animal_info.get("type", "").lower()
|
| 83 |
+
if animal_type_lower == "other":
|
| 84 |
+
type_match = current_type_lower not in ["dog", "cat", "bird"]
|
| 85 |
+
else:
|
| 86 |
+
type_match = animal_type_lower in current_type_lower
|
| 87 |
+
|
| 88 |
+
if breed:
|
| 89 |
+
breed_match = breed.lower() in animal_info.get("breed", "").lower()
|
| 90 |
+
|
| 91 |
+
if type_match and breed_match:
|
| 92 |
+
matching_animals.append(animal_info)
|
| 93 |
|
| 94 |
@tool
|
| 95 |
def get_current_time_in_timezone(timezone: str) -> str:
|