Spaces:
Runtime error
Runtime error
feat: implement sync worker service for automated FAISS index rebuilding and project preprocessing
Browse files
src/similarity_model/__pycache__/embedding_engine.cpython-313.pyc
CHANGED
|
Binary files a/src/similarity_model/__pycache__/embedding_engine.cpython-313.pyc and b/src/similarity_model/__pycache__/embedding_engine.cpython-313.pyc differ
|
|
|
src/similarity_model/embedding_engine.py
CHANGED
|
@@ -96,7 +96,7 @@ def train_embedding_engine():
|
|
| 96 |
|
| 97 |
engine = ProjectEmbedder()
|
| 98 |
engine.build_index(df)
|
| 99 |
-
engine.save_artifacts()
|
| 100 |
|
| 101 |
logger.info("Embedding engine completed successfully.")
|
| 102 |
return engine
|
|
|
|
| 96 |
|
| 97 |
engine = ProjectEmbedder()
|
| 98 |
engine.build_index(df)
|
| 99 |
+
engine.save_artifacts(str(MODEL_DIR))
|
| 100 |
|
| 101 |
logger.info("Embedding engine completed successfully.")
|
| 102 |
return engine
|
src/similarity_model/sync_worker.py
CHANGED
|
@@ -40,18 +40,18 @@ class RebuildManager:
|
|
| 40 |
self.accumulated_changes += 1
|
| 41 |
self.pending_rebuild = True
|
| 42 |
|
| 43 |
-
def check_and_rebuild(self):
|
| 44 |
if not self.pending_rebuild:
|
| 45 |
return False
|
| 46 |
|
| 47 |
now = time.time()
|
| 48 |
time_elapsed = now - self.last_rebuild_time
|
| 49 |
|
| 50 |
-
# Trigger rebuild if we hit the change threshold, OR if the cooldown has passed
|
| 51 |
-
if self.accumulated_changes >= self.rebuild_threshold or time_elapsed >= self.rebuild_cooldown:
|
| 52 |
logger.info(
|
| 53 |
f"Triggering FAISS index rebuild. "
|
| 54 |
-
f"Accumulated changes: {self.accumulated_changes}, time elapsed: {time_elapsed:.1f}s"
|
| 55 |
)
|
| 56 |
try:
|
| 57 |
train_embedding_engine()
|
|
@@ -199,8 +199,8 @@ def run_worker():
|
|
| 199 |
batch = result.mappings().all()
|
| 200 |
|
| 201 |
if not batch:
|
| 202 |
-
# Idle period:
|
| 203 |
-
rebuild_manager.check_and_rebuild()
|
| 204 |
time.sleep(POLL_INTERVAL)
|
| 205 |
continue
|
| 206 |
|
|
@@ -210,8 +210,18 @@ def run_worker():
|
|
| 210 |
if changed:
|
| 211 |
rebuild_manager.record_change()
|
| 212 |
|
| 213 |
-
#
|
| 214 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
except Exception as e:
|
| 217 |
logger.error(f"Error in Sync Worker loop: {e}", exc_info=True)
|
|
|
|
| 40 |
self.accumulated_changes += 1
|
| 41 |
self.pending_rebuild = True
|
| 42 |
|
| 43 |
+
def check_and_rebuild(self, force=False):
|
| 44 |
if not self.pending_rebuild:
|
| 45 |
return False
|
| 46 |
|
| 47 |
now = time.time()
|
| 48 |
time_elapsed = now - self.last_rebuild_time
|
| 49 |
|
| 50 |
+
# Trigger rebuild if we hit the change threshold, OR if the cooldown has passed, OR if forced
|
| 51 |
+
if force or self.accumulated_changes >= self.rebuild_threshold or time_elapsed >= self.rebuild_cooldown:
|
| 52 |
logger.info(
|
| 53 |
f"Triggering FAISS index rebuild. "
|
| 54 |
+
f"Accumulated changes: {self.accumulated_changes}, time elapsed: {time_elapsed:.1f}s, force: {force}"
|
| 55 |
)
|
| 56 |
try:
|
| 57 |
train_embedding_engine()
|
|
|
|
| 199 |
batch = result.mappings().all()
|
| 200 |
|
| 201 |
if not batch:
|
| 202 |
+
# Idle period: force rebuild of any pending changes since queue is empty
|
| 203 |
+
rebuild_manager.check_and_rebuild(force=True)
|
| 204 |
time.sleep(POLL_INTERVAL)
|
| 205 |
continue
|
| 206 |
|
|
|
|
| 210 |
if changed:
|
| 211 |
rebuild_manager.record_change()
|
| 212 |
|
| 213 |
+
# Check if there are any remaining unprocessed items in the queue
|
| 214 |
+
with engine.connect() as conn:
|
| 215 |
+
any_left = conn.execute(text("""
|
| 216 |
+
SELECT TOP 1 QueueId FROM SyncQueue
|
| 217 |
+
WHERE Processed = 0 AND RetryCount < :max_retries
|
| 218 |
+
"""), {"max_retries": MAX_RETRIES}).fetchone()
|
| 219 |
+
|
| 220 |
+
# If no more items are left in the queue, we can rebuild immediately!
|
| 221 |
+
if not any_left:
|
| 222 |
+
rebuild_manager.check_and_rebuild(force=True)
|
| 223 |
+
else:
|
| 224 |
+
rebuild_manager.check_and_rebuild()
|
| 225 |
|
| 226 |
except Exception as e:
|
| 227 |
logger.error(f"Error in Sync Worker loop: {e}", exc_info=True)
|