Spaces:
Sleeping
Sleeping
Upload tool
Browse files- app.py +6 -0
- requirements.txt +3 -0
- tool.py +99 -0
app.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import launch_gradio_demo
|
| 2 |
+
from tool import SimpleTool
|
| 3 |
+
|
| 4 |
+
tool = SimpleTool()
|
| 5 |
+
|
| 6 |
+
launch_gradio_demo(tool)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
requests
|
| 2 |
+
bs4
|
| 3 |
+
smolagents
|
tool.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import Tool
|
| 2 |
+
from typing import Any, Optional
|
| 3 |
+
|
| 4 |
+
class SimpleTool(Tool):
|
| 5 |
+
name = "get_holidays"
|
| 6 |
+
description = "A tool that returns holidays for a given day and month."
|
| 7 |
+
inputs = {"day":{"type":"integer","description":"The day of the month (1-31) in integer format"},"month":{"type":"integer","description":"The month of the year (1-12) in integer format"}}
|
| 8 |
+
output_type = "string"
|
| 9 |
+
|
| 10 |
+
def forward(self, day: int, month: int) -> str:
|
| 11 |
+
"""A tool that returns holidays for a given day and month.
|
| 12 |
+
|
| 13 |
+
Args:
|
| 14 |
+
day: The day of the month (1-31) in integer format
|
| 15 |
+
month: The month of the year (1-12) in integer format
|
| 16 |
+
|
| 17 |
+
Returns:
|
| 18 |
+
A string listing holidays for the specified date, or an error message if something goes wrong.
|
| 19 |
+
"""
|
| 20 |
+
#Imports
|
| 21 |
+
|
| 22 |
+
import requests
|
| 23 |
+
from bs4 import BeautifulSoup
|
| 24 |
+
|
| 25 |
+
#Mapping for request
|
| 26 |
+
|
| 27 |
+
months = {
|
| 28 |
+
1:"yanvar",
|
| 29 |
+
2:"fevral",
|
| 30 |
+
3:"mart",
|
| 31 |
+
4:"aprel",
|
| 32 |
+
5:"may",
|
| 33 |
+
6:"iyun",
|
| 34 |
+
7:"iyul",
|
| 35 |
+
8:"avgust",
|
| 36 |
+
9:"sentyabr",
|
| 37 |
+
10:"oktyabr",
|
| 38 |
+
11:"noyabr",
|
| 39 |
+
12:"dekabr"
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
# Validate the month and day
|
| 43 |
+
if month is None or day is None:
|
| 44 |
+
return "Invalid month/day"
|
| 45 |
+
|
| 46 |
+
# Validate month range
|
| 47 |
+
if not (1 <= month <= 12):
|
| 48 |
+
return "Invalid month"
|
| 49 |
+
|
| 50 |
+
# Validate day range (assuming non-leap year for simplicity)
|
| 51 |
+
if month in {4, 6, 9, 11} and day > 30:
|
| 52 |
+
return "Invalid day for this month"
|
| 53 |
+
elif month == 2 and day > 28:
|
| 54 |
+
return "Invalid day for February"
|
| 55 |
+
elif day > 31:
|
| 56 |
+
return "Invalid day"
|
| 57 |
+
|
| 58 |
+
# Map the month to its Russian name
|
| 59 |
+
russian_month = months[month]
|
| 60 |
+
|
| 61 |
+
# Construct the query URL
|
| 62 |
+
url = f"https://kakoysegodnyaprazdnik.ru/baza/{russian_month}/{day}"
|
| 63 |
+
|
| 64 |
+
# Define headers to mimic a browser
|
| 65 |
+
headers = {
|
| 66 |
+
"User-Agent": "Mozilla/5.0 (Windows; Windows NT 10.3; WOW64; en-US) AppleWebKit/603.45 (KHTML, like Gecko) Chrome/51.0.2756.132 Safari/601",
|
| 67 |
+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
|
| 68 |
+
"Accept-Language": "ru-RU,en;q=0.9",
|
| 69 |
+
"Accept-Encoding": "gzip, deflate"
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
try:
|
| 73 |
+
# Send GET request
|
| 74 |
+
response = requests.get(url, headers=headers)
|
| 75 |
+
response.raise_for_status() # Raises an exception for 4xx/5xx errors
|
| 76 |
+
response.encoding = 'utf-8' # Ensure proper decoding of Russian text
|
| 77 |
+
|
| 78 |
+
# Parse HTML content
|
| 79 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
| 80 |
+
listing_wr_div = soup.find("div", class_="listing_wr")
|
| 81 |
+
|
| 82 |
+
if not listing_wr_div:
|
| 83 |
+
return "No holiday data found for this date."
|
| 84 |
+
|
| 85 |
+
# Extract holiday names
|
| 86 |
+
holidays = []
|
| 87 |
+
for answer_div in listing_wr_div.find_all("div", itemprop=["suggestedAnswer", "acceptedAnswer"]):
|
| 88 |
+
span_tag = answer_div.find("span", itemprop="text")
|
| 89 |
+
if span_tag:
|
| 90 |
+
holiday = span_tag.get_text(strip=True)
|
| 91 |
+
holidays.append(holiday)
|
| 92 |
+
|
| 93 |
+
# Return results
|
| 94 |
+
if not holidays:
|
| 95 |
+
return "No holidays found for this date."
|
| 96 |
+
return "\n".join(holidays) # Join holidays with newlines for readability
|
| 97 |
+
|
| 98 |
+
except requests.RequestException as e:
|
| 99 |
+
return f"Failed to retrieve holidays. Error: {str(e)}"
|