From dfa13da3a8d6fe231c0acddc9a2854b3e5294ee6 Mon Sep 17 00:00:00 2001 From: Shruti1229 Date: Fri, 28 Jul 2023 05:48:41 +0530 Subject: [PATCH] Modify name of temp waves --- backend/task/tasks.py | 11 +++++++++-- backend/voiceover/utils.py | 32 +++++++++++++++----------------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/backend/task/tasks.py b/backend/task/tasks.py index 05496b2a..786957dd 100644 --- a/backend/task/tasks.py +++ b/backend/task/tasks.py @@ -79,9 +79,16 @@ def celery_tts_call( task_id, tts_input, target_language, translation, translation_id, empty_sentences ): logging.info("Calling TTS API for %s", str(task_id)) - translation_obj = Translation.objects.get(id=translation_id) task_obj = Task.objects.get(pk=task_id) - logging.info("Generate TTS output") + translation_obj = ( + Translation.objects.filter(target_language=target_language) + .filter(video=task_obj.video) + .filter(status__in=["TRANSLATION_EDIT_COMPLETE", "TRANSLATION_REVIEW_COMPLETE"]) + .first() + ) + logging.info("Generate TTS output ID %s", str(translation_obj.task.id)) + logging.info("Translation ID %s", str(translation_id)) + logging.info("Empty sentences %s", str(empty_sentences)) tts_payload = generate_tts_output( tts_input, target_language, translation, translation_obj, empty_sentences ) diff --git a/backend/voiceover/utils.py b/backend/voiceover/utils.py index 63e15ff5..345f3563 100644 --- a/backend/voiceover/utils.py +++ b/backend/voiceover/utils.py @@ -182,22 +182,19 @@ def get_tts_output(tts_input, target_language, multiple_speaker, gender): "message": "Error in TTS API. Target Language is not supported.", "status": status.HTTP_400_BAD_REQUEST, } - try: - response = requests.post( - tts_url, - headers={"authorization": dhruva_key}, - json=json_data, - ) - tts_output = response.json() - # Collect the audios - return tts_output - - except Exception as e: - logging.info("Error in TTS API %s", str(e)) + response = requests.post( + tts_url, + headers={"authorization": dhruva_key}, + json=json_data, + ) + if response.status_code != 200: + logging.info("Error in TTS API %s", str(response.status_code)) return { "message": "Error in TTS API. Invalid sentence was passed.", "status": status.HTTP_400_BAD_REQUEST, } + tts_output = response.json() + return tts_output def generate_tts_output( @@ -296,13 +293,13 @@ def generate_tts_output( if ind not in empty_sentences: logging.info("Count of audios saved %s", str(count)) - audio_file = "temp_1.wav" + wave_audio = "temp_" + str(ind) + ".wav" audio_decoded = base64.b64decode(tts_output["audio"][count]["audioContent"]) - with open(audio_file, "wb") as output_f: + with open(wave_audio, "wb") as output_f: output_f.write(audio_decoded) - audio = AudioFileClip("temp_1.wav") + audio = AudioFileClip(wave_audio) wav_seconds = audio.duration - AudioSegment.from_wav("temp_1.wav").export("temp_1.ogg", format="ogg") + AudioSegment.from_wav(wave_audio).export("temp_1.ogg", format="ogg") logging.info("Seconds of wave audio %s", str(wav_seconds)) audio = AudioFileClip("temp_1.ogg") seconds = audio.duration @@ -311,6 +308,7 @@ def generate_tts_output( encoded_audio = base64.b64encode(open("temp_1.ogg", "rb").read()) decoded_audio = encoded_audio.decode() os.remove("temp_1.ogg") + os.remove(wave_audio) payload_size = payload_size + asizeof(decoded_audio) logging.info("Payload size %s", str(asizeof(decoded_audio))) logging.info("Index %s", str(ind)) @@ -328,7 +326,7 @@ def generate_tts_output( pass logging.info("Size of voiceover payload %s", str(asizeof(voiceover_payload))) logging.info("Size of combined audios %s", str(payload_size)) - os.remove("temp_1.wav") + # os.remove("temp_1.wav") return voiceover_payload