Spaces:
Sleeping
Sleeping
Update course_recommender.py
Browse files- course_recommender.py +24 -8
course_recommender.py
CHANGED
|
@@ -21,6 +21,7 @@ class CourseRecommender:
|
|
| 21 |
self._last_data_count = 0 # Track data count for auto-retraining
|
| 22 |
self._auto_retrain_threshold = 5 # Retrain every 5 new feedbacks
|
| 23 |
self._min_samples_for_training = 10 # Minimum samples needed to train
|
|
|
|
| 24 |
|
| 25 |
def preprocess_data(self, df: pd.DataFrame) -> pd.DataFrame:
|
| 26 |
"""Preprocess the data for training"""
|
|
@@ -123,17 +124,18 @@ class CourseRecommender:
|
|
| 123 |
|
| 124 |
def check_and_auto_retrain(self):
|
| 125 |
"""Check if enough new data exists and auto-retrain if needed"""
|
| 126 |
-
|
|
|
|
| 127 |
|
| 128 |
-
if
|
| 129 |
-
print(f"Not enough
|
| 130 |
return False
|
| 131 |
|
| 132 |
-
if
|
| 133 |
-
print(f"Auto-retraining triggered: {
|
| 134 |
try:
|
| 135 |
accuracy = self.train_model(use_database=True)
|
| 136 |
-
self._last_data_count =
|
| 137 |
print(f"Auto-retraining completed with accuracy: {accuracy:.3f}")
|
| 138 |
return True
|
| 139 |
except Exception as e:
|
|
@@ -143,13 +145,27 @@ class CourseRecommender:
|
|
| 143 |
return False
|
| 144 |
|
| 145 |
def add_feedback_with_learning(self, course: str, stanine: int, gwa: float, strand: str,
|
| 146 |
-
rating:
|
| 147 |
"""Add feedback to database and trigger auto-learning if needed"""
|
| 148 |
# Add feedback to database
|
| 149 |
success = self.db_connection.add_feedback(course, stanine, gwa, strand, rating, hobbies)
|
| 150 |
|
| 151 |
if success:
|
| 152 |
print(f"Feedback added for course: {course}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
# Check if we should auto-retrain
|
| 154 |
self.check_and_auto_retrain()
|
| 155 |
|
|
@@ -327,4 +343,4 @@ class CourseRecommender:
|
|
| 327 |
def add_feedback(self, course: str, stanine: int, gwa: float, strand: str,
|
| 328 |
rating: int, hobbies: str) -> bool:
|
| 329 |
"""Add user feedback to the database"""
|
| 330 |
-
return self.db_connection.add_feedback(course, stanine, gwa, strand, rating, hobbies)
|
|
|
|
| 21 |
self._last_data_count = 0 # Track data count for auto-retraining
|
| 22 |
self._auto_retrain_threshold = 5 # Retrain every 5 new feedbacks
|
| 23 |
self._min_samples_for_training = 10 # Minimum samples needed to train
|
| 24 |
+
self._local_feedback = [] # Store feedback locally for learning
|
| 25 |
|
| 26 |
def preprocess_data(self, df: pd.DataFrame) -> pd.DataFrame:
|
| 27 |
"""Preprocess the data for training"""
|
|
|
|
| 124 |
|
| 125 |
def check_and_auto_retrain(self):
|
| 126 |
"""Check if enough new data exists and auto-retrain if needed"""
|
| 127 |
+
# Use local feedback count for auto-retraining
|
| 128 |
+
local_feedback_count = len(self._local_feedback)
|
| 129 |
|
| 130 |
+
if local_feedback_count < self._min_samples_for_training:
|
| 131 |
+
print(f"Not enough local feedback for training: {local_feedback_count} < {self._min_samples_for_training}")
|
| 132 |
return False
|
| 133 |
|
| 134 |
+
if local_feedback_count - self._last_data_count >= self._auto_retrain_threshold:
|
| 135 |
+
print(f"Auto-retraining triggered: {local_feedback_count - self._last_data_count} new local feedbacks")
|
| 136 |
try:
|
| 137 |
accuracy = self.train_model(use_database=True)
|
| 138 |
+
self._last_data_count = local_feedback_count
|
| 139 |
print(f"Auto-retraining completed with accuracy: {accuracy:.3f}")
|
| 140 |
return True
|
| 141 |
except Exception as e:
|
|
|
|
| 145 |
return False
|
| 146 |
|
| 147 |
def add_feedback_with_learning(self, course: str, stanine: int, gwa: float, strand: str,
|
| 148 |
+
rating: str, hobbies: str) -> bool:
|
| 149 |
"""Add feedback to database and trigger auto-learning if needed"""
|
| 150 |
# Add feedback to database
|
| 151 |
success = self.db_connection.add_feedback(course, stanine, gwa, strand, rating, hobbies)
|
| 152 |
|
| 153 |
if success:
|
| 154 |
print(f"Feedback added for course: {course}")
|
| 155 |
+
|
| 156 |
+
# Store feedback locally for learning (since API has issues)
|
| 157 |
+
feedback_record = {
|
| 158 |
+
'course': course,
|
| 159 |
+
'stanine': stanine,
|
| 160 |
+
'gwa': gwa,
|
| 161 |
+
'strand': strand,
|
| 162 |
+
'rating': rating,
|
| 163 |
+
'hobbies': hobbies,
|
| 164 |
+
'count': 1
|
| 165 |
+
}
|
| 166 |
+
self._local_feedback.append(feedback_record)
|
| 167 |
+
print(f"Feedback stored locally for learning: {len(self._local_feedback)} total")
|
| 168 |
+
|
| 169 |
# Check if we should auto-retrain
|
| 170 |
self.check_and_auto_retrain()
|
| 171 |
|
|
|
|
| 343 |
def add_feedback(self, course: str, stanine: int, gwa: float, strand: str,
|
| 344 |
rating: int, hobbies: str) -> bool:
|
| 345 |
"""Add user feedback to the database"""
|
| 346 |
+
return self.db_connection.add_feedback(course, stanine, gwa, strand, rating, hobbies)
|