Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -202,6 +202,88 @@ def get_holidays(year: int = None, app_language: str = "EN", data_language: str
|
|
| 202 |
return f"Error fetching holidays: {e}"
|
| 203 |
|
| 204 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
@tool
|
| 206 |
def get_panchang_field(field: str, date: str = None) -> str:
|
| 207 |
"""Fetches specific Panchang field like Tithi, Nakshatra, Yoga, etc.
|
|
@@ -250,6 +332,7 @@ agent = CodeAgent(
|
|
| 250 |
tools=[final_answer,
|
| 251 |
get_horoscope,
|
| 252 |
get_date_panchang,
|
|
|
|
| 253 |
get_holidays,
|
| 254 |
get_panchang_field,
|
| 255 |
get_festivals_today,
|
|
|
|
| 202 |
return f"Error fetching holidays: {e}"
|
| 203 |
|
| 204 |
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
@tool
|
| 208 |
+
def get_monthly_festivals(year: Optional[int] = None,month: Optional[str] = None,app_language: str = "EN",data_language: str = "EN") -> str:
|
| 209 |
+
"""
|
| 210 |
+
Fetches festival data for a specific month and year from the ExaWeb API.
|
| 211 |
+
|
| 212 |
+
Args:
|
| 213 |
+
year: The year for which to fetch the festivals (e.g., 2025).
|
| 214 |
+
If not provided, the current year will be used.
|
| 215 |
+
month: The full name of the month for which to fetch the festivals (e.g., "june").
|
| 216 |
+
If not provided, the current month will be used.
|
| 217 |
+
app_language: The language for the application interface (default: "EN").
|
| 218 |
+
data_language: The language for the festival names (default: "EN").
|
| 219 |
+
|
| 220 |
+
Returns:
|
| 221 |
+
A formatted string listing the festivals for the given month and year,
|
| 222 |
+
or an error message if the data cannot be fetched.
|
| 223 |
+
"""
|
| 224 |
+
# Set default year to the current year if not provided
|
| 225 |
+
if not year:
|
| 226 |
+
year = datetime.datetime.now().year
|
| 227 |
+
|
| 228 |
+
# Set default month to the current month if not provided
|
| 229 |
+
if not month:
|
| 230 |
+
month = datetime.datetime.now().strftime("%B").lower()
|
| 231 |
+
else:
|
| 232 |
+
month = month.lower()
|
| 233 |
+
|
| 234 |
+
# API endpoint and parameters
|
| 235 |
+
api_url = "https://api.exaweb.in:3004/api/panchang/festival"
|
| 236 |
+
params = {
|
| 237 |
+
"year": year,
|
| 238 |
+
"month": month,
|
| 239 |
+
"app_language": app_language,
|
| 240 |
+
"data_language": data_language,
|
| 241 |
+
}
|
| 242 |
+
headers = {
|
| 243 |
+
"api_key": "anvl_bharat_cal123",
|
| 244 |
+
"Content-Type": "application/json"
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
try:
|
| 248 |
+
# Make the GET request to the API
|
| 249 |
+
response = requests.get(api_url, params=params, headers=headers)
|
| 250 |
+
# Raise an exception for bad status codes (4xx or 5xx)
|
| 251 |
+
response.raise_for_status()
|
| 252 |
+
|
| 253 |
+
# Parse the JSON response
|
| 254 |
+
data = response.json()
|
| 255 |
+
|
| 256 |
+
if not data:
|
| 257 |
+
return f"No festival data found for {month.capitalize()} {year}."
|
| 258 |
+
|
| 259 |
+
# Format the data for display
|
| 260 |
+
formatted_lines = []
|
| 261 |
+
for entry in data:
|
| 262 |
+
date = entry.get("date", "Unknown date")
|
| 263 |
+
festivals = entry.get("festivals", [])
|
| 264 |
+
|
| 265 |
+
# Filter out entries where the only festival is "NA"
|
| 266 |
+
meaningful_festivals = [f for f in festivals if f.strip().upper() != "NA"]
|
| 267 |
+
|
| 268 |
+
if meaningful_festivals:
|
| 269 |
+
# Join multiple festivals with a comma
|
| 270 |
+
festivals_str = ", ".join(meaningful_festivals)
|
| 271 |
+
formatted_lines.append(f"{date}: {festivals_str}")
|
| 272 |
+
|
| 273 |
+
if not formatted_lines:
|
| 274 |
+
return f"No festivals found for {month.capitalize()} {year}."
|
| 275 |
+
|
| 276 |
+
return "\n".join(formatted_lines)
|
| 277 |
+
|
| 278 |
+
except requests.exceptions.RequestException as e:
|
| 279 |
+
# Handle connection errors, timeouts, etc.
|
| 280 |
+
return f"An error occurred while fetching data from the API: {e}"
|
| 281 |
+
except ValueError:
|
| 282 |
+
# Handle cases where the response is not valid JSON
|
| 283 |
+
return "Failed to decode the response from the API."
|
| 284 |
+
|
| 285 |
+
|
| 286 |
+
|
| 287 |
@tool
|
| 288 |
def get_panchang_field(field: str, date: str = None) -> str:
|
| 289 |
"""Fetches specific Panchang field like Tithi, Nakshatra, Yoga, etc.
|
|
|
|
| 332 |
tools=[final_answer,
|
| 333 |
get_horoscope,
|
| 334 |
get_date_panchang,
|
| 335 |
+
get_monthly_festivals,
|
| 336 |
get_holidays,
|
| 337 |
get_panchang_field,
|
| 338 |
get_festivals_today,
|