Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
|
|
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
|
@@ -79,7 +80,197 @@ def get_horoscope(sign: str, date: str = None, language: str = "EN") -> str:
|
|
| 79 |
except Exception as e:
|
| 80 |
return f"Error fetching horoscope: {str(e)}"
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
final_answer = FinalAnswerTool()
|
| 85 |
|
|
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
+
from dateutil import parser
|
| 3 |
import datetime
|
| 4 |
import requests
|
| 5 |
import pytz
|
|
|
|
| 80 |
except Exception as e:
|
| 81 |
return f"Error fetching horoscope: {str(e)}"
|
| 82 |
|
| 83 |
+
|
| 84 |
+
@tool
|
| 85 |
+
def get_date_panchang(date: str = None, data_language: str = "EN") -> str:
|
| 86 |
+
"""Fetches the Panchang data for a given date.
|
| 87 |
+
If the user does not provide a date, use today's real date.
|
| 88 |
+
Args:
|
| 89 |
+
date: Date in any format (optional)
|
| 90 |
+
data_language: Language of the Panchang data (e.g., 'EN' for English, 'HI' for Hindi).
|
| 91 |
+
"""
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
if not date:
|
| 95 |
+
now = datetime.datetime.now()
|
| 96 |
+
else:
|
| 97 |
+
try:
|
| 98 |
+
now = datetime.datetime.strptime(date, "%Y-%m-%d")
|
| 99 |
+
except ValueError:
|
| 100 |
+
return " Invalid date format. Use YYYY-MM-DD."
|
| 101 |
+
|
| 102 |
+
api_date = now.strftime("%d/%m/%y") # Format as DD/MM/YY for API
|
| 103 |
+
|
| 104 |
+
try:
|
| 105 |
+
url = (
|
| 106 |
+
f"https://api.exaweb.in:3004/api/panchang/daily?"
|
| 107 |
+
f"date={api_date}&app_language=EN&data_language={data_language}"
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
headers = {
|
| 111 |
+
"api_key": "anvl_bharat_cal123", # Replace with your actual token
|
| 112 |
+
"Content-Type": "application/json"
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
response = requests.get(url, headers=headers)
|
| 116 |
+
response.raise_for_status()
|
| 117 |
+
data = response.json()
|
| 118 |
+
|
| 119 |
+
if not data or not isinstance(data, dict):
|
| 120 |
+
return " Empty or unexpected response."
|
| 121 |
+
|
| 122 |
+
# Format the entire Panchang data nicely
|
| 123 |
+
lines = [f" {data.get('date', api_date)} का पंचांग ({data.get('location', 'Unknown')}):\n"]
|
| 124 |
+
|
| 125 |
+
for key, value in data.items():
|
| 126 |
+
if key in ["date", "location", "app_language", "data_language", "universalDate"]:
|
| 127 |
+
continue # Skip metadata
|
| 128 |
+
|
| 129 |
+
if isinstance(value, list):
|
| 130 |
+
if all(isinstance(item, str) for item in value):
|
| 131 |
+
lines.append(f"🔹 {key}: {', '.join(value)}")
|
| 132 |
+
elif all(isinstance(item, dict) for item in value):
|
| 133 |
+
lines.append(f"🔹 {key}:")
|
| 134 |
+
for item in value:
|
| 135 |
+
vals = item.get("values", [])
|
| 136 |
+
upto = item.get("upto", "")
|
| 137 |
+
lines.append(f" • {', '.join(vals)} {'— ' + upto if upto else ''}")
|
| 138 |
+
elif isinstance(value, str):
|
| 139 |
+
lines.append(f"🔹 {key}: {value}")
|
| 140 |
+
elif isinstance(value, int):
|
| 141 |
+
lines.append(f"🔹 {key}: {value}")
|
| 142 |
+
|
| 143 |
+
return "\n".join(lines)
|
| 144 |
+
|
| 145 |
+
except Exception as e:
|
| 146 |
+
return f" Failed to fetch Panchang for {api_date}: {str(e)}"
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
@tool
|
| 150 |
+
def get_holidays(year: int = None, app_language: str = "EN", data_language: str = "HI") -> str:
|
| 151 |
+
"""
|
| 152 |
+
Fetches holidays from all categories (Hindu, Islamic, Christian) for a given year from ExaWeb API.
|
| 153 |
+
Args:
|
| 154 |
+
year: Year (e.g., 2025). Optional — defaults to current year.
|
| 155 |
+
app_language: App language for API (default: "EN").
|
| 156 |
+
data_language: Data language for holidays (default: "HI").
|
| 157 |
+
"""
|
| 158 |
+
if not year:
|
| 159 |
+
year = datetime.datetime.now().year
|
| 160 |
+
|
| 161 |
+
params = {
|
| 162 |
+
"app_language": app_language,
|
| 163 |
+
"data_language": data_language,
|
| 164 |
+
"year": year
|
| 165 |
+
}
|
| 166 |
+
headers = {
|
| 167 |
+
"api_key": "anvl_bharat_cal123"
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
try:
|
| 171 |
+
response = requests.get(
|
| 172 |
+
"https://api.exaweb.in:3004/api/panchang/holiday",
|
| 173 |
+
params=params,
|
| 174 |
+
headers=headers
|
| 175 |
+
)
|
| 176 |
+
response.raise_for_status()
|
| 177 |
+
data = response.json()
|
| 178 |
+
|
| 179 |
+
formatted = []
|
| 180 |
+
|
| 181 |
+
for category, groups in data.items():
|
| 182 |
+
if category in ["year", "app_language", "data_language"]:
|
| 183 |
+
continue # Skip non-holiday keys
|
| 184 |
+
|
| 185 |
+
formatted.append(f"\n=== {category} Holidays ===")
|
| 186 |
+
|
| 187 |
+
for group in groups:
|
| 188 |
+
for holiday in group:
|
| 189 |
+
title = holiday.get("title", "")
|
| 190 |
+
date = holiday.get("date", "")
|
| 191 |
+
formatted.append(f"{date}: {title}")
|
| 192 |
+
|
| 193 |
+
return "\n".join(formatted) if formatted else f"No holidays found for {year}."
|
| 194 |
+
|
| 195 |
+
except requests.RequestException as e:
|
| 196 |
+
return f"Error fetching holidays: {e}"
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
@tool
|
| 200 |
+
def get_monthly_festivals(year: Optional[int] = None,month: Optional[str] = None,app_language: str = "EN",data_language: str = "EN") -> str:
|
| 201 |
+
"""
|
| 202 |
+
Fetches festival data for a specific month and year from the ExaWeb API.
|
| 203 |
+
Args:
|
| 204 |
+
year: The year for which to fetch the festivals (e.g., 2025).
|
| 205 |
+
If not provided, the current year will be used.
|
| 206 |
+
month: The full name of the month for which to fetch the festivals (e.g., "june").
|
| 207 |
+
If not provided, the current month will be used.
|
| 208 |
+
app_language: The language for the application interface (default: "EN").
|
| 209 |
+
data_language: The language for the festival names (default: "EN").
|
| 210 |
+
Returns:
|
| 211 |
+
A formatted string listing the festivals for the given month and year,
|
| 212 |
+
or an error message if the data cannot be fetched.
|
| 213 |
+
"""
|
| 214 |
+
# Set default year to the current year if not provided
|
| 215 |
+
if not year:
|
| 216 |
+
year = datetime.datetime.now().year
|
| 217 |
+
|
| 218 |
+
# Set default month to the current month if not provided
|
| 219 |
+
if not month:
|
| 220 |
+
month = datetime.datetime.now().strftime("%B").lower()
|
| 221 |
+
else:
|
| 222 |
+
month = month.lower()
|
| 223 |
+
|
| 224 |
+
# API endpoint and parameters
|
| 225 |
+
api_url = "https://api.exaweb.in:3004/api/panchang/festival"
|
| 226 |
+
params = {
|
| 227 |
+
"year": year,
|
| 228 |
+
"month": month,
|
| 229 |
+
"app_language": app_language,
|
| 230 |
+
"data_language": data_language,
|
| 231 |
+
}
|
| 232 |
+
headers = {
|
| 233 |
+
"api_key": "anvl_bharat_cal123",
|
| 234 |
+
"Content-Type": "application/json"
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
try:
|
| 238 |
+
# Make the GET request to the API
|
| 239 |
+
response = requests.get(api_url, params=params, headers=headers)
|
| 240 |
+
# Raise an exception for bad status codes (4xx or 5xx)
|
| 241 |
+
response.raise_for_status()
|
| 242 |
|
| 243 |
+
# Parse the JSON response
|
| 244 |
+
data = response.json()
|
| 245 |
+
|
| 246 |
+
if not data:
|
| 247 |
+
return f"No festival data found for {month.capitalize()} {year}."
|
| 248 |
+
|
| 249 |
+
# Format the data for display
|
| 250 |
+
formatted_lines = []
|
| 251 |
+
for entry in data:
|
| 252 |
+
date = entry.get("date", "Unknown date")
|
| 253 |
+
festivals = entry.get("festivals", [])
|
| 254 |
+
|
| 255 |
+
# Filter out entries where the only festival is "NA"
|
| 256 |
+
meaningful_festivals = [f for f in festivals if f.strip().upper() != "NA"]
|
| 257 |
+
|
| 258 |
+
if meaningful_festivals:
|
| 259 |
+
# Join multiple festivals with a comma
|
| 260 |
+
festivals_str = ", ".join(meaningful_festivals)
|
| 261 |
+
formatted_lines.append(f"{date}: {festivals_str}")
|
| 262 |
+
|
| 263 |
+
if not formatted_lines:
|
| 264 |
+
return f"No festivals found for {month.capitalize()} {year}."
|
| 265 |
+
|
| 266 |
+
return "\n".join(formatted_lines)
|
| 267 |
+
|
| 268 |
+
except requests.exceptions.RequestException as e:
|
| 269 |
+
# Handle connection errors, timeouts, etc.
|
| 270 |
+
return f"An error occurred while fetching data from the API: {e}"
|
| 271 |
+
except ValueError:
|
| 272 |
+
# Handle cases where the response is not valid JSON
|
| 273 |
+
return "Failed to decode the response from the API."
|
| 274 |
|
| 275 |
final_answer = FinalAnswerTool()
|
| 276 |
|