Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multiple speaker info #455

Merged
merged 11 commits into from
Nov 21, 2023
1 change: 1 addition & 0 deletions backend/video/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
FEMALE = "FEMALE"

GENDER = ((MALE, "Male"), (FEMALE, "Female"))
MULTISPEAKER_AGE_GROUP = ("1-10", "11-20", "21-60", "61-100")

VIDEO_STATUS = (
("NEW", "NEW"),
Expand Down
13 changes: 13 additions & 0 deletions backend/video/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,3 +708,16 @@ def create_video(
new_request.GET["task_type"] = task_type
new_request.GET["target_language"] = target_language
return get_video_func(new_request)


def find_duplicates(data, key):
temp = []
duplicates = []

for dictionary in data:
if dictionary[key] not in temp:
temp.append(dictionary[key])
else:
duplicates.append(dictionary[key])

return duplicates
95 changes: 86 additions & 9 deletions backend/video/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from transcript.models import ORIGINAL_SOURCE, Transcript
from translation.models import Translation
from project.decorators import is_project_owner
from .models import Video, GENDER
from .models import Video, GENDER, MULTISPEAKER_AGE_GROUP
from .serializers import VideoSerializer
from .utils import *
from django.utils import timezone
Expand Down Expand Up @@ -257,7 +257,6 @@ def list_recent(request):
# In the future, if that constraint is removed then we might need to alter the logic.

try:

# Get the relevant videos, based on the audio only param
video_list = Video.objects.filter(audio_only=is_audio_only)

Expand Down Expand Up @@ -506,6 +505,14 @@ def download_all(request):
type=openapi.TYPE_STRING,
description="Gender of video's voice",
),
"multiple_speaker": openapi.Schema(
type=openapi.TYPE_BOOLEAN,
description="Multiple speaker true or false",
),
"speaker_info": openapi.Schema(
type=openapi.TYPE_OBJECT,
description="Speaker info of video",
),
},
required=["video_id"],
),
Expand All @@ -521,9 +528,13 @@ def update_video(request):
video_id = request.data.get("video_id")
description = request.data.get("description")
gender = request.data.get("gender")
multiple_speaker = request.data.get("multiple_speaker", "false")
speaker_info = request.data.get("speaker_info")

multiple_speaker = multiple_speaker.lower() == "true"
try:
video = Video.objects.get(id=video_id)
errors = []

if description is not None:
video.description = description
Expand All @@ -533,14 +544,80 @@ def update_video(request):
if gender.upper() in gender_list:
video.gender = gender.upper()

video.save()
if multiple_speaker is not None:
video.multiple_speaker = multiple_speaker

return Response(
{
"message": "Video updated successfully.",
},
status=status.HTTP_200_OK,
)
if speaker_info is not None:
# Get the task transcript status for the video, if none or selected source
task = (
Task.objects.filter(video_id=video_id)
.filter(task_type="TRANSCRIPTION_EDIT")
.filter(status__in=["SELECTED_SOURCE"])
)
if not task:
errors.append(
{
"message": f"Video's transcript status must be selected source or none",
}
)

speaker_info_for_update = []
gender_list = [gender[0] for gender in GENDER]

# Find dictionary matching value in list
dubplicte_ids = find_duplicates(speaker_info, "id")
if dubplicte_ids:
errors.append(
{
"message": f"Ids must be unique Age in : {i}",
}
)

for i in speaker_info:
speaker_info_obj = {}

if i["name"] is not None:
speaker_info_obj["name"] = i["name"]

if i["gender"].upper() in gender_list:
speaker_info_obj["gender"] = i["gender"].upper()
else:
errors.append(
{
"message": f"Invalid Gender in : {i}",
}
)

if i["age"] in MULTISPEAKER_AGE_GROUP:
speaker_info_obj["age"] = i["age"]
else:
errors.append(
{
"message": f"Invalid Age in : {i}",
}
)

if i["id"] is not None:
speaker_info_obj["id"] = i["id"]

speaker_info_for_update.append(speaker_info_obj)

video.speaker_info = speaker_info_for_update

if len(errors) > 0:
return Response(
{"message": "Invalid Data", "response": errors},
status=status.HTTP_400_BAD_REQUEST,
)
else:
video.save()

return Response(
{
"message": "Video updated successfully.",
},
status=status.HTTP_200_OK,
)
except Video.DoesNotExist:
return Response(
{"message": "Video not found"}, status=status.HTTP_404_NOT_FOUND
Expand Down