Save speaking asynchronously

This commit is contained in:
Cristiano Ferreira
2023-12-10 15:38:26 +00:00
parent 50c39e5f9c
commit 9f4aed52ae
3 changed files with 91 additions and 63 deletions

View File

@@ -1,9 +1,12 @@
import os
import random
import requests
import time
from dotenv import load_dotenv
from helper.constants import *
from helper.firebase_helper import upload_file_firebase_get_url, save_to_db_with_id
from heygen.AvatarEnum import AvatarEnum
load_dotenv()
@@ -30,6 +33,59 @@ KAYLA_ABBI = "d688099f8db9472cb4890b0561e81793"
JEROME_RYAN = "ad41feb2a5c4483085525e3d8907f512"
TYLER_CHRISTOPHER = "03c796f8ed274bb38f19e893bcbc6121"
def create_videos_and_save_to_db(exercises, template, id):
# Speaking 1
sp1_result = create_video(exercises[0]["question"], random.choice(list(AvatarEnum)))
if sp1_result is not None:
sound_file_path = VIDEO_FILES_PATH + sp1_result
firebase_file_path = FIREBASE_SPEAKING_VIDEO_FILES_PATH + sp1_result
url = upload_file_firebase_get_url(FIREBASE_BUCKET, firebase_file_path, sound_file_path)
sp1_video_path = firebase_file_path
sp1_video_url = url
template["exercises"][0]["text"] = exercises[0]["question"]
template["exercises"][0]["title"] = exercises[0]["topic"]
template["exercises"][0]["video_url"] = sp1_video_url
template["exercises"][0]["video_path"] = sp1_video_path
else:
print("Failed to create video for part 1 question: " + exercises[0]["question"])
# Speaking 2
sp2_result = create_video(exercises[1]["question"], random.choice(list(AvatarEnum)))
if sp2_result is not None:
sound_file_path = VIDEO_FILES_PATH + sp2_result
firebase_file_path = FIREBASE_SPEAKING_VIDEO_FILES_PATH + sp2_result
url = upload_file_firebase_get_url(FIREBASE_BUCKET, firebase_file_path, sound_file_path)
sp2_video_path = firebase_file_path
sp2_video_url = url
template["exercises"][1]["prompts"] = exercises[1]["prompts"]
template["exercises"][1]["text"] = exercises[1]["question"]
template["exercises"][1]["title"] = exercises[1]["topic"]
template["exercises"][1]["video_url"] = sp2_video_url
template["exercises"][1]["video_path"] = sp2_video_path
else:
print("Failed to create video for part 2 question: " + exercises[1]["question"])
# Speaking 3
sp3_questions = []
avatar = random.choice(list(AvatarEnum))
for question in exercises[2]["questions"]:
result = create_video(question, avatar)
if result is not None:
sound_file_path = VIDEO_FILES_PATH + result
firebase_file_path = FIREBASE_SPEAKING_VIDEO_FILES_PATH + result
url = upload_file_firebase_get_url(FIREBASE_BUCKET, firebase_file_path, sound_file_path)
video = {
"text": question,
"video_path": firebase_file_path,
"video_url": url
}
sp3_questions.append(video)
else:
print("Failed to create video for part 3 question: " + question)
template["exercises"][2]["prompts"] = sp3_questions
template["exercises"][2]["title"] = exercises[2]["topic"]
save_to_db_with_id("speaking", template, id)
def create_video(text, avatar: AvatarEnum):
# POST TO CREATE VIDEO