Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,107 +1,63 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
import json
|
| 4 |
import datetime
|
| 5 |
import re
|
| 6 |
from bs4 import BeautifulSoup
|
| 7 |
from typing import Optional, Dict, Any
|
| 8 |
-
from urllib.parse import quote
|
| 9 |
|
| 10 |
class ShabbatCandleLightingAgent:
|
| 11 |
-
"""
|
| 12 |
-
AI Agent for finding Shabbat candle lighting times worldwide using Chabad.org
|
| 13 |
-
"""
|
| 14 |
|
| 15 |
def __init__(self):
|
| 16 |
self.base_url = "https://www.chabad.org"
|
| 17 |
self.candle_lighting_url = "https://www.chabad.org/calendar/candlelighting_cdo/aid/6226/jewish/Accurate-Shabbat-Candle-Lighting-Times.htm"
|
| 18 |
|
| 19 |
def search_city_on_chabad(self, city_name: str) -> Optional[str]:
|
| 20 |
-
"""
|
| 21 |
-
Search for a city on Chabad.org and get the specific URL for candle lighting times
|
| 22 |
-
"""
|
| 23 |
try:
|
| 24 |
-
# Clean and format city name
|
| 25 |
city_clean = city_name.strip()
|
| 26 |
search_query = quote(city_clean)
|
| 27 |
|
| 28 |
headers = {
|
| 29 |
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
| 30 |
-
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
| 31 |
-
'Accept-Language': 'en-US,en;q=0.5',
|
| 32 |
-
'Accept-Encoding': 'gzip, deflate',
|
| 33 |
-
'Connection': 'keep-alive',
|
| 34 |
-
'Upgrade-Insecure-Requests': '1'
|
| 35 |
}
|
| 36 |
|
| 37 |
-
|
| 38 |
-
search_strategies = [
|
| 39 |
-
# Direct city page approach
|
| 40 |
-
f"https://www.chabad.org/calendar/candlelighting_cdo/jewish/{search_query}.htm",
|
| 41 |
-
# Search with city parameter
|
| 42 |
f"https://www.chabad.org/calendar/candlelighting_cdo/aid/6226/jewish/Accurate-Shabbat-Candle-Lighting-Times.htm?city={search_query}",
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
# General search
|
| 46 |
-
f"https://www.chabad.org/calendar/candlelighting_cdo/aid/6226/jewish/Accurate-Shabbat-Candle-Lighting-Times.htm"
|
| 47 |
]
|
| 48 |
|
| 49 |
-
for url in
|
| 50 |
try:
|
| 51 |
-
response = requests.get(url, headers=headers, timeout=
|
| 52 |
if response.status_code == 200:
|
| 53 |
-
# Check if the response contains candle lighting information
|
| 54 |
content_lower = response.text.lower()
|
| 55 |
-
if any(keyword in content_lower for keyword in ['candle lighting', '
|
| 56 |
return url
|
| 57 |
-
except Exception
|
| 58 |
-
print(f"Failed to fetch {url}: {e}")
|
| 59 |
continue
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
post_data = {
|
| 64 |
-
'city': city_clean,
|
| 65 |
-
'submit': 'Get Times'
|
| 66 |
-
}
|
| 67 |
-
response = requests.post(self.candle_lighting_url, data=post_data, headers=headers, timeout=15)
|
| 68 |
-
if response.status_code == 200 and 'candle lighting' in response.text.lower():
|
| 69 |
-
return response.url
|
| 70 |
-
except Exception as e:
|
| 71 |
-
print(f"POST request failed: {e}")
|
| 72 |
-
|
| 73 |
-
except Exception as e:
|
| 74 |
-
print(f"City search error: {e}")
|
| 75 |
|
| 76 |
-
return self.candle_lighting_url
|
| 77 |
|
| 78 |
def extract_candle_lighting_times(self, html_content: str, city_name: str) -> Dict[str, Any]:
|
| 79 |
-
"""
|
| 80 |
-
Extract candle lighting times from Chabad.org HTML content using multiple strategies
|
| 81 |
-
"""
|
| 82 |
try:
|
| 83 |
soup = BeautifulSoup(html_content, 'html.parser')
|
| 84 |
text = soup.get_text().lower()
|
| 85 |
|
| 86 |
-
# Multiple patterns to find candle lighting times
|
| 87 |
candle_patterns = [
|
| 88 |
r'candle\s+lighting[:\s]*(\d{1,2}:\d{2}(?:\s*[ap]m)?)',
|
| 89 |
r'light\s+candles[:\s]*(\d{1,2}:\d{2}(?:\s*[ap]m)?)',
|
| 90 |
-
r'friday[:\s]*(\d{1,2}:\d{2}(?:\s*[ap]m)?)'
|
| 91 |
-
r'(\d{1,2}:\d{2}\s*[ap]m?)(?=.*candle)',
|
| 92 |
-
r'(\d{1,2}:\d{2}\s*[ap]m?)(?=.*light)',
|
| 93 |
-
r'(\d{1,2}:\d{2}\s*[ap]m?)(?=.*friday)'
|
| 94 |
]
|
| 95 |
|
| 96 |
-
# Patterns for Havdalah times
|
| 97 |
havdalah_patterns = [
|
| 98 |
r'havdalah[:\s]*(\d{1,2}:\d{2}(?:\s*[ap]m)?)',
|
| 99 |
-
r'saturday[:\s]*(\d{1,2}:\d{2}(?:\s*[ap]m)?)'
|
| 100 |
-
r'(\d{1,2}:\d{2}\s*[ap]m?)(?=.*havdalah)',
|
| 101 |
-
r'(\d{1,2}:\d{2}\s*[ap]m?)(?=.*saturday)'
|
| 102 |
]
|
| 103 |
|
| 104 |
-
# Find candle lighting time
|
| 105 |
candle_time = None
|
| 106 |
for pattern in candle_patterns:
|
| 107 |
match = re.search(pattern, text, re.IGNORECASE)
|
|
@@ -109,7 +65,6 @@ class ShabbatCandleLightingAgent:
|
|
| 109 |
candle_time = match.group(1)
|
| 110 |
break
|
| 111 |
|
| 112 |
-
# Find Havdalah time
|
| 113 |
havdalah_time = None
|
| 114 |
for pattern in havdalah_patterns:
|
| 115 |
match = re.search(pattern, text, re.IGNORECASE)
|
|
@@ -117,34 +72,9 @@ class ShabbatCandleLightingAgent:
|
|
| 117 |
havdalah_time = match.group(1)
|
| 118 |
break
|
| 119 |
|
| 120 |
-
# Look for times in common HTML elements
|
| 121 |
-
if not candle_time or not havdalah_time:
|
| 122 |
-
# Check for time elements with specific classes or IDs
|
| 123 |
-
time_selectors = [
|
| 124 |
-
'[class*="time"]',
|
| 125 |
-
'[class*="candle"]',
|
| 126 |
-
'[class*="light"]',
|
| 127 |
-
'[id*="time"]',
|
| 128 |
-
'[id*="candle"]',
|
| 129 |
-
'td', 'span', 'div'
|
| 130 |
-
]
|
| 131 |
-
|
| 132 |
-
for selector in time_selectors:
|
| 133 |
-
elements = soup.select(selector)
|
| 134 |
-
for element in elements:
|
| 135 |
-
element_text = element.get_text().strip()
|
| 136 |
-
time_match = re.search(r'\d{1,2}:\d{2}(?:\s*[ap]m)?', element_text, re.IGNORECASE)
|
| 137 |
-
if time_match:
|
| 138 |
-
time_found = time_match.group()
|
| 139 |
-
if not candle_time and any(word in element_text.lower() for word in ['candle', 'light', 'friday']):
|
| 140 |
-
candle_time = time_found
|
| 141 |
-
elif not havdalah_time and any(word in element_text.lower() for word in ['havdalah', 'saturday']):
|
| 142 |
-
havdalah_time = time_found
|
| 143 |
-
|
| 144 |
-
# Calculate current Shabbat dates
|
| 145 |
now = datetime.datetime.now()
|
| 146 |
days_until_friday = (4 - now.weekday()) % 7
|
| 147 |
-
if days_until_friday == 0 and now.hour >= 18:
|
| 148 |
days_until_friday = 7
|
| 149 |
|
| 150 |
friday = now + datetime.timedelta(days=days_until_friday)
|
|
@@ -156,110 +86,52 @@ class ShabbatCandleLightingAgent:
|
|
| 156 |
'havdalah_time': havdalah_time,
|
| 157 |
'friday_date': friday.strftime('%B %d, %Y'),
|
| 158 |
'saturday_date': saturday.strftime('%B %d, %Y'),
|
| 159 |
-
'found_data': bool(candle_time or havdalah_time)
|
| 160 |
-
'search_successful': True
|
| 161 |
}
|
| 162 |
|
| 163 |
-
except Exception
|
| 164 |
-
print(f"Time extraction error: {e}")
|
| 165 |
return {
|
| 166 |
'city': city_name,
|
| 167 |
'candle_lighting_time': None,
|
| 168 |
'havdalah_time': None,
|
| 169 |
'friday_date': None,
|
| 170 |
'saturday_date': None,
|
| 171 |
-
'found_data': False
|
| 172 |
-
'search_successful': False,
|
| 173 |
-
'error': str(e)
|
| 174 |
-
}
|
| 175 |
-
|
| 176 |
-
def get_backup_times(self, city_name: str) -> Optional[Dict[str, Any]]:
|
| 177 |
-
"""
|
| 178 |
-
Try alternative sources for Shabbat times when Chabad.org fails
|
| 179 |
-
"""
|
| 180 |
-
try:
|
| 181 |
-
# Alternative Jewish calendar websites
|
| 182 |
-
backup_urls = [
|
| 183 |
-
f"https://www.hebcal.com/shabbat?city={quote(city_name)}",
|
| 184 |
-
f"https://www.myzmanim.com/search.aspx?city={quote(city_name)}"
|
| 185 |
-
]
|
| 186 |
-
|
| 187 |
-
headers = {
|
| 188 |
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
| 189 |
}
|
| 190 |
-
|
| 191 |
-
for url in backup_urls:
|
| 192 |
-
try:
|
| 193 |
-
response = requests.get(url, headers=headers, timeout=10)
|
| 194 |
-
if response.status_code == 200:
|
| 195 |
-
times = self.extract_candle_lighting_times(response.text, city_name)
|
| 196 |
-
if times.get('found_data'):
|
| 197 |
-
times['source'] = 'backup'
|
| 198 |
-
return times
|
| 199 |
-
except Exception as e:
|
| 200 |
-
print(f"Backup source {url} failed: {e}")
|
| 201 |
-
continue
|
| 202 |
-
|
| 203 |
-
except Exception as e:
|
| 204 |
-
print(f"Backup sources error: {e}")
|
| 205 |
-
|
| 206 |
-
return None
|
| 207 |
|
| 208 |
def get_shabbat_times(self, city_input: str = "") -> str:
|
| 209 |
-
"""
|
| 210 |
-
Main function to get Shabbat candle lighting times
|
| 211 |
-
"""
|
| 212 |
try:
|
| 213 |
city_name = city_input.strip()
|
| 214 |
|
| 215 |
if not city_name:
|
| 216 |
-
return
|
| 217 |
|
| 218 |
-
# Search Chabad.org for the city
|
| 219 |
-
print(f"🔍 Searching for Shabbat times in: {city_name}")
|
| 220 |
city_url = self.search_city_on_chabad(city_name)
|
| 221 |
|
| 222 |
-
result_data = None
|
| 223 |
-
source = "Chabad.org"
|
| 224 |
-
|
| 225 |
if city_url:
|
| 226 |
try:
|
| 227 |
-
headers = {
|
| 228 |
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
| 229 |
-
}
|
| 230 |
response = requests.get(city_url, headers=headers, timeout=15)
|
| 231 |
|
| 232 |
if response.status_code == 200:
|
| 233 |
result_data = self.extract_candle_lighting_times(response.text, city_name)
|
| 234 |
-
print(f"📊 Extraction result: {result_data}")
|
| 235 |
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
print("🔄 Trying backup sources...")
|
| 242 |
-
backup_data = self.get_backup_times(city_name)
|
| 243 |
-
if backup_data and backup_data.get('found_data'):
|
| 244 |
-
result_data = backup_data
|
| 245 |
-
source = "Alternative Jewish calendar source"
|
| 246 |
|
| 247 |
-
|
| 248 |
-
if result_data and result_data.get('found_data'):
|
| 249 |
-
return self._format_success_response(result_data, source, city_url)
|
| 250 |
-
else:
|
| 251 |
-
return self._format_not_found_response(city_name, city_url)
|
| 252 |
|
| 253 |
except Exception as e:
|
| 254 |
-
|
| 255 |
-
return self._format_error_response(f"❌ An error occurred: {str(e)}")
|
| 256 |
|
| 257 |
-
def _format_success_response(self, data: Dict[str, Any]
|
| 258 |
-
"""Format successful response with Shabbat times"""
|
| 259 |
candle_time = data.get('candle_lighting_time', 'Not found')
|
| 260 |
havdalah_time = data.get('havdalah_time', 'Not found')
|
| 261 |
|
| 262 |
-
|
| 263 |
# 🕯️ Shabbat Candle Lighting Times
|
| 264 |
|
| 265 |
## 📍 Location: {data.get('city', 'Unknown')}
|
|
@@ -277,84 +149,32 @@ class ShabbatCandleLightingAgent:
|
|
| 277 |
## 📋 **Important Notes:**
|
| 278 |
- 🕯️ Light Shabbat candles **18 minutes before sunset** on Friday
|
| 279 |
- ⭐ Havdalah is performed after **3 stars appear** on Saturday night
|
| 280 |
-
-
|
| 281 |
-
- ⏰ Always verify times with your local rabbi or Jewish community
|
| 282 |
-
|
| 283 |
-
## 📚 **Source Information:**
|
| 284 |
-
**Data from:** {source}
|
| 285 |
-
**Retrieved:** {datetime.datetime.now().strftime('%B %d, %Y at %I:%M %p')}
|
| 286 |
-
{f"**URL:** {url}" if url else ""}
|
| 287 |
-
|
| 288 |
-
---
|
| 289 |
|
| 290 |
## 🙏 **Shabbat Shalom!**
|
| 291 |
-
*May your Shabbat be filled with peace, joy, and spiritual connection.*
|
| 292 |
-
|
| 293 |
-
**🔔 Reminder:** Set an alarm 30 minutes before candle lighting time to prepare properly.
|
| 294 |
"""
|
| 295 |
-
return response
|
| 296 |
|
| 297 |
-
def _format_not_found_response(self, city_name: str
|
| 298 |
-
"""Format response when times are not found"""
|
| 299 |
return f"""
|
| 300 |
# 🕯️ Shabbat Candle Lighting Times
|
| 301 |
|
| 302 |
## ❌ Times Not Found for {city_name}
|
| 303 |
|
| 304 |
-
We searched multiple sources but couldn't find specific Shabbat candle lighting times for **{city_name}**.
|
| 305 |
-
|
| 306 |
### 🔍 **What you can try:**
|
| 307 |
-
1. **Check spelling** - Make sure the city name is
|
| 308 |
2. **Add country** - Try "City, Country" format (e.g., "Paris, France")
|
| 309 |
3. **Use major city** - Try the nearest major city instead
|
| 310 |
-
4. **Alternative spellings** - Try different variations of the city name
|
| 311 |
|
| 312 |
-
### 📍 **General Guidelines
|
| 313 |
- 🕯️ **Candle lighting:** Typically 18 minutes before sunset on Friday
|
| 314 |
-
- ⭐ **Havdalah:** Approximately 25-42 minutes after sunset on Saturday
|
| 315 |
-
- 🌍 **Location matters:** Times vary significantly by geographic location
|
| 316 |
-
|
| 317 |
-
### 🌐 **Manual Resources:**
|
| 318 |
-
- 📱 **Chabad.org:** [Direct search]({url or 'https://www.chabad.org/calendar/candlelighting_cdo/aid/6226/jewish/Accurate-Shabbat-Candle-Lighting-Times.htm'})
|
| 319 |
-
- 📱 **Hebcal.com:** Alternative Jewish calendar
|
| 320 |
-
- 📞 **Local contacts:** Your synagogue or Chabad house
|
| 321 |
|
| 322 |
### 📞 **Local Resources:**
|
| 323 |
- Contact your local Chabad house or synagogue
|
| 324 |
-
-
|
| 325 |
-
- Consult your rabbi for precise local customs
|
| 326 |
-
|
| 327 |
-
---
|
| 328 |
-
|
| 329 |
-
**Retrieved:** {datetime.datetime.now().strftime('%B %d, %Y at %I:%M %p')}
|
| 330 |
-
"""
|
| 331 |
-
|
| 332 |
-
def _format_error_response(self, error_message: str) -> str:
|
| 333 |
-
"""Format error response"""
|
| 334 |
-
return f"""
|
| 335 |
-
# 🕯️ Shabbat Candle Lighting Times
|
| 336 |
-
|
| 337 |
-
## ⚠️ Error
|
| 338 |
-
|
| 339 |
-
{error_message}
|
| 340 |
-
|
| 341 |
-
### 🔄 **Please try:**
|
| 342 |
-
1. **Refresh** and try again
|
| 343 |
-
2. **Check internet connection**
|
| 344 |
-
3. **Use a different city name**
|
| 345 |
-
4. **Visit Chabad.org directly:** [Candle Lighting Times](https://www.chabad.org/calendar/candlelighting_cdo/aid/6226/jewish/Accurate-Shabbat-Candle-Lighting-Times.htm)
|
| 346 |
-
|
| 347 |
-
### 📞 **Alternative:**
|
| 348 |
-
Contact your local synagogue or Chabad house for accurate times.
|
| 349 |
-
|
| 350 |
-
---
|
| 351 |
-
|
| 352 |
-
**Time:** {datetime.datetime.now().strftime('%B %d, %Y at %I:%M %p')}
|
| 353 |
"""
|
| 354 |
|
| 355 |
def create_shabbat_interface():
|
| 356 |
-
"""Create the Gradio interface for the Shabbat Candle Lighting Agent"""
|
| 357 |
-
|
| 358 |
agent = ShabbatCandleLightingAgent()
|
| 359 |
|
| 360 |
with gr.Blocks(title="🕯️ Shabbat Candle Lighting Times", theme=gr.themes.Soft()) as app:
|
|
@@ -362,21 +182,14 @@ def create_shabbat_interface():
|
|
| 362 |
gr.Markdown("""
|
| 363 |
# 🕯️ Shabbat Candle Lighting Times AI Agent
|
| 364 |
|
| 365 |
-
**Get accurate Shabbat candle lighting and Havdalah times
|
| 366 |
-
|
| 367 |
-
This AI agent searches Chabad.org and other reliable Jewish sources to find precise ritual times for your location.
|
| 368 |
-
|
| 369 |
-
---
|
| 370 |
""")
|
| 371 |
|
| 372 |
with gr.Row():
|
| 373 |
-
with gr.Column(
|
| 374 |
-
gr.Markdown("### 📍 Location Input")
|
| 375 |
-
|
| 376 |
city_input = gr.Textbox(
|
| 377 |
label="🏙️ City Name",
|
| 378 |
-
placeholder="Enter city name (e.g., 'New York', 'London, UK'
|
| 379 |
-
info="Please enter your city name to get accurate Shabbat times",
|
| 380 |
lines=1
|
| 381 |
)
|
| 382 |
|
|
@@ -385,136 +198,30 @@ def create_shabbat_interface():
|
|
| 385 |
variant="primary",
|
| 386 |
size="lg"
|
| 387 |
)
|
| 388 |
-
|
| 389 |
-
gr.Markdown("""
|
| 390 |
-
### 💡 **City Input Tips:**
|
| 391 |
-
- Use full city names for better results
|
| 392 |
-
- Add country for common city names (e.g., "Paris, France")
|
| 393 |
-
- Try nearest major city if yours isn't found
|
| 394 |
-
- Use English city names for best search results
|
| 395 |
-
|
| 396 |
-
### 🇮🇱 **Popular Israeli Cities:**
|
| 397 |
-
Jerusalem, Tel Aviv, Haifa, Beer Sheva, Netanya, Ashdod,
|
| 398 |
-
Petah Tikva, Ramat Gan, Bnei Brak, Herzliya
|
| 399 |
-
|
| 400 |
-
### 🌍 **International Examples:**
|
| 401 |
-
London, UK • Paris, France • New York, USA • Toronto, Canada
|
| 402 |
-
""")
|
| 403 |
|
| 404 |
-
with gr.Column(
|
| 405 |
output_area = gr.Markdown(
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
# 🕯️ Welcome!
|
| 409 |
-
|
| 410 |
-
👆 **Enter your city name above and click 'Get Shabbat Times'**
|
| 411 |
-
|
| 412 |
-
# Or click one of the popular cities below for a quick search.
|
| 413 |
-
|
| 414 |
-
# ### 🇮🇱 **Common Israeli Cities:**
|
| 415 |
-
# Use any of these city names: Jerusalem, Tel Aviv, Haifa, Beer Sheva, Netanya, Ashdod
|
| 416 |
-
|
| 417 |
-
# ### 🌍 **International Cities:**
|
| 418 |
-
# Include country name: London, UK • Paris, France • New York, USA
|
| 419 |
-
|
| 420 |
-
# ---
|
| 421 |
-
|
| 422 |
-
# **🕯️ Every Friday evening, Jewish families around the world light candles to welcome Shabbat**
|
| 423 |
-
# """
|
| 424 |
-
# )
|
| 425 |
-
|
| 426 |
-
# # Example buttons for popular cities
|
| 427 |
-
# gr.Markdown("### 🌍 Quick Search - Popular Cities:")
|
| 428 |
-
|
| 429 |
-
# with gr.Row():
|
| 430 |
-
# # Israeli cities first for the detected user
|
| 431 |
-
# popular_cities = [
|
| 432 |
-
# "Jerusalem, Israel",
|
| 433 |
-
# "Tel Aviv, Israel",
|
| 434 |
-
# "Haifa, Israel",
|
| 435 |
-
# "Beer Sheva, Israel",
|
| 436 |
-
# "Netanya, Israel",
|
| 437 |
-
# "Ashdod, Israel",
|
| 438 |
-
# # International cities
|
| 439 |
-
# "New York, NY",
|
| 440 |
-
# "London, UK",
|
| 441 |
-
# "Paris, France",
|
| 442 |
-
# "Toronto, Canada",
|
| 443 |
-
# "Los Angeles, CA",
|
| 444 |
-
# "Miami, FL"
|
| 445 |
-
# ]
|
| 446 |
-
|
| 447 |
-
# example_btns = []
|
| 448 |
-
# for i in range(0, len(popular_cities), 4):
|
| 449 |
-
# with gr.Row():
|
| 450 |
-
# for city in popular_cities[i:i+4]:
|
| 451 |
-
# btn = gr.Button(f"📍 {city}", size="sm")
|
| 452 |
-
# example_btns.append((btn, city))
|
| 453 |
-
|
| 454 |
-
# # Button click handlers
|
| 455 |
-
# search_btn.click(
|
| 456 |
-
# fn=agent.get_shabbat_times,
|
| 457 |
-
# inputs=[city_input],
|
| 458 |
-
# outputs=output_area
|
| 459 |
-
# )
|
| 460 |
-
|
| 461 |
-
# # Example button handlers
|
| 462 |
-
# for btn, city in example_btns:
|
| 463 |
-
# btn.click(
|
| 464 |
-
# fn=lambda c=city: agent.get_shabbat_times(c),
|
| 465 |
-
# outputs=output_area
|
| 466 |
-
# )
|
| 467 |
-
|
| 468 |
-
# # Resources and information
|
| 469 |
-
# with gr.Accordion("📚 Resources & Information", open=False):
|
| 470 |
-
# gr.Markdown("""
|
| 471 |
-
# ### 🕯️ **About Shabbat Candle Lighting:**
|
| 472 |
-
|
| 473 |
-
# **Candle Lighting Time:**
|
| 474 |
-
# - Performed on Friday evening before sunset
|
| 475 |
-
# - Typically 18 minutes before sunset (varies by community)
|
| 476 |
-
# - Marks the beginning of Shabbat
|
| 477 |
-
# - Usually done by the woman of the house
|
| 478 |
-
|
| 479 |
-
# **Havdalah Time:**
|
| 480 |
-
# - Performed on Saturday evening after sunset
|
| 481 |
-
# - When three medium-sized stars are visible in the sky
|
| 482 |
-
# - Marks the end of Shabbat
|
| 483 |
-
# - Includes blessings over wine, spices, and candle
|
| 484 |
-
|
| 485 |
-
# ### 🌐 **Data Sources:**
|
| 486 |
-
# - **Primary:** [Chabad.org](https://www.chabad.org/calendar/candlelighting_cdo/aid/6226/jewish/Accurate-Shabbat-Candle-Lighting-Times.htm)
|
| 487 |
-
# - **Backup:** Hebcal.com and other Jewish calendar sites
|
| 488 |
-
# - **Method:** Intelligent web scraping with multiple fallback strategies
|
| 489 |
-
|
| 490 |
-
# ### ⚠️ **Important Disclaimers:**
|
| 491 |
-
# - Always consult your local rabbi for specific customs
|
| 492 |
-
# - Times may vary based on local Jewish community practices
|
| 493 |
-
# - This tool is for reference only - not a substitute for religious guidance
|
| 494 |
-
# - Some communities light candles at different intervals before sunset
|
| 495 |
-
|
| 496 |
-
# ### 🔧 **Technical Features:**
|
| 497 |
-
# - Multiple search strategies for maximum success on Chabad.org
|
| 498 |
-
# - Intelligent pattern matching to extract candle lighting times
|
| 499 |
-
# - Fallback to alternative Jewish calendar sources if needed
|
| 500 |
-
# - Manual city input for maximum accuracy and reliability
|
| 501 |
-
# """)
|
| 502 |
-
|
| 503 |
-
# gr.Markdown("""
|
| 504 |
-
# ---
|
| 505 |
-
|
| 506 |
-
# **🙏 Shabbat Shalom!** | Built with ❤️ for the global Jewish community
|
| 507 |
|
| 508 |
-
|
| 509 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 510 |
|
| 511 |
return app
|
| 512 |
|
| 513 |
-
# Main execution
|
| 514 |
if __name__ == "__main__":
|
| 515 |
app = create_shabbat_interface()
|
| 516 |
-
|
| 517 |
-
# Launch the application
|
| 518 |
app.launch(
|
| 519 |
server_name="0.0.0.0",
|
| 520 |
server_port=7860,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
import datetime
|
| 4 |
import re
|
| 5 |
from bs4 import BeautifulSoup
|
| 6 |
from typing import Optional, Dict, Any
|
| 7 |
+
from urllib.parse import quote
|
| 8 |
|
| 9 |
class ShabbatCandleLightingAgent:
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def __init__(self):
|
| 12 |
self.base_url = "https://www.chabad.org"
|
| 13 |
self.candle_lighting_url = "https://www.chabad.org/calendar/candlelighting_cdo/aid/6226/jewish/Accurate-Shabbat-Candle-Lighting-Times.htm"
|
| 14 |
|
| 15 |
def search_city_on_chabad(self, city_name: str) -> Optional[str]:
|
|
|
|
|
|
|
|
|
|
| 16 |
try:
|
|
|
|
| 17 |
city_clean = city_name.strip()
|
| 18 |
search_query = quote(city_clean)
|
| 19 |
|
| 20 |
headers = {
|
| 21 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
}
|
| 23 |
|
| 24 |
+
search_urls = [
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
f"https://www.chabad.org/calendar/candlelighting_cdo/aid/6226/jewish/Accurate-Shabbat-Candle-Lighting-Times.htm?city={search_query}",
|
| 26 |
+
f"https://www.chabad.org/calendar/candlelighting_cdo/jewish/{search_query}.htm",
|
| 27 |
+
self.candle_lighting_url
|
|
|
|
|
|
|
| 28 |
]
|
| 29 |
|
| 30 |
+
for url in search_urls:
|
| 31 |
try:
|
| 32 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 33 |
if response.status_code == 200:
|
|
|
|
| 34 |
content_lower = response.text.lower()
|
| 35 |
+
if any(keyword in content_lower for keyword in ['candle lighting', 'shabbat', 'friday']):
|
| 36 |
return url
|
| 37 |
+
except Exception:
|
|
|
|
| 38 |
continue
|
| 39 |
+
|
| 40 |
+
except Exception:
|
| 41 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
return self.candle_lighting_url
|
| 44 |
|
| 45 |
def extract_candle_lighting_times(self, html_content: str, city_name: str) -> Dict[str, Any]:
|
|
|
|
|
|
|
|
|
|
| 46 |
try:
|
| 47 |
soup = BeautifulSoup(html_content, 'html.parser')
|
| 48 |
text = soup.get_text().lower()
|
| 49 |
|
|
|
|
| 50 |
candle_patterns = [
|
| 51 |
r'candle\s+lighting[:\s]*(\d{1,2}:\d{2}(?:\s*[ap]m)?)',
|
| 52 |
r'light\s+candles[:\s]*(\d{1,2}:\d{2}(?:\s*[ap]m)?)',
|
| 53 |
+
r'friday[:\s]*(\d{1,2}:\d{2}(?:\s*[ap]m)?)'
|
|
|
|
|
|
|
|
|
|
| 54 |
]
|
| 55 |
|
|
|
|
| 56 |
havdalah_patterns = [
|
| 57 |
r'havdalah[:\s]*(\d{1,2}:\d{2}(?:\s*[ap]m)?)',
|
| 58 |
+
r'saturday[:\s]*(\d{1,2}:\d{2}(?:\s*[ap]m)?)'
|
|
|
|
|
|
|
| 59 |
]
|
| 60 |
|
|
|
|
| 61 |
candle_time = None
|
| 62 |
for pattern in candle_patterns:
|
| 63 |
match = re.search(pattern, text, re.IGNORECASE)
|
|
|
|
| 65 |
candle_time = match.group(1)
|
| 66 |
break
|
| 67 |
|
|
|
|
| 68 |
havdalah_time = None
|
| 69 |
for pattern in havdalah_patterns:
|
| 70 |
match = re.search(pattern, text, re.IGNORECASE)
|
|
|
|
| 72 |
havdalah_time = match.group(1)
|
| 73 |
break
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
now = datetime.datetime.now()
|
| 76 |
days_until_friday = (4 - now.weekday()) % 7
|
| 77 |
+
if days_until_friday == 0 and now.hour >= 18:
|
| 78 |
days_until_friday = 7
|
| 79 |
|
| 80 |
friday = now + datetime.timedelta(days=days_until_friday)
|
|
|
|
| 86 |
'havdalah_time': havdalah_time,
|
| 87 |
'friday_date': friday.strftime('%B %d, %Y'),
|
| 88 |
'saturday_date': saturday.strftime('%B %d, %Y'),
|
| 89 |
+
'found_data': bool(candle_time or havdalah_time)
|
|
|
|
| 90 |
}
|
| 91 |
|
| 92 |
+
except Exception:
|
|
|
|
| 93 |
return {
|
| 94 |
'city': city_name,
|
| 95 |
'candle_lighting_time': None,
|
| 96 |
'havdalah_time': None,
|
| 97 |
'friday_date': None,
|
| 98 |
'saturday_date': None,
|
| 99 |
+
'found_data': False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
def get_shabbat_times(self, city_input: str = "") -> str:
|
|
|
|
|
|
|
|
|
|
| 103 |
try:
|
| 104 |
city_name = city_input.strip()
|
| 105 |
|
| 106 |
if not city_name:
|
| 107 |
+
return "❌ Please enter a city name to search for Shabbat times."
|
| 108 |
|
|
|
|
|
|
|
| 109 |
city_url = self.search_city_on_chabad(city_name)
|
| 110 |
|
|
|
|
|
|
|
|
|
|
| 111 |
if city_url:
|
| 112 |
try:
|
| 113 |
+
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
|
|
|
|
|
|
|
| 114 |
response = requests.get(city_url, headers=headers, timeout=15)
|
| 115 |
|
| 116 |
if response.status_code == 200:
|
| 117 |
result_data = self.extract_candle_lighting_times(response.text, city_name)
|
|
|
|
| 118 |
|
| 119 |
+
if result_data and result_data.get('found_data'):
|
| 120 |
+
return self._format_success_response(result_data)
|
| 121 |
+
|
| 122 |
+
except Exception:
|
| 123 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
return self._format_not_found_response(city_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
except Exception as e:
|
| 128 |
+
return f"❌ An error occurred: {str(e)}"
|
|
|
|
| 129 |
|
| 130 |
+
def _format_success_response(self, data: Dict[str, Any]) -> str:
|
|
|
|
| 131 |
candle_time = data.get('candle_lighting_time', 'Not found')
|
| 132 |
havdalah_time = data.get('havdalah_time', 'Not found')
|
| 133 |
|
| 134 |
+
return f"""
|
| 135 |
# 🕯️ Shabbat Candle Lighting Times
|
| 136 |
|
| 137 |
## 📍 Location: {data.get('city', 'Unknown')}
|
|
|
|
| 149 |
## 📋 **Important Notes:**
|
| 150 |
- 🕯️ Light Shabbat candles **18 minutes before sunset** on Friday
|
| 151 |
- ⭐ Havdalah is performed after **3 stars appear** on Saturday night
|
| 152 |
+
- ⏰ Always verify times with your local rabbi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
## 🙏 **Shabbat Shalom!**
|
|
|
|
|
|
|
|
|
|
| 155 |
"""
|
|
|
|
| 156 |
|
| 157 |
+
def _format_not_found_response(self, city_name: str) -> str:
|
|
|
|
| 158 |
return f"""
|
| 159 |
# 🕯️ Shabbat Candle Lighting Times
|
| 160 |
|
| 161 |
## ❌ Times Not Found for {city_name}
|
| 162 |
|
|
|
|
|
|
|
| 163 |
### 🔍 **What you can try:**
|
| 164 |
+
1. **Check spelling** - Make sure the city name is correct
|
| 165 |
2. **Add country** - Try "City, Country" format (e.g., "Paris, France")
|
| 166 |
3. **Use major city** - Try the nearest major city instead
|
|
|
|
| 167 |
|
| 168 |
+
### 📍 **General Guidelines:**
|
| 169 |
- 🕯️ **Candle lighting:** Typically 18 minutes before sunset on Friday
|
| 170 |
+
- ⭐ **Havdalah:** Approximately 25-42 minutes after sunset on Saturday
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
### 📞 **Local Resources:**
|
| 173 |
- Contact your local Chabad house or synagogue
|
| 174 |
+
- Visit [Chabad.org](https://www.chabad.org/calendar/candlelighting_cdo/aid/6226/jewish/Accurate-Shabbat-Candle-Lighting-Times.htm) directly
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
"""
|
| 176 |
|
| 177 |
def create_shabbat_interface():
|
|
|
|
|
|
|
| 178 |
agent = ShabbatCandleLightingAgent()
|
| 179 |
|
| 180 |
with gr.Blocks(title="🕯️ Shabbat Candle Lighting Times", theme=gr.themes.Soft()) as app:
|
|
|
|
| 182 |
gr.Markdown("""
|
| 183 |
# 🕯️ Shabbat Candle Lighting Times AI Agent
|
| 184 |
|
| 185 |
+
**Get accurate Shabbat candle lighting and Havdalah times worldwide**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
""")
|
| 187 |
|
| 188 |
with gr.Row():
|
| 189 |
+
with gr.Column():
|
|
|
|
|
|
|
| 190 |
city_input = gr.Textbox(
|
| 191 |
label="🏙️ City Name",
|
| 192 |
+
placeholder="Enter city name (e.g., 'Jerusalem', 'New York', 'London, UK')",
|
|
|
|
| 193 |
lines=1
|
| 194 |
)
|
| 195 |
|
|
|
|
| 198 |
variant="primary",
|
| 199 |
size="lg"
|
| 200 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
+
with gr.Column():
|
| 203 |
output_area = gr.Markdown(
|
| 204 |
+
value="# 🕯️ Welcome!\n\n👆 **Enter your city name and click 'Get Shabbat Times'**"
|
| 205 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
+
with gr.Row():
|
| 208 |
+
cities = ["Jerusalem", "Tel Aviv", "New York", "London, UK", "Paris, France", "Toronto, Canada"]
|
| 209 |
+
for city in cities:
|
| 210 |
+
btn = gr.Button(f"📍 {city}", size="sm")
|
| 211 |
+
btn.click(fn=lambda c=city: agent.get_shabbat_times(c), outputs=output_area)
|
| 212 |
+
|
| 213 |
+
search_btn.click(
|
| 214 |
+
fn=agent.get_shabbat_times,
|
| 215 |
+
inputs=[city_input],
|
| 216 |
+
outputs=output_area
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
gr.Markdown("**🙏 Shabbat Shalom!**")
|
| 220 |
|
| 221 |
return app
|
| 222 |
|
|
|
|
| 223 |
if __name__ == "__main__":
|
| 224 |
app = create_shabbat_interface()
|
|
|
|
|
|
|
| 225 |
app.launch(
|
| 226 |
server_name="0.0.0.0",
|
| 227 |
server_port=7860,
|