From ac7ba2edfa921a6f8ce2b5e4abb15831390afe99 Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Tue, 23 Jan 2024 17:32:15 +0000 Subject: [PATCH] Added two more endpoints for the Speaking generation --- app.py | 70 +++++++++++++++++++++++++++++++++++++++++--- helper/heygen_api.py | 1 + 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 237fe3a..7a897f7 100644 --- a/app.py +++ b/app.py @@ -9,7 +9,8 @@ from helper.api_messages import * from helper.exercises import * from helper.file_helper import delete_files_older_than_one_day from helper.firebase_helper import * -from helper.heygen_api import create_videos_and_save_to_db +from helper.heygen_api import create_video, create_videos_and_save_to_db +from heygen.AvatarEnum import AvatarEnum from helper.speech_to_text_helper import * from helper.openai_interface import * import os @@ -177,7 +178,7 @@ def save_listening(): else: text_to_speech(part["text"], sound_file_path) file_url = upload_file_firebase_get_url(FIREBASE_BUCKET, firebase_file_path, sound_file_path) - + part_template["audio"]["source"] = file_url part_template["exercises"] = part["exercises"] @@ -563,9 +564,9 @@ def save_speaking(): exercises = data.get('exercises') minTimer = data.get('minTimer', SPEAKING_MIN_TIMER_DEFAULT) template = getSpeakingTemplate() + template["minTimer"] = minTimer - if minTimer != SPEAKING_MIN_TIMER_DEFAULT: - template["minTimer"] = minTimer + if minTimer < SPEAKING_MIN_TIMER_DEFAULT: template["variant"] = ExamVariant.PARTIAL.value else: template["variant"] = ExamVariant.FULL.value @@ -586,6 +587,67 @@ def save_speaking(): except Exception as e: return str(e) +@app.route("/speaking/generate_speaking_video", methods=['POST']) +@jwt_required() +def generate_speaking_video(): + try: + data = request.get_json() + sp1_result = create_video(data["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 + + return { + "text": data["question"], + "prompts": data["prompts"], + "title": data["topic"], + "video_url": sp1_video_url, + "video_path": sp1_video_path, + "type": "speaking", + "id": uuid.uuid4() + } + else: + app.logger.error("Failed to create video for part 1 question: " + data["question"]) + return str("Failed to create video for part 1 question: " + data["question"]) + + except Exception as e: + return str(e) + +@app.route("/speaking/generate_interactive_video", methods=['POST']) +@jwt_required() +def generate_interactive_video(): + try: + data = request.get_json() + sp3_questions = [] + avatar = random.choice(list(AvatarEnum)) + + app.logger.info('Creating videos for speaking part 3') + for question in data["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: + app.app.logger.error("Failed to create video for part 3 question: " + question) + + return { + "prompts": sp3_questions, + "title": data["topic"], + "type": "interactiveSpeaking", + "id": uuid.uuid4() + } + except Exception as e: + return str(e) @app.route('/reading_passage_1', methods=['GET']) @jwt_required() diff --git a/helper/heygen_api.py b/helper/heygen_api.py index b96478d..5f69444 100644 --- a/helper/heygen_api.py +++ b/helper/heygen_api.py @@ -36,6 +36,7 @@ KAYLA_ABBI = "d688099f8db9472cb4890b0561e81793" JEROME_RYAN = "ad41feb2a5c4483085525e3d8907f512" TYLER_CHRISTOPHER = "03c796f8ed274bb38f19e893bcbc6121" + def create_videos_and_save_to_db(exercises, template, id): # Speaking 1 # Using list comprehension to find the element with the desired value in the 'type' field