Spaces:
Running
Running
File size: 7,440 Bytes
09801ca | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | """
ML Models — Experiments, trained models, training jobs, predictions.
"""
import uuid
from datetime import datetime
from typing import Optional
from sqlalchemy import String, Boolean, Text, Integer, Float, BigInteger, Index, ForeignKey
from sqlalchemy.dialects.postgresql import UUID, JSONB
from sqlalchemy.orm import Mapped, mapped_column
from app.models.base import Base, TimestampMixin, SoftDeleteMixin, UUIDPrimaryKeyMixin
class Project(UUIDPrimaryKeyMixin, TimestampMixin, SoftDeleteMixin, Base):
"""Top-level project container for organizing ML work."""
__tablename__ = "projects"
__table_args__ = (
Index("ix_projects_user_id", "user_id"),
Index("ix_projects_name", "name"),
)
user_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True),
ForeignKey("users.id", ondelete="CASCADE"),
nullable=False,
)
name: Mapped[str] = mapped_column(String(255), nullable=False)
description: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
status: Mapped[str] = mapped_column(String(20), default="active", nullable=False)
tags: Mapped[dict] = mapped_column(JSONB, default=list, nullable=False)
settings: Mapped[dict] = mapped_column(JSONB, default=dict, nullable=False)
def __repr__(self) -> str:
return f"<Project name={self.name}>"
class Dataset(UUIDPrimaryKeyMixin, TimestampMixin, SoftDeleteMixin, Base):
"""Dataset metadata — linked to a project."""
__tablename__ = "datasets"
__table_args__ = (
Index("ix_datasets_user_id", "user_id"),
Index("ix_datasets_project_id", "project_id"),
)
user_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True),
ForeignKey("users.id", ondelete="CASCADE"),
nullable=False,
)
project_id: Mapped[Optional[uuid.UUID]] = mapped_column(
UUID(as_uuid=True),
ForeignKey("projects.id", ondelete="SET NULL"),
nullable=True,
)
name: Mapped[str] = mapped_column(String(255), nullable=False)
description: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
source_type: Mapped[str] = mapped_column(String(50), nullable=False)
file_path: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
row_count: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
column_count: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
file_size_bytes: Mapped[Optional[int]] = mapped_column(BigInteger, nullable=True)
schema_info: Mapped[dict] = mapped_column(JSONB, default=dict, nullable=False)
statistics: Mapped[dict] = mapped_column(JSONB, default=dict, nullable=False)
def __repr__(self) -> str:
return f"<Dataset name={self.name}>"
class Experiment(UUIDPrimaryKeyMixin, TimestampMixin, Base):
"""ML experiment — a logical grouping of training runs."""
__tablename__ = "experiments"
__table_args__ = (
Index("ix_experiments_user_id", "user_id"),
Index("ix_experiments_project_id", "project_id"),
)
user_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True),
ForeignKey("users.id", ondelete="CASCADE"),
nullable=False,
)
project_id: Mapped[Optional[uuid.UUID]] = mapped_column(
UUID(as_uuid=True),
ForeignKey("projects.id", ondelete="SET NULL"),
nullable=True,
)
name: Mapped[str] = mapped_column(String(255), nullable=False)
description: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
task_type: Mapped[str] = mapped_column(String(50), nullable=False)
status: Mapped[str] = mapped_column(String(20), default="created", nullable=False)
config: Mapped[dict] = mapped_column(JSONB, default=dict, nullable=False)
best_metric_name: Mapped[Optional[str]] = mapped_column(String(50), nullable=True)
best_metric_value: Mapped[Optional[float]] = mapped_column(Float, nullable=True)
def __repr__(self) -> str:
return f"<Experiment name={self.name} status={self.status}>"
class MLModel(UUIDPrimaryKeyMixin, TimestampMixin, SoftDeleteMixin, Base):
"""Registered ML model in the model registry."""
__tablename__ = "ml_models"
__table_args__ = (
Index("ix_ml_models_user_id", "user_id"),
Index("ix_ml_models_name", "name"),
)
user_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True),
ForeignKey("users.id", ondelete="CASCADE"),
nullable=False,
)
experiment_id: Mapped[Optional[uuid.UUID]] = mapped_column(
UUID(as_uuid=True),
ForeignKey("experiments.id", ondelete="SET NULL"),
nullable=True,
)
name: Mapped[str] = mapped_column(String(255), nullable=False)
description: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
framework: Mapped[str] = mapped_column(String(50), nullable=False)
algorithm: Mapped[str] = mapped_column(String(100), nullable=False)
version: Mapped[str] = mapped_column(String(50), default="1.0.0", nullable=False)
stage: Mapped[str] = mapped_column(
String(20), default="development", nullable=False
)
metrics: Mapped[dict] = mapped_column(JSONB, default=dict, nullable=False)
hyperparameters: Mapped[dict] = mapped_column(JSONB, default=dict, nullable=False)
artifact_path: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
input_schema: Mapped[dict] = mapped_column(JSONB, default=dict, nullable=False)
output_schema: Mapped[dict] = mapped_column(JSONB, default=dict, nullable=False)
file_size_bytes: Mapped[Optional[int]] = mapped_column(BigInteger, nullable=True)
def __repr__(self) -> str:
return f"<MLModel name={self.name} stage={self.stage}>"
class TrainingJob(UUIDPrimaryKeyMixin, TimestampMixin, Base):
"""Individual model training run."""
__tablename__ = "training_jobs"
__table_args__ = (
Index("ix_training_jobs_user_id", "user_id"),
Index("ix_training_jobs_status", "status"),
)
user_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True),
ForeignKey("users.id", ondelete="CASCADE"),
nullable=False,
)
experiment_id: Mapped[Optional[uuid.UUID]] = mapped_column(
UUID(as_uuid=True),
ForeignKey("experiments.id", ondelete="SET NULL"),
nullable=True,
)
model_id: Mapped[Optional[uuid.UUID]] = mapped_column(
UUID(as_uuid=True),
ForeignKey("ml_models.id", ondelete="SET NULL"),
nullable=True,
)
dataset_id: Mapped[Optional[uuid.UUID]] = mapped_column(
UUID(as_uuid=True),
ForeignKey("datasets.id", ondelete="SET NULL"),
nullable=True,
)
status: Mapped[str] = mapped_column(
String(20), default="pending", nullable=False
)
config: Mapped[dict] = mapped_column(JSONB, default=dict, nullable=False)
metrics: Mapped[dict] = mapped_column(JSONB, default=dict, nullable=False)
logs: Mapped[dict] = mapped_column(JSONB, default=list, nullable=False)
error_message: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
started_at: Mapped[Optional[datetime]] = mapped_column(nullable=True)
completed_at: Mapped[Optional[datetime]] = mapped_column(nullable=True)
duration_seconds: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
def __repr__(self) -> str:
return f"<TrainingJob id={self.id} status={self.status}>"
|