Skip to content

Commit

Permalink
[DONE] Clean code (#1100)
Browse files Browse the repository at this point in the history
* Simplify video_chapter_save & video_chapter_import function

* Remove unused variable

* Simplify video_completion_track_save & video_completion_track_delete function

* Rename captFile variable

* Rename enrichModelQueue variable

* Rename FFMPEG_DRESSING_INPUT params

* Update pydoc

* Set some variable to lowercase

* bump docker version to bullseye

---------

Co-authored-by: Ptitloup <nicolas.can@univ-lille.fr>
  • Loading branch information
SebastienCozeDev and ptitloup authored Apr 15, 2024
1 parent 7c9d22e commit 8b5e6a3
Show file tree
Hide file tree
Showing 14 changed files with 210 additions and 244 deletions.
2 changes: 1 addition & 1 deletion .env.dev-exemple
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ DJANGO_SUPERUSER_EMAIL=<MAIL>
### You can use internal registry
ELASTICSEARCH_TAG=elasticsearch:8.13.0
NODE_TAG=node:19
PYTHON_TAG=python:3.9-buster
PYTHON_TAG=python:3.9-bullseye
REDIS_TAG=redis:alpine3.16
### DOCKER_ENV : You can specify light or full.
### In case of value changing, you have to rebuild and restart your container.
Expand Down
88 changes: 33 additions & 55 deletions pod/chapter/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,37 @@ def video_chapter_new(request, video):
)


def toggle_valid_form__video_chapter(request, video):
"""Toggle the form validity."""
list_chapter = video.chapter_set.all()
if is_ajax(request):
csrf_token_value = get_token(request)
some_data_to_dump = {
"list_chapter": render_to_string(
"chapter/list_chapter.html",
{
"list_chapter": list_chapter,
"video": video,
"csrf_token_value": csrf_token_value,
},
request=request,
),
"video-elem": render_to_string(
"videos/video-element.html",
{"video": video},
request=request,
),
}
data = json.dumps(some_data_to_dump)
return HttpResponse(data, content_type="application/json")
else:
return render(
request,
"video_chapter.html",
{"video": video, "list_chapter": list_chapter},
)


def video_chapter_save(request, video):
"""Save a video chapter form request."""
list_chapter = video.chapter_set.all()
Expand All @@ -94,33 +125,7 @@ def video_chapter_save(request, video):
form_chapter = ChapterForm(request.POST)
if form_chapter.is_valid():
form_chapter.save()
list_chapter = video.chapter_set.all()
if is_ajax(request):
csrf_token_value = get_token(request)
some_data_to_dump = {
"list_chapter": render_to_string(
"chapter/list_chapter.html",
{
"list_chapter": list_chapter,
"video": video,
"csrf_token_value": csrf_token_value,
},
request=request,
),
"video-elem": render_to_string(
"videos/video-element.html",
{"video": video},
request=request,
),
}
data = json.dumps(some_data_to_dump)
return HttpResponse(data, content_type="application/json")
else:
return render(
request,
"video_chapter.html",
{"video": video, "list_chapter": list_chapter},
)
return toggle_valid_form__video_chapter(request, video)
else:
if is_ajax(request):
csrf_token_value = get_token(request)
Expand Down Expand Up @@ -175,7 +180,6 @@ def video_chapter_modify(request, video):


