Skip to content

Commit

Permalink
Merge pull request #436 from AI4Bharat/vo_fixes
Browse files Browse the repository at this point in the history
Vo fixes
  • Loading branch information
Shruti1229 authored Jul 28, 2023
2 parents eb43ecc + dfa13da commit 5e736dd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
11 changes: 9 additions & 2 deletions backend/task/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,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
)
Expand Down
32 changes: 15 additions & 17 deletions backend/voiceover/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand All @@ -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


Expand Down

0 comments on commit 5e736dd

Please sign in to comment.