Spaces:
Sleeping
Sleeping
from sqlalchemy import ( | |
Column, | |
Integer, | |
String, | |
Float, | |
Boolean, | |
DateTime, | |
ForeignKey, | |
Text, | |
) | |
from sqlalchemy.orm import relationship, declarative_base | |
Base = declarative_base() | |
# --------------------------------------------------------------------------- # | |
# TTSData # | |
# --------------------------------------------------------------------------- # | |
class TTSData(Base): | |
__tablename__ = "tts_data" | |
id = Column(Integer, primary_key=True) | |
filename = Column(String(255), nullable=False, unique=True) | |
sentence = Column(Text, nullable=False) | |
annotations = relationship( | |
"Annotation", | |
back_populates="tts_data", | |
cascade="all, delete-orphan", | |
) | |
# --------------------------------------------------------------------------- # | |
# Annotator # | |
# --------------------------------------------------------------------------- # | |
class Annotator(Base): | |
__tablename__ = "annotators" | |
id = Column(Integer, primary_key=True) | |
name = Column(String(100), nullable=False, unique=True) | |
password = Column(String(255), nullable=False) | |
last_login = Column(DateTime) | |
is_active = Column(Boolean, default=True) | |
annotations = relationship( | |
"Annotation", | |
back_populates="annotator", | |
cascade="all, delete-orphan", | |
) | |
validation_records = relationship( | |
"Validator", | |
back_populates="annotated_annotator", | |
foreign_keys="Validator.annotator_id", | |
cascade="all, delete-orphan", | |
) | |
validations_done = relationship( | |
"Validator", | |
back_populates="validator_user", | |
foreign_keys="Validator.validator_id", | |
cascade="all, delete-orphan", | |
) | |
annotation_intervals = relationship( | |
"AnnotationInterval", | |
back_populates="annotator", | |
cascade="all, delete-orphan", | |
) | |
# --------------------------------------------------------------------------- # | |
# Validator # | |
# --------------------------------------------------------------------------- # | |
class Validator(Base): | |
__tablename__ = "validators" | |
id = Column(Integer, primary_key=True) | |
annotator_id = Column(Integer, ForeignKey("annotators.id"), nullable=False) | |
validator_id = Column(Integer, ForeignKey("annotators.id"), nullable=False) | |
annotated_annotator = relationship( | |
"Annotator", | |
foreign_keys=[annotator_id], | |
back_populates="validation_records", | |
) | |
validator_user = relationship( | |
"Annotator", | |
foreign_keys=[validator_id], | |
back_populates="validations_done", | |
) | |
# --------------------------------------------------------------------------- # | |
# AnnotationInterval # | |
# --------------------------------------------------------------------------- # | |
class AnnotationInterval(Base): | |
__tablename__ = "annotation_intervals" | |
id = Column(Integer, primary_key=True) | |
annotator_id = Column(Integer, ForeignKey("annotators.id")) | |
start_index = Column(Integer, nullable=True) | |
end_index = Column(Integer, nullable=True) | |
annotator = relationship("Annotator", back_populates="annotation_intervals") | |
# --------------------------------------------------------------------------- # | |
# Annotation # | |
# --------------------------------------------------------------------------- # | |
class Annotation(Base): | |
__tablename__ = "annotations" | |
id = Column(Integer, primary_key=True) | |
tts_data_id = Column(Integer, ForeignKey("tts_data.id"), nullable=False) | |
annotator_id = Column(Integer, ForeignKey("annotators.id"), nullable=False) | |
annotated_sentence = Column(Text) | |
validated = Column(Boolean, default=False) | |
annotated_at = Column(DateTime) | |
tts_data = relationship("TTSData", back_populates="annotations") | |
annotator = relationship("Annotator", back_populates="annotations") | |
# Relationship to AudioTrim (one-to-MANY) | |
audio_trims = relationship( # Renamed from audio_trim | |
"AudioTrim", | |
back_populates="annotation", | |
uselist=True, # Important for one-to-many | |
cascade="all, delete-orphan" # If annotation is deleted, delete its trims too | |
) | |
# --------------------------------------------------------------------------- # | |
# AudioTrim # | |
# --------------------------------------------------------------------------- # | |
class AudioTrim(Base): | |
__tablename__ = "audio_trims" | |
id = Column(Integer, primary_key=True) | |
annotation_id = Column(Integer, ForeignKey("annotations.id"), nullable=False) # Removed unique=True | |
original_tts_data_id = Column(Integer, ForeignKey("tts_data.id"), nullable=False) # Link to original audio | |
start = Column(Float, nullable=False) | |
end = Column(Float, nullable=False) | |
# Relationship back to Annotation | |
annotation = relationship("Annotation", back_populates="audio_trims") # Renamed from audio_trim | |
original_tts_data = relationship("TTSData") # Optional: if you want to navigate from trim to original TTSData directly | |
# --------------------------------------------------------------------------- # | |
# Validation # | |
# --------------------------------------------------------------------------- # | |
class Validation(Base): | |
__tablename__ = "validations" | |
id = Column(Integer, primary_key=True) | |
annotation_id = Column(Integer, ForeignKey("annotations.id")) | |
validator_id = Column(Integer, ForeignKey("annotators.id")) # Fixed: should reference annotators.id | |
validated = Column(Boolean, nullable=False) | |
description = Column(Text, nullable=True) | |
validated_at = Column(DateTime, nullable=False) | |
annotation = relationship("Annotation") | |
validator = relationship("Annotator", foreign_keys=[validator_id]) # Fixed: should reference Annotator |