def video_chapter_delete(request, video):
list_chapter = video.chapter_set.all()
chapter = get_object_or_404(Chapter, id=request.POST.get("id"))
chapter.delete()
list_chapter = video.chapter_set.all()
Expand Down Expand Up @@ -220,33 +224,7 @@ def video_chapter_import(request, video):
form_chapter = ChapterForm(initial={"video": video})
form_import = ChapterImportForm(request.POST, user=request.user, video=video)
if form_import.is_valid():
list_chapter = video.chapter_set.all()
if is_ajax(request):
csrf_token_value = get_token(request)
some_data_to_dump = {
"list_chapter": render_to_string(
"chapter/list_chapter.html",
{
"list_chapter": list_chapter,
"video": video,
"csrf_token_value": csrf_token_value,
},
request=request,
),
"video-elem": render_to_string(
"videos/video-element.html",
{"video": video},
request=request,
),
}
data = json.dumps(some_data_to_dump)
return HttpResponse(data, content_type="application/json")
else:
return render(
request,
"video_chapter.html",
{"video": video, "list_chapter": list_chapter},
)
return toggle_valid_form__video_chapter(request, video)
else:
if is_ajax(request):
some_data_to_dump = {
Expand Down
36 changes: 18 additions & 18 deletions pod/completion/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ def debug(text):
def check_if_treatment_in_progress() -> bool:
return EnrichModelQueue.objects.filter(in_treatment=True).exists()

def write_into_kaldi_file(enrichModelQueue: EnrichModelQueue):
def write_into_kaldi_file(enrich_model_queue: EnrichModelQueue):
with open(
MODEL_COMPILE_DIR + "/" + enrichModelQueue.lang + "/db/extra.txt", "w"
MODEL_COMPILE_DIR + "/" + enrich_model_queue.lang + "/db/extra.txt", "w"
) as f:
f.write(enrichModelQueue.text)
f.write(enrich_model_queue.text)
subprocess.call(
[
"docker",
Expand All @@ -174,16 +174,16 @@ def write_into_kaldi_file(enrichModelQueue: EnrichModelQueue):
MODEL_COMPILE_DIR + ":/kaldi/compile-model",
"-it",
"kaldi",
enrichModelQueue.lang,
enrich_model_queue.lang,
]
)

def copy_result_into_current_model(enrichModelQueue: EnrichModelQueue):
def copy_result_into_current_model(enrich_model_queue: EnrichModelQueue):
from_path: str = (
MODEL_COMPILE_DIR + "/" + enrichModelQueue.lang + "/exp/chain/tdnn/graph"
MODEL_COMPILE_DIR + "/" + enrich_model_queue.lang + "/exp/chain/tdnn/graph"
)
to_path: str = (
TRANSCRIPTION_MODEL_PARAM[enrichModelQueue.model_type][enrichModelQueue.lang][
TRANSCRIPTION_MODEL_PARAM[enrich_model_queue.model_type][enrich_model_queue.lang][
"model"
]
+ "/graph"
Expand All @@ -193,10 +193,10 @@ def copy_result_into_current_model(enrichModelQueue: EnrichModelQueue):
shutil.copytree(from_path, to_path)

from_path: str = (
MODEL_COMPILE_DIR + "/" + enrichModelQueue.lang + "/data/lang_test_rescore"
MODEL_COMPILE_DIR + "/" + enrich_model_queue.lang + "/data/lang_test_rescore"
)
to_path: str = (
TRANSCRIPTION_MODEL_PARAM[enrichModelQueue.model_type][enrichModelQueue.lang][
TRANSCRIPTION_MODEL_PARAM[enrich_model_queue.model_type][enrich_model_queue.lang][
"model"
]
+ "/rescore/"
Expand All @@ -208,10 +208,10 @@ def copy_result_into_current_model(enrichModelQueue: EnrichModelQueue):
shutil.copy(from_path + "/G.carpa", to_path)

from_path: str = (
MODEL_COMPILE_DIR + "/" + enrichModelQueue.lang + "/exp/rnnlm_out"
MODEL_COMPILE_DIR + "/" + enrich_model_queue.lang + "/exp/rnnlm_out"
)
to_path: str = (
TRANSCRIPTION_MODEL_PARAM[enrichModelQueue.model_type][enrichModelQueue.lang][
TRANSCRIPTION_MODEL_PARAM[enrich_model_queue.model_type][enrich_model_queue.lang][
"model"
]
+ "/rnnlm/"
Expand All @@ -221,17 +221,17 @@ def copy_result_into_current_model(enrichModelQueue: EnrichModelQueue):

def enrich_kaldi_model_launch():
TrackAdmin.debug("enrich_kaldi_model")
enrichModelQueue = EnrichModelQueue.objects.filter(model_type="VOSK").first()
if enrichModelQueue is not None:
enrichModelQueue.in_treatment = True
enrichModelQueue.save()
enrich_model_queue = EnrichModelQueue.objects.filter(model_type="VOSK").first()
if enrich_model_queue is not None:
enrich_model_queue.in_treatment = True
enrich_model_queue.save()
TrackAdmin.debug("start subprocess")
TrackAdmin.write_into_kaldi_file(enrichModelQueue)
TrackAdmin.write_into_kaldi_file(enrich_model_queue)
TrackAdmin.debug("finish subprocess")
TrackAdmin.debug("start copy result")
TrackAdmin.copy_result_into_current_model(enrichModelQueue)
TrackAdmin.copy_result_into_current_model(enrich_model_queue)
TrackAdmin.debug("finish copy result")
enrichModelQueue.delete()
enrich_model_queue.delete()
TrackAdmin.enrich_kaldi_model_launch()
return
else:
Expand Down
77 changes: 32 additions & 45 deletions pod/completion/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ def video_caption_maker(request, slug):
else:
track_language = LANGUAGE_CODE
track_kind = "captions"
captionFileId = request.GET.get("src")
if captionFileId:
captionFile = CustomFileModel.objects.filter(id=captionFileId).first()
if captionFile:
track = Track.objects.filter(video=video, src=captionFile).first()
caption_file_id = request.GET.get("src")
if caption_file_id:
caption_file = CustomFileModel.objects.filter(id=caption_file_id).first()
if caption_file:
track = Track.objects.filter(video=video, src=caption_file).first()
if track:
track_language = track.lang
track_kind = track.kind
Expand Down Expand Up @@ -119,12 +119,12 @@ def video_caption_maker_save(request, video):
response = file_edit_save(request, cur_folder)
response_data = json.loads(response.content)
if ("list_element" in response_data) and (lang in __LANG_CHOICES_DICT__):
captFile = get_object_or_404(CustomFileModel, id=response_data["file_id"])
capt_file = get_object_or_404(CustomFileModel, id=response_data["file_id"])
# immediately assign the newly created captions file to the video
desired = Track.objects.filter(video=video, src=captFile)
desired = Track.objects.filter(video=video, src=capt_file)
if desired.exists():
desired.update(
lang=lang, kind=kind, src=captFile, enrich_ready=enrich_ready
lang=lang, kind=kind, src=capt_file, enrich_ready=enrich_ready
)
else:
# check if the same combination of lang and kind exists
Expand All @@ -133,7 +133,7 @@ def video_caption_maker_save(request, video):
video=video,
kind=kind,
lang=lang,
src=captFile,
src=capt_file,
enrich_ready=enrich_ready,
)
track.save()
Expand Down Expand Up @@ -587,31 +587,35 @@ def video_completion_get_form_track(request):
return form_track


def toggle_form_track_is_valid__video_completion_track(request, video, list_track):
"""Toggle form_track is valid."""
if request.is_ajax():
some_data_to_dump = {
"list_data": render_to_string(
"track/list_track.html",
{"list_track": list_track, "video": video},
request=request,
)
}
data = json.dumps(some_data_to_dump)
return HttpResponse(data, content_type="application/json")
else:
context = get_video_completion_context(video, list_track=list_track)
return render(
request,
"video_completion.html",
context,
)


def video_completion_track_save(request, video):
"""View to save a track associated to a video."""
form_track = video_completion_get_form_track(request)
list_track = video.track_set.all()

if form_track.is_valid():
form_track.save()

if request.is_ajax():
some_data_to_dump = {
"list_data": render_to_string(
"track/list_track.html",
{"list_track": list_track, "video": video},
request=request,
)
}
data = json.dumps(some_data_to_dump)
return HttpResponse(data, content_type="application/json")
else:
context = get_video_completion_context(video, list_track=list_track)
return render(
request,
"video_completion.html",
context,
)
return toggle_form_track_is_valid__video_completion_track(request, video, list_track)
else:
if request.is_ajax():
some_data_to_dump = {
Expand Down Expand Up @@ -659,24 +663,7 @@ def video_completion_track_delete(request, video):
track = get_object_or_404(Track, id=request.POST["id"])
track.delete()
list_track = video.track_set.all()

if request.is_ajax():
some_data_to_dump = {
"list_data": render_to_string(
"track/list_track.html",
{"list_track": list_track, "video": video},
request=request,
)
}
data = json.dumps(some_data_to_dump)
return HttpResponse(data, content_type="application/json")
else:
context = get_video_completion_context(video, list_track=list_track)
return render(
request,
"video_completion.html",
context,
)
return toggle_form_track_is_valid__video_completion_track(request, video, list_track)


def is_already_link(url, text):
Expand Down
10 changes: 5 additions & 5 deletions pod/dressing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@
from django.db.models import Q


def get_dressing_input(dressing: Dressing, FFMPEG_DRESSING_INPUT: str) -> str:
def get_dressing_input(dressing: Dressing, ffmpeg_dressing_input: str) -> str:
"""
Obtain the files necessary for encoding a dressed video.
Args:
dressing (:class:`pod.dressing.models.Dressing`): The dressing object.
FFMPEG_DRESSING_INPUT (str): Source file for encoding.
ffmpeg_dressing_input (str): Source file for encoding.
Returns:
command (str): params for the ffmpeg command.
"""
command = ""
if dressing.watermark:
command += FFMPEG_DRESSING_INPUT % {"input": dressing.watermark.file.path}
command += ffmpeg_dressing_input % {"input": dressing.watermark.file.path}
if dressing.opening_credits:
command += FFMPEG_DRESSING_INPUT % {
command += ffmpeg_dressing_input % {
"input": os.path.join(
settings.MEDIA_ROOT, str(dressing.opening_credits.video)
)
}
if dressing.ending_credits:
command += FFMPEG_DRESSING_INPUT % {
command += ffmpeg_dressing_input % {
"input": os.path.join(settings.MEDIA_ROOT, str(dressing.ending_credits.video))
}
return command
Expand Down
Loading

0 comments on commit 8b5e6a3

Please sign in to comment.