Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Make embedding model configurable via EMBEDDING_MODEL env var, warn on dimensionality changes

Murtaza Nasire41703b3b620
diff --git a/src/init_db.py b/src/init_db.py
index d8b6508d..4a237baa 100644
--- a/src/init_db.py
+++ b/src/init_db.py
@@ -651,6 +651,36 @@ def initialize_database(app):
)
app.logger.info("Initialized enable_folders setting")
+ # Track the embedding model name in system_setting so we can warn when
+ # it changes between restarts. Issue #262 — old vectors won't match a
+ # new model's output dimensionality and inquire mode will silently
+ # return zero results until users reprocess.
+ try:
+ current_model = os.environ.get('EMBEDDING_MODEL', 'all-MiniLM-L6-v2').strip() or 'all-MiniLM-L6-v2'
+ stored_model = SystemSetting.get_setting('embedding_model_name', None)
+ chunk_count = TranscriptChunk.query.filter(TranscriptChunk.embedding.isnot(None)).count()
+ if stored_model is None:
+ SystemSetting.set_setting(
+ key='embedding_model_name',
+ value=current_model,
+ description='Sentence-transformers model that produced the stored chunk embeddings. Used to detect dimensionality mismatches at startup.',
+ setting_type='string',
+ )
+ if chunk_count:
+ app.logger.info(f"Recorded embedding_model_name={current_model} (existing {chunk_count} chunks assumed to match)")
+ elif stored_model != current_model:
+ if chunk_count:
+ app.logger.warning(
+ f"EMBEDDING_MODEL changed from {stored_model!r} to {current_model!r} but {chunk_count} "
+ "chunks were embedded with the previous model. Inquire mode will return wrong results "
+ "until you reprocess affected recordings."
+ )
+ SystemSetting.set_setting(key='embedding_model_name', value=current_model)
+ app.logger.info(f"Updated embedding_model_name in system_setting: {stored_model!r} -> {current_model!r}")
+ except Exception as e:
+ db.session.rollback()
+ app.logger.warning(f"embedding_model_name compatibility check skipped: {e}")
+
# One-shot migration: clean up legacy User.transcription_language values
# that were stored as display names ("Français", "English") before the
# account-settings input was a dropdown. Issue #256.
diff --git a/src/services/embeddings.py b/src/services/embeddings.py
index fb42a374..369aa4cc 100644
--- a/src/services/embeddings.py
+++ b/src/services/embeddings.py
@@ -20,6 +20,13 @@ from src.models import Recording, TranscriptChunk, InternalShare, RecordingTag
ENABLE_INTERNAL_SHARING = os.environ.get('ENABLE_INTERNAL_SHARING', 'false').lower() == 'true'
+# Sentence-transformers model used to generate semantic-search vectors. The
+# default — all-MiniLM-L6-v2 — produces 384-dim vectors and is what older
+# Speakr instances embedded their chunks with. Changing this env var on an
+# existing instance will cause a dimensionality mismatch with stored vectors;
+# see startup warning in src/init_db.py and the docs.
+EMBEDDING_MODEL = os.environ.get('EMBEDDING_MODEL', 'all-MiniLM-L6-v2').strip() or 'all-MiniLM-L6-v2'
+
# Initialize embedding model (lazy loading)
_embedding_model = None
@@ -28,16 +35,16 @@ _embedding_model = None
def get_embedding_model():
"""Get or initialize the sentence transformer model."""
global _embedding_model
-
+
if not EMBEDDINGS_AVAILABLE:
return None
-
+
if _embedding_model is None:
try:
- _embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
- current_app.logger.info("Embedding model loaded successfully")
+ _embedding_model = SentenceTransformer(EMBEDDING_MODEL)
+ current_app.logger.info(f"Embedding model loaded successfully: {EMBEDDING_MODEL}")
except Exception as e:
- current_app.logger.error(f"Failed to load embedding model: {e}")
+ current_app.logger.error(f"Failed to load embedding model {EMBEDDING_MODEL!r}: {e}")
return None
return _embedding_model