mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-21 12:49:06 +00:00
server: add an endpoint to merge speaker
This commit is contained in:
@@ -23,6 +23,11 @@ class SpeakerAssignmentStatus(BaseModel):
|
||||
status: str
|
||||
|
||||
|
||||
class SpeakerMerge(BaseModel):
|
||||
speaker_from: int
|
||||
speaker_to: int
|
||||
|
||||
|
||||
@router.patch("/transcripts/{transcript_id}/speaker/assign")
|
||||
async def transcript_assign_speaker(
|
||||
transcript_id: str,
|
||||
@@ -61,3 +66,66 @@ async def transcript_assign_speaker(
|
||||
)
|
||||
|
||||
return SpeakerAssignmentStatus(status="ok")
|
||||
|
||||
|
||||
@router.patch("/transcripts/{transcript_id}/speaker/merge")
|
||||
async def transcript_merge_speaker(
|
||||
transcript_id: str,
|
||||
merge: SpeakerMerge,
|
||||
user: Annotated[Optional[auth.UserInfo], Depends(auth.current_user_optional)],
|
||||
) -> SpeakerAssignmentStatus:
|
||||
user_id = user["sub"] if user else None
|
||||
transcript = await transcripts_controller.get_by_id_for_http(
|
||||
transcript_id, user_id=user_id
|
||||
)
|
||||
|
||||
if not transcript:
|
||||
raise HTTPException(status_code=404, detail="Transcript not found")
|
||||
|
||||
# ensure both speaker are not assigned to the 2 differents participants
|
||||
participant_from = next(
|
||||
(
|
||||
participant
|
||||
for participant in transcript.participants
|
||||
if participant.speaker == merge.speaker_from
|
||||
),
|
||||
None,
|
||||
)
|
||||
participant_to = next(
|
||||
(
|
||||
participant
|
||||
for participant in transcript.participants
|
||||
if participant.speaker == merge.speaker_to
|
||||
),
|
||||
None,
|
||||
)
|
||||
if participant_from and participant_to:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Both speakers are assigned to participants",
|
||||
)
|
||||
|
||||
# reassign speakers from words in the transcript
|
||||
speaker_from = merge.speaker_from
|
||||
speaker_to = merge.speaker_to
|
||||
changed_topics = []
|
||||
for topic in transcript.topics:
|
||||
changed = False
|
||||
for word in topic.words:
|
||||
if word.speaker == speaker_from:
|
||||
word.speaker = speaker_to
|
||||
changed = True
|
||||
if changed:
|
||||
changed_topics.append(topic)
|
||||
|
||||
# batch changes
|
||||
for topic in changed_topics:
|
||||
transcript.upsert_topic(topic)
|
||||
await transcripts_controller.update(
|
||||
transcript,
|
||||
{
|
||||
"topics": transcript.topics_dump(),
|
||||
},
|
||||
)
|
||||
|
||||
return SpeakerAssignmentStatus(status="ok")
|
||||
|
||||
Reference in New Issue
Block a user