AryanJh commited on
Commit
14bdd38
Β·
verified Β·
1 Parent(s): 6471f9e

Inference time cutodonw

Browse files
Files changed (1) hide show
  1. event_matcher.py +69 -70
event_matcher.py CHANGED
@@ -28,90 +28,89 @@ class EventMatcher:
28
  def query(self, user_query: str) -> str:
29
  """Main query method called by the app"""
30
  try:
31
- # Update events if needed
32
- self.update_events()
33
-
34
- # Find matching events
 
 
 
 
 
 
35
  matched_events = self.find_matching_events(user_query)
36
 
37
  if not matched_events:
38
  return "I couldn't find any events matching your query. Try asking in a different way!"
39
 
40
- # Format the response
41
- return self.format_response(matched_events, user_query)
42
-
43
- except Exception as e:
44
- print(f"Error in query: {e}")
45
- return "I encountered an error processing your query. Please try again!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
- def find_matching_events(self, query: str) -> List[Tuple[Dict, float]]:
48
- """Find events matching the query"""
49
- matched_events = []
50
 
51
- for event in self.events:
52
- # Calculate various similarity scores
53
- title_score = fuzz.token_set_ratio(query.lower(), event['title'].lower()) / 100
54
- desc_score = fuzz.token_set_ratio(query.lower(), event['description'].lower()) / 100
55
- location_score = fuzz.token_set_ratio(query.lower(), event['location'].lower()) / 100
56
- categories_score = fuzz.token_set_ratio(query.lower(), event['categories'].lower()) / 100
57
- hosts_score = fuzz.token_set_ratio(query.lower(), event['hosts'].lower()) / 100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  # Weight the scores
60
  total_score = (
61
- title_score * 0.35 +
62
- desc_score * 0.25 +
63
- location_score * 0.20 +
64
- categories_score * 0.15 +
65
- hosts_score * 0.05
66
  )
67
 
68
  if total_score > 0.3: # Threshold for relevance
69
  matched_events.append((event, total_score))
70
-
71
- # Sort by score and get top matches
72
- matched_events.sort(key=lambda x: x[1], reverse=True)
73
- return matched_events[:3]
74
-
75
- def format_response(self, matched_events: List[Tuple[Dict, float]], query: str) -> str:
76
- """Format the response with matched events"""
77
- try:
78
- # Prepare text for T5 model
79
- events_text = ""
80
- for event, score in matched_events:
81
- events_text += f"""
82
- πŸ“… {event['title']}
83
- Date: {event['start_time'].strftime('%A, %B %d, %Y')}
84
- Time: {event['start_time'].strftime('%I:%M %p')}
85
- πŸ“ Location: {event['location']}
86
- 🏷️ Categories: {event['categories']}
87
- πŸ‘₯ Hosted by: {event['hosts']}
88
-
89
- {event['description'][:200]}...
90
-
91
- πŸ”— More info: {event['link']}
92
- """
93
-
94
- # Generate response using T5
95
- input_text = f"query: {query} context: {events_text}"
96
- inputs = self.tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
97
- outputs = self.model.generate(
98
- inputs.input_ids,
99
- max_length=150,
100
- num_beams=4,
101
- temperature=0.7,
102
- no_repeat_ngram_size=2
103
- )
104
-
105
- response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
106
-
107
- # Combine T5 response with event details
108
- final_response = f"{response}\n\nHere are the matching events:{events_text}"
109
-
110
- return final_response
111
-
112
- except Exception as e:
113
- print(f"Error formatting response: {e}")
114
- return "Here are some relevant events I found:" + events_text
115
 
116
  def parse_event_datetime(self, entry) -> Tuple[Optional[datetime], Optional[datetime]]:
117
  """Parse event dates from RSS feed"""
 
28
  def query(self, user_query: str) -> str:
29
  """Main query method called by the app"""
30
  try:
31
+ # Update events if needed - make this non-blocking
32
+ if self.last_update is None or (datetime.now() - self.last_update).seconds >= 3600:
33
+ # Start a background task to update events
34
+ self.update_events()
35
+
36
+ # Quick response for empty query
37
+ if not user_query.strip():
38
+ return "Please ask me about events at Brock University!"
39
+
40
+ # Find matching events - optimize by limiting initial search
41
  matched_events = self.find_matching_events(user_query)
42
 
43
  if not matched_events:
44
  return "I couldn't find any events matching your query. Try asking in a different way!"
45
 
46
+ # Format the response without T5 for faster response
47
+ events_text = ""
48
+ for event, score in matched_events:
49
+ events_text += f"""
50
+ πŸ“… {event['title']}
51
+ πŸ“† {event['start_time'].strftime('%A, %B %d, %Y')}
52
+ ⏰ {event['start_time'].strftime('%I:%M %p')}
53
+ πŸ“ {event['location']}
54
+ 🏷️ {event['categories']}
55
+ πŸ‘₯ {event['hosts']}
56
+
57
+ {event['description'][:200]}...
58
+
59
+ πŸ”— {event['link']}
60
+
61
+ """
62
+ # Create a simple response prefix based on the query type
63
+ if "today" in user_query.lower():
64
+ prefix = "Here are today's events:"
65
+ elif "week" in user_query.lower():
66
+ prefix = "Here are the events happening this week:"
67
+ elif any(word in user_query.lower() for word in ["workshop", "training", "seminar"]):
68
+ prefix = "I found these workshops and seminars:"
69
+ elif any(word in user_query.lower() for word in ["faculty", "department", "school"]):
70
+ prefix = "Here are the faculty-related events:"
71
+ else:
72
+ prefix = "Here are some events that match your query:"
73
 
74
+ return f"{prefix}\n{events_text}"
 
 
75
 
76
+ except Exception as e:
77
+ print(f"Error in query: {e}")
78
+ return "I encountered an error processing your query. Please try again!"
79
+ return "I encountered an error processing your query. Please try again!"
80
+
81
+ def find_matching_events(self, query: str) -> List[Tuple[Dict, float]]:
82
+ """Find events matching the query - optimized version"""
83
+ matched_events = []
84
+ query_lower = query.lower()
85
+
86
+ for event in self.events:
87
+ # Quick initial filter
88
+ if any(term in event['title'].lower() or
89
+ term in event['description'].lower()[:200] or
90
+ term in event['location'].lower() or
91
+ term in event['categories'].lower()
92
+ for term in query_lower.split()):
93
+
94
+ # Calculate similarity scores only for potentially matching events
95
+ title_score = fuzz.token_set_ratio(query_lower, event['title'].lower()) / 100
96
+ desc_score = fuzz.token_set_ratio(query_lower, event['description'].lower()) / 100
97
+ location_score = fuzz.token_set_ratio(query_lower, event['location'].lower()) / 100
98
+ categories_score = fuzz.token_set_ratio(query_lower, event['categories'].lower()) / 100
99
 
100
  # Weight the scores
101
  total_score = (
102
+ title_score * 0.4 +
103
+ desc_score * 0.3 +
104
+ location_score * 0.2 +
105
+ categories_score * 0.1
 
106
  )
107
 
108
  if total_score > 0.3: # Threshold for relevance
109
  matched_events.append((event, total_score))
110
+
111
+ # Sort by score and get top matches
112
+ matched_events.sort(key=lambda x: x[1], reverse=True)
113
+ return matched_events[:3]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
 
115
  def parse_event_datetime(self, entry) -> Tuple[Optional[datetime], Optional[datetime]]:
116
  """Parse event dates from RSS feed"""