Spaces:
Runtime error
Runtime error
File size: 2,064 Bytes
3dcada4 | 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 | from sqlalchemy import Column, Integer, String, TIMESTAMP, ForeignKey
from sqlalchemy.sql import func
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
from .database import Base
from sqlalchemy.orm import relationship
class Transcription(Base):
__tablename__ = "transcriptions"
id = Column(Integer, primary_key=True, index=True)
session_id = Column(String, ForeignKey("sessions.session_id",onupdate='CASCADE',ondelete='CASCADE'))
chunk_number = Column(Integer)
text = Column(String)
language = Column(String)
created_at = Column(TIMESTAMP(timezone=True), server_default=func.now())
session = relationship("Session", back_populates="transcriptions")
# CRUD operations
async def create_transcription(db: AsyncSession, session_id: str, chunk_number: int, text: str, language: str):
new_transcription = Transcription(
session_id=session_id,
chunk_number=chunk_number,
text=text,
language=language
)
db.add(new_transcription)
await db.commit()
await db.refresh(new_transcription)
return new_transcription
async def get_transcriptions_by_session(db: AsyncSession, session_id: str):
result = await db.execute(select(Transcription).where(Transcription.session_id == session_id))
return result.scalars().all()
async def update_transcription_text(db: AsyncSession, transcription_id: int, new_text: str):
result = await db.execute(select(Transcription).where(Transcription.id == transcription_id))
transcription = result.scalar_one_or_none()
if transcription:
transcription.text = new_text
await db.commit()
await db.refresh(transcription)
return transcription
async def delete_transcription(db: AsyncSession, transcription_id: int):
result = await db.execute(select(Transcription).where(Transcription.id == transcription_id))
transcription = result.scalar_one_or_none()
if transcription:
await db.delete(transcription)
await db.commit()
return transcription
|