feat: add MLflow experiment tracking to all 4 Gradio UIs
Each UI now logs per-request metrics to MLflow: - llm.py: latency, tokens/sec, prompt/completion tokens (gradio-llm-tuning) - embeddings.py: latency, text length, batch size (gradio-embeddings-tuning) - stt.py: latency, audio duration, real-time factor (gradio-stt-tuning) - tts.py: latency, text length, audio duration (gradio-tts-tuning) Uses try/except guarded imports so UIs still work if MLflow is unreachable. Persistent run per Gradio instance, batched metric logging via MlflowClient.log_batch().
This commit is contained in:
@@ -30,10 +30,64 @@ EMBEDDINGS_URL = os.environ.get(
|
||||
# Default: Ray Serve Embeddings endpoint
|
||||
"http://ai-inference-serve-svc.ai-ml.svc.cluster.local:8000/embeddings"
|
||||
)
|
||||
MLFLOW_TRACKING_URI = os.environ.get(
|
||||
"MLFLOW_TRACKING_URI",
|
||||
"http://mlflow.mlflow.svc.cluster.local:80"
|
||||
)
|
||||
# ─── MLflow experiment tracking ──────────────────────────────────────────
|
||||
try:
|
||||
import mlflow
|
||||
from mlflow.tracking import MlflowClient
|
||||
|
||||
MLFLOW_TRACKING_URI = os.environ.get(
|
||||
"MLFLOW_TRACKING_URI",
|
||||
"http://mlflow.mlflow.svc.cluster.local:80",
|
||||
)
|
||||
mlflow.set_tracking_uri(MLFLOW_TRACKING_URI)
|
||||
_mlflow_client = MlflowClient()
|
||||
|
||||
_experiment = _mlflow_client.get_experiment_by_name("gradio-embeddings-tuning")
|
||||
if _experiment is None:
|
||||
_experiment_id = _mlflow_client.create_experiment(
|
||||
"gradio-embeddings-tuning",
|
||||
artifact_location="/mlflow/artifacts/gradio-embeddings-tuning",
|
||||
)
|
||||
else:
|
||||
_experiment_id = _experiment.experiment_id
|
||||
|
||||
_mlflow_run = mlflow.start_run(
|
||||
experiment_id=_experiment_id,
|
||||
run_name=f"gradio-embeddings-{os.environ.get('HOSTNAME', 'local')}",
|
||||
tags={"service": "gradio-embeddings", "endpoint": EMBEDDINGS_URL},
|
||||
)
|
||||
_mlflow_run_id = _mlflow_run.info.run_id
|
||||
_mlflow_step = 0
|
||||
MLFLOW_ENABLED = True
|
||||
logger.info("MLflow tracking enabled: experiment=%s run=%s", _experiment_id, _mlflow_run_id)
|
||||
except Exception as exc:
|
||||
logger.warning("MLflow tracking disabled: %s", exc)
|
||||
_mlflow_client = None
|
||||
_mlflow_run_id = None
|
||||
_mlflow_step = 0
|
||||
MLFLOW_ENABLED = False
|
||||
|
||||
|
||||
def _log_embedding_metrics(latency: float, batch_size: int, embedding_dims: int = 0) -> None:
|
||||
"""Log embedding inference metrics to MLflow (non-blocking best-effort)."""
|
||||
global _mlflow_step
|
||||
if not MLFLOW_ENABLED or _mlflow_client is None:
|
||||
return
|
||||
try:
|
||||
_mlflow_step += 1
|
||||
ts = int(time.time() * 1000)
|
||||
_mlflow_client.log_batch(
|
||||
_mlflow_run_id,
|
||||
metrics=[
|
||||
mlflow.entities.Metric("latency_s", latency, ts, _mlflow_step),
|
||||
mlflow.entities.Metric("batch_size", batch_size, ts, _mlflow_step),
|
||||
mlflow.entities.Metric("embedding_dims", embedding_dims, ts, _mlflow_step),
|
||||
mlflow.entities.Metric("latency_per_text_ms", (latency * 1000 / batch_size) if batch_size > 0 else 0, ts, _mlflow_step),
|
||||
],
|
||||
)
|
||||
except Exception:
|
||||
logger.debug("MLflow log failed", exc_info=True)
|
||||
|
||||
|
||||
# HTTP client
|
||||
client = httpx.Client(timeout=60.0)
|
||||
@@ -76,6 +130,9 @@ def generate_single_embedding(text: str) -> tuple[str, str, str]:
|
||||
|
||||
embedding = embeddings[0]
|
||||
dims = len(embedding)
|
||||
|
||||
# Log to MLflow
|
||||
_log_embedding_metrics(latency, batch_size=1, embedding_dims=dims)
|
||||
|
||||
# Format output
|
||||
status = f"✅ Generated {dims}-dimensional embedding in {latency*1000:.1f}ms"
|
||||
@@ -118,6 +175,9 @@ def compare_texts(text1: str, text2: str) -> tuple[str, str]:
|
||||
return "❌ Failed to get embeddings for both texts", ""
|
||||
|
||||
similarity = cosine_similarity(embeddings[0], embeddings[1])
|
||||
|
||||
# Log to MLflow
|
||||
_log_embedding_metrics(latency, batch_size=2, embedding_dims=len(embeddings[0]))
|
||||
|
||||
# Determine similarity level
|
||||
if similarity > 0.9:
|
||||
@@ -167,6 +227,9 @@ def batch_embed(texts_input: str) -> tuple[str, str]:
|
||||
try:
|
||||
embeddings, latency = get_embeddings(texts)
|
||||
|
||||
# Log to MLflow
|
||||
_log_embedding_metrics(latency, batch_size=len(embeddings), embedding_dims=len(embeddings[0]) if embeddings else 0)
|
||||
|
||||
status = f"✅ Generated {len(embeddings)} embeddings in {latency*1000:.1f}ms"
|
||||
status += f" ({latency*1000/len(texts):.1f}ms per text)"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user