Replace logging with app.logger.

This commit is contained in:
Cristiano Ferreira
2023-12-12 23:02:00 +00:00
parent 2b91cfe26d
commit 479620116d
3 changed files with 27 additions and 24 deletions

18
app.py
View File

@@ -58,7 +58,7 @@ def get_listening_section_1_question():
unprocessed_conversation, processed_conversation = generate_listening_1_conversation(topic) unprocessed_conversation, processed_conversation = generate_listening_1_conversation(topic)
logging.info("Generated conversation: " + str(processed_conversation)) app.logger.info("Generated conversation: " + str(processed_conversation))
start_id = 1 start_id = 1
exercises = generate_listening_conversation_exercises(unprocessed_conversation, req_exercises, exercises = generate_listening_conversation_exercises(unprocessed_conversation, req_exercises,
@@ -88,7 +88,7 @@ def get_listening_section_2_question():
monologue = generate_listening_2_monologue(topic) monologue = generate_listening_2_monologue(topic)
logging.info("Generated monologue: " + str(monologue)) app.logger.info("Generated monologue: " + str(monologue))
start_id = 11 start_id = 11
exercises = generate_listening_monologue_exercises(monologue, req_exercises, number_of_exercises_q, start_id) exercises = generate_listening_monologue_exercises(monologue, req_exercises, number_of_exercises_q, start_id)
return { return {
@@ -115,7 +115,7 @@ def get_listening_section_3_question():
unprocessed_conversation, processed_conversation = generate_listening_3_conversation(topic) unprocessed_conversation, processed_conversation = generate_listening_3_conversation(topic)
logging.info("Generated conversation: " + str(processed_conversation)) app.logger.info("Generated conversation: " + str(processed_conversation))
start_id = 21 start_id = 21
exercises = generate_listening_conversation_exercises(unprocessed_conversation, req_exercises, exercises = generate_listening_conversation_exercises(unprocessed_conversation, req_exercises,
@@ -145,7 +145,7 @@ def get_listening_section_4_question():
monologue = generate_listening_4_monologue(topic) monologue = generate_listening_4_monologue(topic)
logging.info("Generated monologue: " + str(monologue)) app.logger.info("Generated monologue: " + str(monologue))
start_id = 31 start_id = 31
exercises = generate_listening_monologue_exercises(monologue, req_exercises, number_of_exercises_q, start_id) exercises = generate_listening_monologue_exercises(monologue, req_exercises, number_of_exercises_q, start_id)
return { return {
@@ -532,7 +532,7 @@ def save_speaking():
exercises = data.get('exercises') exercises = data.get('exercises')
template = getSpeakingTemplate() template = getSpeakingTemplate()
id = str(uuid.uuid4()) id = str(uuid.uuid4())
logging.info('Received request to save speaking with id: ' + id) app.logger.info('Received request to save speaking with id: ' + id)
thread_event.set() thread_event.set()
thread = threading.Thread( thread = threading.Thread(
target=create_videos_and_save_to_db, target=create_videos_and_save_to_db,
@@ -540,7 +540,7 @@ def save_speaking():
name=("thread-save-speaking-" + id) name=("thread-save-speaking-" + id)
) )
thread.start() thread.start()
logging.info('Started thread to save speaking. Thread: ' + thread.getName()) app.logger.info('Started thread to save speaking. Thread: ' + thread.getName())
# Return response without waiting for create_videos_and_save_to_db to finish # Return response without waiting for create_videos_and_save_to_db to finish
return {**template, "id": id} return {**template, "id": id}
@@ -562,7 +562,7 @@ def get_reading_passage_1_question():
number_of_exercises_q = divide_number_into_parts(TOTAL_READING_PASSAGE_1_EXERCISES, len(req_exercises)) number_of_exercises_q = divide_number_into_parts(TOTAL_READING_PASSAGE_1_EXERCISES, len(req_exercises))
passage = generate_reading_passage(QuestionType.READING_PASSAGE_1, topic) passage = generate_reading_passage(QuestionType.READING_PASSAGE_1, topic)
logging.info("Generated passage: " + str(passage)) app.logger.info("Generated passage: " + str(passage))
start_id = 1 start_id = 1
exercises = generate_reading_exercises(passage["text"], req_exercises, number_of_exercises_q, start_id) exercises = generate_reading_exercises(passage["text"], req_exercises, number_of_exercises_q, start_id)
return { return {
@@ -590,7 +590,7 @@ def get_reading_passage_2_question():
number_of_exercises_q = divide_number_into_parts(TOTAL_READING_PASSAGE_2_EXERCISES, len(req_exercises)) number_of_exercises_q = divide_number_into_parts(TOTAL_READING_PASSAGE_2_EXERCISES, len(req_exercises))
passage = generate_reading_passage(QuestionType.READING_PASSAGE_2, topic) passage = generate_reading_passage(QuestionType.READING_PASSAGE_2, topic)
logging.info("Generated passage: " + str(passage)) app.logger.info("Generated passage: " + str(passage))
start_id = 14 start_id = 14
exercises = generate_reading_exercises(passage["text"], req_exercises, number_of_exercises_q, start_id) exercises = generate_reading_exercises(passage["text"], req_exercises, number_of_exercises_q, start_id)
return { return {
@@ -618,7 +618,7 @@ def get_reading_passage_3_question():
number_of_exercises_q = divide_number_into_parts(TOTAL_READING_PASSAGE_3_EXERCISES, len(req_exercises)) number_of_exercises_q = divide_number_into_parts(TOTAL_READING_PASSAGE_3_EXERCISES, len(req_exercises))
passage = generate_reading_passage(QuestionType.READING_PASSAGE_3, topic) passage = generate_reading_passage(QuestionType.READING_PASSAGE_3, topic)
logging.info("Generated passage: " + str(passage)) app.logger.info("Generated passage: " + str(passage))
start_id = 27 start_id = 27
exercises = generate_reading_exercises(passage["text"], req_exercises, number_of_exercises_q, start_id) exercises = generate_reading_exercises(passage["text"], req_exercises, number_of_exercises_q, start_id)
return { return {

View File

@@ -9,6 +9,9 @@ from firebase_admin import credentials, firestore
from dotenv import load_dotenv from dotenv import load_dotenv
import app
def download_firebase_file(bucket_name, source_blob_name, destination_file_name): def download_firebase_file(bucket_name, source_blob_name, destination_file_name):
# Downloads a file from Firebase Storage. # Downloads a file from Firebase Storage.
storage_client = storage.Client() storage_client = storage.Client()
@@ -28,7 +31,7 @@ def upload_file_firebase(bucket_name, destination_blob_name, source_file_name):
logging.info(f"File uploaded to {destination_blob_name}") logging.info(f"File uploaded to {destination_blob_name}")
return True return True
except Exception as e: except Exception as e:
logging.error("Error uploading file to Google Cloud Storage: " + str(e)) app.app.logger.error("Error uploading file to Google Cloud Storage: " + str(e))
return False return False
def upload_file_firebase_get_url(bucket_name, destination_blob_name, source_file_name): def upload_file_firebase_get_url(bucket_name, destination_blob_name, source_file_name):
@@ -47,7 +50,7 @@ def upload_file_firebase_get_url(bucket_name, destination_blob_name, source_file
url = blob.public_url url = blob.public_url
return url return url
except Exception as e: except Exception as e:
logging.error("Error uploading file to Google Cloud Storage: " + str(e)) app.app.logger.error("Error uploading file to Google Cloud Storage: " + str(e))
return None return None
def save_to_db(collection: str, item): def save_to_db(collection: str, item):

View File

@@ -38,7 +38,7 @@ TYLER_CHRISTOPHER = "03c796f8ed274bb38f19e893bcbc6121"
def create_videos_and_save_to_db(exercises, template, id): def create_videos_and_save_to_db(exercises, template, id):
# Speaking 1 # Speaking 1
logging.info('Creating video for speaking part 1') app.app.logger.info('Creating video for speaking part 1')
sp1_result = create_video(exercises[0]["question"], random.choice(list(AvatarEnum))) sp1_result = create_video(exercises[0]["question"], random.choice(list(AvatarEnum)))
if sp1_result is not None: if sp1_result is not None:
sound_file_path = VIDEO_FILES_PATH + sp1_result sound_file_path = VIDEO_FILES_PATH + sp1_result
@@ -51,10 +51,10 @@ def create_videos_and_save_to_db(exercises, template, id):
template["exercises"][0]["video_url"] = sp1_video_url template["exercises"][0]["video_url"] = sp1_video_url
template["exercises"][0]["video_path"] = sp1_video_path template["exercises"][0]["video_path"] = sp1_video_path
else: else:
logging.error("Failed to create video for part 1 question: " + exercises[0]["question"]) app.app.logger.error("Failed to create video for part 1 question: " + exercises[0]["question"])
# Speaking 2 # Speaking 2
logging.info('Creating video for speaking part 2') app.app.logger.info('Creating video for speaking part 2')
sp2_result = create_video(exercises[1]["question"], random.choice(list(AvatarEnum))) sp2_result = create_video(exercises[1]["question"], random.choice(list(AvatarEnum)))
if sp2_result is not None: if sp2_result is not None:
sound_file_path = VIDEO_FILES_PATH + sp2_result sound_file_path = VIDEO_FILES_PATH + sp2_result
@@ -68,12 +68,12 @@ def create_videos_and_save_to_db(exercises, template, id):
template["exercises"][1]["video_url"] = sp2_video_url template["exercises"][1]["video_url"] = sp2_video_url
template["exercises"][1]["video_path"] = sp2_video_path template["exercises"][1]["video_path"] = sp2_video_path
else: else:
logging.error("Failed to create video for part 2 question: " + exercises[1]["question"]) app.app.logger.error("Failed to create video for part 2 question: " + exercises[1]["question"])
# Speaking 3 # Speaking 3
sp3_questions = [] sp3_questions = []
avatar = random.choice(list(AvatarEnum)) avatar = random.choice(list(AvatarEnum))
logging.info('Creating videos for speaking part 3') app.app.logger.info('Creating videos for speaking part 3')
for question in exercises[2]["questions"]: for question in exercises[2]["questions"]:
result = create_video(question, avatar) result = create_video(question, avatar)
if result is not None: if result is not None:
@@ -87,7 +87,7 @@ def create_videos_and_save_to_db(exercises, template, id):
} }
sp3_questions.append(video) sp3_questions.append(video)
else: else:
logging.error("Failed to create video for part 3 question: " + question) app.app.logger.error("Failed to create video for part 3 question: " + question)
template["exercises"][2]["prompts"] = sp3_questions template["exercises"][2]["prompts"] = sp3_questions
template["exercises"][2]["title"] = exercises[2]["topic"] template["exercises"][2]["title"] = exercises[2]["topic"]
@@ -109,8 +109,8 @@ def create_video(text, avatar: AvatarEnum):
] ]
} }
response = requests.post(CREATE_VIDEO_URL, headers=POST_HEADER, json=data) response = requests.post(CREATE_VIDEO_URL, headers=POST_HEADER, json=data)
logging.info(response.status_code) app.app.logger.info(response.status_code)
logging.info(response.json()) app.app.logger.info(response.json())
# GET TO CHECK STATUS AND GET VIDEO WHEN READY # GET TO CHECK STATUS AND GET VIDEO WHEN READY
video_id = response.json()["data"]["video_id"] video_id = response.json()["data"]["video_id"]
@@ -129,11 +129,11 @@ def create_video(text, avatar: AvatarEnum):
error = response_data["data"]["error"] error = response_data["data"]["error"]
if status != "completed" and error is None: if status != "completed" and error is None:
logging.info(f"Status: {status}") app.app.logger.info(f"Status: {status}")
time.sleep(5) # Wait for 5 second before the next request time.sleep(5) # Wait for 5 second before the next request
logging.info(response.status_code) app.app.logger.info(response.status_code)
logging.info(response.json()) app.app.logger.info(response.json())
# DOWNLOAD VIDEO # DOWNLOAD VIDEO
download_url = response.json()['data']['video_url'] download_url = response.json()['data']['video_url']
@@ -147,8 +147,8 @@ def create_video(text, avatar: AvatarEnum):
output_path = os.path.join(output_directory, output_filename) output_path = os.path.join(output_directory, output_filename)
with open(output_path, 'wb') as f: with open(output_path, 'wb') as f:
f.write(response.content) f.write(response.content)
logging.info(f"File '{output_filename}' downloaded successfully.") app.app.logger.info(f"File '{output_filename}' downloaded successfully.")
return output_filename return output_filename
else: else:
logging.error(f"Failed to download file. Status code: {response.status_code}") app.app.logger.error(f"Failed to download file. Status code: {response.status_code}")
return None return None