Replace prints with proper logs.
This commit is contained in:
21
app.py
21
app.py
@@ -13,6 +13,7 @@ from helper.token_counter import count_tokens
|
||||
from helper.openai_interface import make_openai_call, make_openai_instruct_call
|
||||
import os
|
||||
import re
|
||||
import logging
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
@@ -32,6 +33,9 @@ firebase_admin.initialize_app(cred)
|
||||
|
||||
thread_event = threading.Event()
|
||||
|
||||
# Configure logging
|
||||
logging.basicConfig(level=logging.DEBUG, # Set the logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
|
||||
format='%(asctime)s - %(levelname)s - %(message)s')
|
||||
|
||||
@app.route('/healthcheck', methods=['GET'])
|
||||
def healthcheck():
|
||||
@@ -54,7 +58,7 @@ def get_listening_section_1_question():
|
||||
|
||||
unprocessed_conversation, processed_conversation = generate_listening_1_conversation(topic)
|
||||
|
||||
print("Generated conversation: " + str(processed_conversation))
|
||||
logging.info("Generated conversation: " + str(processed_conversation))
|
||||
|
||||
start_id = 1
|
||||
exercises = generate_listening_conversation_exercises(unprocessed_conversation, req_exercises,
|
||||
@@ -84,7 +88,7 @@ def get_listening_section_2_question():
|
||||
|
||||
monologue = generate_listening_2_monologue(topic)
|
||||
|
||||
print("Generated monologue: " + str(monologue))
|
||||
logging.info("Generated monologue: " + str(monologue))
|
||||
start_id = 11
|
||||
exercises = generate_listening_monologue_exercises(monologue, req_exercises, number_of_exercises_q, start_id)
|
||||
return {
|
||||
@@ -111,7 +115,7 @@ def get_listening_section_3_question():
|
||||
|
||||
unprocessed_conversation, processed_conversation = generate_listening_3_conversation(topic)
|
||||
|
||||
print("Generated conversation: " + str(processed_conversation))
|
||||
logging.info("Generated conversation: " + str(processed_conversation))
|
||||
|
||||
start_id = 21
|
||||
exercises = generate_listening_conversation_exercises(unprocessed_conversation, req_exercises,
|
||||
@@ -141,7 +145,7 @@ def get_listening_section_4_question():
|
||||
|
||||
monologue = generate_listening_4_monologue(topic)
|
||||
|
||||
print("Generated monologue: " + str(monologue))
|
||||
logging.info("Generated monologue: " + str(monologue))
|
||||
start_id = 31
|
||||
exercises = generate_listening_monologue_exercises(monologue, req_exercises, number_of_exercises_q, start_id)
|
||||
return {
|
||||
@@ -528,7 +532,7 @@ def save_speaking():
|
||||
exercises = data.get('exercises')
|
||||
template = getSpeakingTemplate()
|
||||
id = str(uuid.uuid4())
|
||||
|
||||
logging.info('Received request to save speaking with id: ' + id)
|
||||
thread_event.set()
|
||||
thread = threading.Thread(
|
||||
target=create_videos_and_save_to_db,
|
||||
@@ -536,6 +540,7 @@ def save_speaking():
|
||||
name=("thread-save-speaking-" + id)
|
||||
)
|
||||
thread.start()
|
||||
logging.info('Started thread to save speaking. Thread: ' + thread.getName())
|
||||
|
||||
# Return response without waiting for create_videos_and_save_to_db to finish
|
||||
return {**template, "id": id}
|
||||
@@ -557,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))
|
||||
|
||||
passage = generate_reading_passage(QuestionType.READING_PASSAGE_1, topic)
|
||||
print("Generated passage: " + str(passage))
|
||||
logging.info("Generated passage: " + str(passage))
|
||||
start_id = 1
|
||||
exercises = generate_reading_exercises(passage["text"], req_exercises, number_of_exercises_q, start_id)
|
||||
return {
|
||||
@@ -585,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))
|
||||
|
||||
passage = generate_reading_passage(QuestionType.READING_PASSAGE_2, topic)
|
||||
print("Generated passage: " + str(passage))
|
||||
logging.info("Generated passage: " + str(passage))
|
||||
start_id = 14
|
||||
exercises = generate_reading_exercises(passage["text"], req_exercises, number_of_exercises_q, start_id)
|
||||
return {
|
||||
@@ -613,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))
|
||||
|
||||
passage = generate_reading_passage(QuestionType.READING_PASSAGE_3, topic)
|
||||
print("Generated passage: " + str(passage))
|
||||
logging.info("Generated passage: " + str(passage))
|
||||
start_id = 27
|
||||
exercises = generate_reading_exercises(passage["text"], req_exercises, number_of_exercises_q, start_id)
|
||||
return {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import logging
|
||||
|
||||
from google.cloud import storage
|
||||
import os
|
||||
import uuid
|
||||
@@ -13,7 +15,7 @@ def download_firebase_file(bucket_name, source_blob_name, destination_file_name)
|
||||
bucket = storage_client.bucket(bucket_name)
|
||||
blob = bucket.blob(source_blob_name)
|
||||
blob.download_to_filename(destination_file_name)
|
||||
print(f"File downloaded to {destination_file_name}")
|
||||
logging.info(f"File uploaded to {destination_file_name}")
|
||||
return destination_file_name
|
||||
|
||||
def upload_file_firebase(bucket_name, destination_blob_name, source_file_name):
|
||||
@@ -23,10 +25,10 @@ def upload_file_firebase(bucket_name, destination_blob_name, source_file_name):
|
||||
try:
|
||||
blob = bucket.blob(destination_blob_name)
|
||||
blob.upload_from_filename(source_file_name)
|
||||
print(f"File uploaded to {destination_blob_name}")
|
||||
logging.info(f"File uploaded to {destination_blob_name}")
|
||||
return True
|
||||
except Exception as e:
|
||||
print("Error uploading file to Google Cloud Storage:", e)
|
||||
logging.error("Error uploading file to Google Cloud Storage: " + str(e))
|
||||
return False
|
||||
|
||||
def upload_file_firebase_get_url(bucket_name, destination_blob_name, source_file_name):
|
||||
@@ -36,7 +38,7 @@ def upload_file_firebase_get_url(bucket_name, destination_blob_name, source_file
|
||||
try:
|
||||
blob = bucket.blob(destination_blob_name)
|
||||
blob.upload_from_filename(source_file_name)
|
||||
print(f"File uploaded to {destination_blob_name}")
|
||||
logging.info(f"File uploaded to {destination_blob_name}")
|
||||
|
||||
# Make the file public
|
||||
blob.make_public()
|
||||
@@ -45,7 +47,7 @@ def upload_file_firebase_get_url(bucket_name, destination_blob_name, source_file
|
||||
url = blob.public_url
|
||||
return url
|
||||
except Exception as e:
|
||||
print("Error uploading file to Google Cloud Storage:", e)
|
||||
logging.error("Error uploading file to Google Cloud Storage: " + str(e))
|
||||
return None
|
||||
|
||||
def save_to_db(collection: str, item):
|
||||
@@ -53,7 +55,7 @@ def save_to_db(collection: str, item):
|
||||
collection_ref = db.collection(collection)
|
||||
(update_time, document_ref) = collection_ref.add(item)
|
||||
if document_ref:
|
||||
print(f"Document added with ID: {document_ref.id}")
|
||||
logging.info(f"Document added with ID: {document_ref.id}")
|
||||
return (True, document_ref.id)
|
||||
else:
|
||||
return (False, None)
|
||||
@@ -67,7 +69,7 @@ def save_to_db_with_id(collection: str, item, id: str):
|
||||
# Set the data to the document
|
||||
document_ref.set(item)
|
||||
if document_ref:
|
||||
print(f"Document added with ID: {document_ref.id}")
|
||||
logging.info(f"Document added with ID: {document_ref.id}")
|
||||
return (True, document_ref.id)
|
||||
else:
|
||||
return (False, None)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import logging
|
||||
import os
|
||||
import random
|
||||
|
||||
@@ -5,6 +6,8 @@ import requests
|
||||
import time
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
import app
|
||||
from helper.constants import *
|
||||
from helper.firebase_helper import upload_file_firebase_get_url, save_to_db_with_id
|
||||
from heygen.AvatarEnum import AvatarEnum
|
||||
@@ -35,6 +38,7 @@ TYLER_CHRISTOPHER = "03c796f8ed274bb38f19e893bcbc6121"
|
||||
|
||||
def create_videos_and_save_to_db(exercises, template, id):
|
||||
# Speaking 1
|
||||
logging.info('Creating video for speaking part 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
|
||||
@@ -47,9 +51,10 @@ def create_videos_and_save_to_db(exercises, template, id):
|
||||
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"])
|
||||
logging.error("Failed to create video for part 1 question: " + exercises[0]["question"])
|
||||
|
||||
# Speaking 2
|
||||
logging.info('Creating video for speaking part 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
|
||||
@@ -63,11 +68,12 @@ def create_videos_and_save_to_db(exercises, template, id):
|
||||
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"])
|
||||
logging.error("Failed to create video for part 2 question: " + exercises[1]["question"])
|
||||
|
||||
# Speaking 3
|
||||
sp3_questions = []
|
||||
avatar = random.choice(list(AvatarEnum))
|
||||
logging.info('Creating videos for speaking part 3')
|
||||
for question in exercises[2]["questions"]:
|
||||
result = create_video(question, avatar)
|
||||
if result is not None:
|
||||
@@ -81,7 +87,7 @@ def create_videos_and_save_to_db(exercises, template, id):
|
||||
}
|
||||
sp3_questions.append(video)
|
||||
else:
|
||||
print("Failed to create video for part 3 question: " + question)
|
||||
logging.error("Failed to create video for part 3 question: " + question)
|
||||
template["exercises"][2]["prompts"] = sp3_questions
|
||||
template["exercises"][2]["title"] = exercises[2]["topic"]
|
||||
|
||||
@@ -103,8 +109,8 @@ def create_video(text, avatar: AvatarEnum):
|
||||
]
|
||||
}
|
||||
response = requests.post(CREATE_VIDEO_URL, headers=POST_HEADER, json=data)
|
||||
print(response.status_code)
|
||||
print(response.json())
|
||||
logging.info(response.status_code)
|
||||
logging.info(response.json())
|
||||
|
||||
# GET TO CHECK STATUS AND GET VIDEO WHEN READY
|
||||
video_id = response.json()["data"]["video_id"]
|
||||
@@ -123,11 +129,11 @@ def create_video(text, avatar: AvatarEnum):
|
||||
error = response_data["data"]["error"]
|
||||
|
||||
if status != "completed" and error is None:
|
||||
print(f"Status: {status}")
|
||||
logging.info(f"Status: {status}")
|
||||
time.sleep(5) # Wait for 5 second before the next request
|
||||
|
||||
print(response.status_code)
|
||||
print(response.json())
|
||||
logging.info(response.status_code)
|
||||
logging.info(response.json())
|
||||
|
||||
# DOWNLOAD VIDEO
|
||||
download_url = response.json()['data']['video_url']
|
||||
@@ -141,8 +147,8 @@ def create_video(text, avatar: AvatarEnum):
|
||||
output_path = os.path.join(output_directory, output_filename)
|
||||
with open(output_path, 'wb') as f:
|
||||
f.write(response.content)
|
||||
print(f"File '{output_filename}' downloaded successfully.")
|
||||
logging.info(f"File '{output_filename}' downloaded successfully.")
|
||||
return output_filename
|
||||
else:
|
||||
print(f"Failed to download file. Status code: {response.status_code}")
|
||||
logging.error(f"Failed to download file. Status code: {response.status_code}")
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user