Spaces:
Build error
Build error
File size: 18,422 Bytes
a05515b 8476633 a05515b 8476633 a05515b 6520230 a05515b 9e6b464 a05515b 9e6b464 a05515b 9e6b464 a05515b 9e6b464 a05515b 9e6b464 a05515b 9e6b464 a05515b 354d0de a05515b 354d0de 9e6b464 a05515b 354d0de a05515b 9e6b464 a05515b 9e6b464 a05515b 354d0de a05515b 354d0de a05515b 7a7223a 88daa62 2816cdf a05515b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
# app.py
import gradio as gr
import feedparser
from bs4 import BeautifulSoup
from datetime import datetime, timedelta
import pytz
from typing import List, Dict
from sentence_transformers import SentenceTransformer
import chromadb
import gc
import json
import os
class BrockEventsRAG:
def __init__(self):
"""Initialize the RAG system with improved caching"""
self.model = SentenceTransformer('all-MiniLM-L6-v2')
self.embeddings = HuggingFaceEmbeddings(model_name='all-MiniLM-L6-v2')
# ChromaDB client setup
self.chroma_client = chromadb.Client(Settings(persist_directory="chroma_db", chroma_db_impl="duckdb+parquet"))
# LLM model setup
self.tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")
self.llm = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
# Get current date range
self.eastern = pytz.timezone('America/New_York')
self.today = datetime.now(self.eastern).replace(hour=0, minute=0, second=0, microsecond=0)
self.date_range_end = self.today + timedelta(days=14)
# Cache directory setup
os.makedirs("cache", exist_ok=True)
self.cache_file = "cache/events_cache.json"
# Initialize or reset collection
try:
self.collection = self.chroma_client.create_collection(
name="brock_events",
metadata={"description": "Brock University Events Database"}
)
except Exception:
self.chroma_client.delete_collection("brock_events")
self.collection = self.chroma_client.create_collection(
name="brock_events",
metadata={"description": "Brock University Events Database"}
)
# Load initial events
self.update_database()
def fetch_rss_feed(self, url: str) -> List[Dict]:
"""Fetch and parse RSS feed from the given URL"""
try:
feed = feedparser.parse(url)
entries = feed.entries
print(f"Fetched {len(entries)} entries from the feed.")
return entries
except Exception as e:
print(f"Error fetching RSS feed: {e}")
return []
def parse_event_datetime(self, entry) -> tuple:
"""Parse start and end times from both RSS and HTML"""
try:
# First try to get times from the events namespace
start_time = entry.get('start', None)
end_time = entry.get('end', None)
# Parse the RSS feed times if available
if start_time:
start_dt = datetime.strptime(start_time, '%a, %d %b %Y %H:%M:%S %Z')
start_dt = pytz.UTC.localize(start_dt).astimezone(self.eastern)
else:
start_dt = None
if end_time:
end_dt = datetime.strptime(end_time, '%a, %d %b %Y %H:%M:%S %Z')
end_dt = pytz.UTC.localize(end_dt).astimezone(self.eastern)
else:
end_dt = None
# If we didn't get times from RSS, try HTML
if not start_dt or not end_dt:
soup = BeautifulSoup(entry.description, 'html.parser')
start_elem = soup.find('time', class_='dt-start')
end_elem = soup.find('time', class_='dt-end')
if start_elem and 'datetime' in start_elem.attrs:
dt_str = start_elem['datetime'].split('.')[0]
start_dt = datetime.strptime(dt_str, '%Y-%m-%dT%H:%M:%S')
start_dt = self.eastern.localize(start_dt)
if end_elem and 'datetime' in end_elem.attrs:
dt_str = end_elem['datetime'].split('.')[0]
end_dt = datetime.strptime(dt_str, '%Y-%m-%dT%H:%M:%S')
end_dt = self.eastern.localize(end_dt)
return start_dt, end_dt
except Exception as e:
print(f"Error parsing dates: {e}")
return None, None
def get_location(self, entry) -> str:
"""Extract location from both RSS and HTML"""
try:
# First try RSS events namespace
location = entry.get('location', None)
# If not found, try HTML
if not location:
soup = BeautifulSoup(entry.description, 'html.parser')
location_elem = soup.find('span', class_='p-location')
if location_elem:
location = location_elem.get_text().strip()
return location if location else "Location not specified"
except Exception as e:
print(f"Error getting location: {e}")
return "Location not specified"
def process_event(self, entry) -> Dict:
"""Process a single event entry"""
try:
# Get times
start_time, end_time = self.parse_event_datetime(entry)
# Skip if event is not in our date range
if not start_time or not self.is_event_in_range(start_time):
return None
# Get location
location = self.get_location(entry)
# Get categories
categories = [tag.term for tag in entry.get('tags', [])]
categories_str = '; '.join(categories) if categories else 'No categories'
# Get hosts
hosts = entry.get('host', [])
if not isinstance(hosts, list):
hosts = [hosts]
hosts_str = '; '.join(hosts) if hosts else 'No host specified'
# Clean description
soup = BeautifulSoup(entry.description, 'html.parser')
description = ' '.join(soup.get_text().split())
return {
'title': entry.title,
'start_time': start_time,
'end_time': end_time,
'location': location,
'categories': categories_str,
'hosts': hosts_str,
'description': description,
'link': entry.link,
'guid': entry.guid
}
except Exception as e:
print(f"Error processing event {entry.get('title', 'Unknown')}: {e}")
return None
def is_event_in_range(self, event_time: datetime) -> bool:
"""Check if event falls within our date range"""
if not event_time:
return False
return self.today <= event_time <= self.date_range_end
def format_event_text(self, event: Dict) -> str:
"""Format event information for embedding"""
return f"""
Event: {event['title']}
Date: {event['start_time'].strftime('%A, %B %d, %Y')}
Time: {event['start_time'].strftime('%I:%M %p')} to {event['end_time'].strftime('%I:%M %p') if event['end_time'] else 'not specified'}
Location: {event['location']}
Categories: {event['categories']}
Hosted by: {event['hosts']}
Description: {event['description'][:500]}
"""
def update_database(self):
"""Update database with events in date range"""
print("Fetching events...")
feed = feedparser.parse("https://experiencebu.brocku.ca/events.rss")
print(f"Found {len(feed.entries)} total events")
# Process events
valid_events = []
for entry in feed.entries:
event = self.process_event(entry)
if event: # Only include events in our date range
valid_events.append(event)
print(f"Found {len(valid_events)} events in the next 14 days")
if not valid_events:
print("No events found in date range")
return
# Prepare data for database
documents = [self.format_event_text(event) for event in valid_events]
metadatas = [{
'title': event['title'],
'date': event['start_time'].strftime('%Y-%m-%d'),
'time': event['start_time'].strftime('%I:%M %p'),
'location': event['location'],
'categories': event['categories'],
'link': event['link']
} for event in valid_events]
ids = [f"event_{i}" for i in range(len(valid_events))]
# Generate embeddings and add to database
try:
embeddings = self.model.encode(documents)
self.collection.add(
documents=documents,
embeddings=embeddings.tolist(),
metadatas=metadatas,
ids=ids
)
print(f"Successfully added {len(valid_events)} events to database")
except Exception as e:
print(f"Error adding events to database: {e}")
# Save to cache
cache_data = {
'last_update': datetime.now().isoformat(),
'events': valid_events
}
self.save_cache(cache_data)
# Clean up
gc.collect()
def query(self, question: str, n_results: int = 3) -> List[Dict]:
"""Query the database"""
try:
question_embedding = self.model.encode(question)
results = self.collection.query(
query_embeddings=[question_embedding.tolist()],
n_results=n_results,
include=['documents', 'metadatas', 'distances']
)
return results
except Exception as e:
print(f"Error during query: {e}")
return None
def generate_response_with_llm(events: List[Dict]) -> str:
"""Use the LLM to generate a natural language response for the given events."""
try:
if not events:
input_text = "There are no events matching the query. How should I respond?"
else:
event_summaries = "\n".join([
f"Event: {event['title']}. Start: {event['start_time']}, Location: {event['location']}."
for event in events
])
input_text = f"Format this information into a friendly response: {event_summaries}"
inputs = self.tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
outputs = self.llm.generate(**inputs)
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
return response
except Exception as e:
print(f"Error generating response: {e}")
return "Sorry, I couldn't generate a response."
def generate_response(self, question: str, history: list) -> str:
"""Generate a response based on the query and chat history"""
try:
# Query the database
results = self.query(question)
if not results or not results['documents'] or not results['documents'][0]:
return "I couldn't find any events matching your query. Try asking about upcoming events in a different way!"
# Analyze the question type
question_lower = question.lower()
is_time_query = any(word in question_lower for word in ['when', 'time', 'date', 'week', 'today', 'tomorrow'])
is_location_query = any(word in question_lower for word in ['where', 'location', 'place', 'building', 'room'])
# Format the response
response = generate_response_with_llm(matched_events)
# Add top 3 matching events
for i, (doc, metadata) in enumerate(zip(results['documents'][0][:3], results['metadatas'][0][:3]), 1):
response += f"{i}. **{metadata['title']}**\n"
response += f"π
{metadata['date']} at {metadata['time']}\n"
response += f"π {metadata['location']}\n"
if 'categories' in metadata:
response += f"π·οΈ {metadata['categories']}\n"
response += f"π More info: {metadata['link']}\n\n"
# Add a helpful prompt
response += "\nYou can ask me for more specific details about any of these events!"
return response
except Exception as e:
print(f"Error generating response: {e}")
return "I encountered an error while searching for events. Please try asking in a different way."
def save_cache(self, data: dict):
"""Save events data to cache file"""
try:
# Convert datetime objects to strings for JSON serialization
serializable_data = {
'last_update': data['last_update'],
'events': []
}
for event in data['events']:
event_copy = event.copy()
# Convert datetime objects to strings
if event_copy.get('start_time'):
event_copy['start_time'] = event_copy['start_time'].isoformat()
if event_copy.get('end_time'):
event_copy['end_time'] = event_copy['end_time'].isoformat()
serializable_data['events'].append(event_copy)
with open(self.cache_file, 'w', encoding='utf-8') as f:
json.dump(serializable_data, f, ensure_ascii=False, indent=2)
print(f"Cache saved successfully to {self.cache_file}")
except Exception as e:
print(f"Error saving cache: {e}")
"""
def load_cache(self) -> dict:
#Load and parse cached events data
try:
if os.path.exists(self.cache_file):
with open(self.cache_file, 'r', encoding='utf-8') as f:
data = json.load(f)
# Convert string timestamps back to datetime objects
for event in data['events']:
if event.get('start_time'):
event['start_time'] = datetime.fromisoformat(event['start_time'])
if event.get('end_time'):
event['end_time'] = datetime.fromisoformat(event['end_time'])
return data
return {'last_update': None, 'events': []}
except Exception as e:
print(f"Error loading cache: {e}")
return {'last_update': None, 'events': []}
def should_update_cache(self) -> bool:
#Check if cache needs updating (older than 24 hours)
try:
cached_data = self.load_cache()
if not cached_data['last_update']:
return True
last_update = datetime.fromisoformat(cached_data['last_update'])
time_since_update = datetime.now() - last_update
return time_since_update.total_seconds() > 86400 # 24 hours
except Exception as e:
print(f"Error checking cache: {e}")
return True
"""
def create_demo():
# Initialize the RAG system
rag_system = BrockEventsRAG()
# Custom CSS for better appearance
custom_css = """
.gr-button-primary {
background-color: #8b0000 !important;
border-color: #8b0000 !important;
}
"""
# Create the Gradio interface
with gr.Blocks(css=custom_css) as demo:
gr.Markdown("""
# π Brock University Events Assistant
Ask me about upcoming events at Brock! I can help you discover:
- Academic workshops
- Student activities
- Campus events
- And more!
""")
chatbot = gr.Chatbot(
label="Chat History",
height=400,
bubble_full_width=False,
)
with gr.Row():
msg = gr.Textbox(
label="Your Question",
placeholder="e.g., What events are happening this week?",
scale=4
)
submit = gr.Button("Ask", scale=1, variant="primary")
with gr.Row():
clear = gr.Button("Clear Chat")
refresh = gr.Button("Refresh Events")
# Event handlers
def respond(message, history):
bot_message = rag_system.generate_response(message, history)
history.append({"role": "user", "content": message})
history.append({"role": "assistant", "content": bot_message})
return "", history
# In the create_demo function:
chatbot = gr.Chatbot(
label="Chat History",
height=400,
bubble_full_width=False,
type="messages" # Use new message format
)
def refresh_events():
rag_system.update_database()
return "Events database has been refreshed!"
submit.click(respond, [msg, chatbot], [msg, chatbot])
msg.submit(respond, [msg, chatbot], [msg, chatbot])
clear.click(lambda: None, None, chatbot)
refresh.click(refresh_events, None, msg)
# Example questions
gr.Examples(
examples=[
"What events are happening this week?",
"Are there any workshops in the library?",
"Tell me about upcoming career events",
"What's happening in the MakerSpace?",
"Any student club meetings soon?",
],
inputs=msg
)
gr.Markdown("""
### Tips:
- Ask about specific dates, locations, or event types
- You can refresh the events database using the button above
- Click on event links to get more details on ExperienceBU
Data is refreshed automatically every 24 hours. Events shown are for the next 14 days.
""")
return demo
if __name__ == "__main__":
demo = create_demo()
demo.launch(
server_name="0.0.0.0", # Required for Spaces
server_port=7860, # Default port
share=False, # Don't create a public link
max_threads=40 # Handle concurrent users
) |