From b93ead3a7b71d63e44653dd2dda71ddb388cc920 Mon Sep 17 00:00:00 2001 From: Cristiano Ferreira Date: Mon, 17 Jun 2024 22:51:59 +0100 Subject: [PATCH] Update speaking generation endpoints. --- app.py | 136 +++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 114 insertions(+), 22 deletions(-) diff --git a/app.py b/app.py index e7fa177..a2a2fe2 100644 --- a/app.py +++ b/app.py @@ -351,12 +351,12 @@ def grade_writing_task_2(): { "role": "user", "content": ( - 'Evaluate the given Writing Task 2 response based on the IELTS grading system, ensuring a ' - 'strict assessment that penalizes errors. Deduct points for deviations from the task, and ' - 'assign a score of 0 if the response fails to address the question. Additionally, provide an ' - 'exemplary answer with a minimum of 250 words, along with a detailed commentary highlighting ' - 'both strengths and weaknesses in the response.' - '\n Question: "' + question + '" \n Answer: "' + answer + '"') + 'Evaluate the given Writing Task 2 response based on the IELTS grading system, ensuring a ' + 'strict assessment that penalizes errors. Deduct points for deviations from the task, and ' + 'assign a score of 0 if the response fails to address the question. Additionally, provide an ' + 'exemplary answer with a minimum of 250 words, along with a detailed commentary highlighting ' + 'both strengths and weaknesses in the response.' + '\n Question: "' + question + '" \n Answer: "' + answer + '"') }, { "role": "user", @@ -1080,21 +1080,91 @@ def save_speaking(): return str(e) -@app.route("/speaking/generate_speaking_video", methods=['POST']) +@app.route("/speaking/generate_video_1", methods=['POST']) @jwt_required() -def generate_speaking_video(): +def generate_video_1(): + try: + data = request.get_json() + sp3_questions = [] + avatar = data.get("avatar", random.choice(list(AvatarEnum)).value) + + request_id = str(uuid.uuid4()) + logging.info("POST - generate_video_1 - Received request to generate video 1. " + "Use this id to track the logs: " + str(request_id) + " - Request data: " + str( + request.get_json())) + + logging.info("POST - generate_video_1 - " + str(request_id) + " - Creating videos for speaking part 1.") + for question in data["questions"]: + logging.info("POST - generate_video_1 - " + str(request_id) + " - Creating video for question: " + question) + result = create_video(question, avatar) + logging.info("POST - generate_video_1 - " + str(request_id) + " - Video created: " + result) + if result is not None: + sound_file_path = VIDEO_FILES_PATH + result + firebase_file_path = FIREBASE_SPEAKING_VIDEO_FILES_PATH + result + logging.info( + "POST - generate_video_1 - " + str( + request_id) + " - Uploading video to firebase: " + firebase_file_path) + url = upload_file_firebase_get_url(FIREBASE_BUCKET, firebase_file_path, sound_file_path) + logging.info( + "POST - generate_video_1 - " + str( + request_id) + " - Uploaded video to firebase: " + url) + video = { + "text": question, + "video_path": firebase_file_path, + "video_url": url + } + sp3_questions.append(video) + else: + logging.error("POST - generate_video_1 - " + str( + request_id) + " - Failed to create video for part 1 question: " + question) + + response = { + "prompts": sp3_questions, + "first_title": data["first_topic"], + "second_title": data["second_topic"], + "type": "interactiveSpeaking", + "id": uuid.uuid4() + } + logging.info( + "POST - generate_video_1 - " + str( + request_id) + " - Finished creating videos for speaking part 1: " + str(response)) + return response + except Exception as e: + return str(e) + + +@app.route("/speaking/generate_video_2", methods=['POST']) +@jwt_required() +def generate_video_2(): try: data = request.get_json() avatar = data.get("avatar", random.choice(list(AvatarEnum)).value) prompts = data.get("prompts", []) question = data.get("question") - if len(prompts) > 0: - question = question + " In your answer you should consider: " + " ".join(prompts) - sp1_result = create_video(question, avatar) - if sp1_result is not None: - sound_file_path = VIDEO_FILES_PATH + sp1_result - firebase_file_path = FIREBASE_SPEAKING_VIDEO_FILES_PATH + sp1_result + suffix = data.get("suffix", "") + + question = question + " In your answer you should consider: " + " ".join(prompts) + suffix + + request_id = str(uuid.uuid4()) + logging.info("POST - generate_video_2 - Received request to generate video 2. " + "Use this id to track the logs: " + str(request_id) + " - Request data: " + str( + request.get_json())) + + logging.info("POST - generate_video_2 - " + str(request_id) + " - Creating video for speaking part 2.") + logging.info("POST - generate_video_2 - " + str(request_id) + " - Creating video for question: " + question) + result = create_video(question, avatar) + logging.info("POST - generate_video_2 - " + str(request_id) + " - Video created: " + result) + + if result is not None: + sound_file_path = VIDEO_FILES_PATH + result + firebase_file_path = FIREBASE_SPEAKING_VIDEO_FILES_PATH + result + logging.info( + "POST - generate_video_2 - " + str( + request_id) + " - Uploading video to firebase: " + firebase_file_path) url = upload_file_firebase_get_url(FIREBASE_BUCKET, firebase_file_path, sound_file_path) + logging.info( + "POST - generate_video_2 - " + str( + request_id) + " - Uploaded video to firebase: " + url) sp1_video_path = firebase_file_path sp1_video_url = url @@ -1105,31 +1175,47 @@ def generate_speaking_video(): "video_url": sp1_video_url, "video_path": sp1_video_path, "type": "speaking", - "id": uuid.uuid4() + "id": uuid.uuid4(), + "suffix": suffix } 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"]) + logging.error("POST - generate_video_2 - " + str( + request_id) + " - Failed to create video for part 2 question: " + question) + return str("Failed to create video for part 2 question: " + data["question"]) except Exception as e: return str(e) -@app.route("/speaking/generate_interactive_video", methods=['POST']) +@app.route("/speaking/generate_video_3", methods=['POST']) @jwt_required() -def generate_interactive_video(): +def generate_video_3(): try: data = request.get_json() sp3_questions = [] avatar = data.get("avatar", random.choice(list(AvatarEnum)).value) - app.logger.info('Creating videos for speaking part 3') + request_id = str(uuid.uuid4()) + logging.info("POST - generate_video_3 - Received request to generate video 3. " + "Use this id to track the logs: " + str(request_id) + " - Request data: " + str( + request.get_json())) + + logging.info("POST - generate_video_3 - " + str(request_id) + " - Creating videos for speaking part 3.") for question in data["questions"]: + logging.info("POST - generate_video_3 - " + str(request_id) + " - Creating video for question: " + question) result = create_video(question, avatar) + logging.info("POST - generate_video_3 - " + str(request_id) + " - Video created: " + result) + if result is not None: sound_file_path = VIDEO_FILES_PATH + result firebase_file_path = FIREBASE_SPEAKING_VIDEO_FILES_PATH + result + logging.info( + "POST - generate_video_3 - " + str( + request_id) + " - Uploading video to firebase: " + firebase_file_path) url = upload_file_firebase_get_url(FIREBASE_BUCKET, firebase_file_path, sound_file_path) + logging.info( + "POST - generate_video_3 - " + str( + request_id) + " - Uploaded video to firebase: " + url) video = { "text": question, "video_path": firebase_file_path, @@ -1137,14 +1223,19 @@ def generate_interactive_video(): } sp3_questions.append(video) else: - app.app.logger.error("Failed to create video for part 3 question: " + question) + logging.error("POST - generate_video_3 - " + str( + request_id) + " - Failed to create video for part 3 question: " + question) - return { + response = { "prompts": sp3_questions, "title": data["topic"], "type": "interactiveSpeaking", "id": uuid.uuid4() } + logging.info( + "POST - generate_video_3 - " + str( + request_id) + " - Finished creating videos for speaking part 3: " + str(response)) + return response except Exception as e: return str(e) @@ -1203,6 +1294,7 @@ def get_level_exam(): except Exception as e: return str(e) + @app.route('/level_utas', methods=['GET']) @jwt_required() def get_level_utas():