Files
encoach_backend/generate_speaking_questions.py

181 lines
6.6 KiB
Python

import random
from helper.firebase_helper import upload_file_firebase_get_url
from helper.heygen_api import create_video
import os
import uuid
import firebase_admin
from firebase_admin import credentials, firestore
from dotenv import load_dotenv
from heygen.AvatarEnum import AvatarEnum
load_dotenv()
# Initialize Firebase Admin SDK
cred = credentials.Certificate(os.getenv("GOOGLE_APPLICATION_CREDENTIALS"))
FIREBASE_BUCKET = os.getenv('FIREBASE_BUCKET')
firebase_admin.initialize_app(cred)
VIDEO_FILES_PATH = 'download-video/'
FIREBASE_SPEAKING_VIDEO_FILES_PATH = 'speaking_videos/'
# PART 1
sp1_video_url = ""
sp1_video_path = ""
# sp1_questions_json = {
# "topic": "Hobbies and Interests",
# "question": "What do you like to do in your free time? Do you have any hobbies or interests? Are there any new hobbies you would like to try in the future?"
# }
sp1_questions_json = {
"topic": "Hobbies and Interests",
"question": "Do you have any hobbies or interests that you enjoy in your free time? Please describe one of your favorite hobbies and explain why you like it."
}
sp1_result = create_video(sp1_questions_json["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
else:
print("Failed to create video for question: " + sp1_questions_json["question"])
# PART 2
sp2_video_url = ""
sp2_video_path = ""
# sp2_questions_json = {
# "topic": "New Environment",
# "question": "Describe an occasion when you had to adapt to a new environment."
# }
sp2_questions_json = {
"topic": "Travel and Adventure",
"question": "Describe an occasion when you had to adapt to a new environment.",
"prompts": [
"Please include details about the destination",
"What you did there",
"why this trip was memorable for you."
]
}
sp2_result = create_video(sp2_questions_json["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
else:
print("Failed to create video for question: " + sp2_questions_json["question"])
# PART 3
# 1
# sp3_questions_json = {
# "topic": "Technology and Society",
# "questions": [
# "How do you think technology has affected the way people communicate with each other in today's society?",
# "In what ways has the use of smartphones and social media platforms changed the dynamics of personal relationships?",
# "Some argue that technology has made communication more convenient, while others worry that it has led to a decline in face-to-face interactions. What's your perspective on this matter, and how do you think it might impact future generations?"
# ]
# }
# 2
# sp3_questions_json = {
# "topic": "Environmental Concerns",
# "questions": [
# "What do you believe are the most pressing environmental issues facing the world today?",
# "In your opinion, what role should governments play in addressing environmental concerns?",
# "Many people argue that individual actions, like recycling and conserving energy, are not sufficient to address environmental issues. What's your perspective on this? How can individuals contribute to solving these problems?"
# ]
# }
# 3
# sp3_questions_json = {
# "topic": "Education and Technology",
# "questions": [
# "How has technology impacted the way students learn in today's classrooms?",
# "Do you think traditional textbooks will become obsolete in the future due to digital learning materials? Why or why not?",
# "What are the potential advantages and disadvantages of online courses and e-learning platforms?"
# ]
# }
# 4
sp3_questions_json = {
"topic": "Cultural Diversity",
"questions": [
"How can exposure to different cultures benefit individuals and society as a whole?",
"What challenges might arise in culturally diverse communities, and how can they be addressed?",
"Do you think it's important for schools to incorporate cultural diversity into their curriculum? Why or why not?"
]
}
questions = []
avatar = random.choice(list(AvatarEnum))
for question in sp3_questions_json["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
}
questions.append(video)
else:
print("Failed to create video for question: " + question)
# BUILD OBJECT TO SAVE
if len(questions) == len(sp3_questions_json["questions"]):
speaking_module_to_insert = {
"exercises": [
{
"id": str(uuid.uuid4()),
"prompts": [],
"text": sp1_questions_json["question"],
"title": sp1_questions_json["topic"],
"video_url": sp1_video_url,
"video_path": sp1_video_path,
"type": "speaking"
},
{
"id": str(uuid.uuid4()),
"prompts": sp2_questions_json["prompts"],
"text": sp2_questions_json["question"],
"title": sp2_questions_json["topic"],
"video_url": sp2_video_url,
"video_path": sp2_video_path,
"type": "speaking"
},
{
"id": str(uuid.uuid4()),
"prompts": questions,
"text": "Listen carefully and respond.",
"title": sp3_questions_json["topic"],
"type": "interactiveSpeaking"
}
],
"isDiagnostic": True,
"minTimer": 14,
"module": "speaking"
}
db = firestore.client()
# JSON data to insert
# Add the JSON data to Firestore
collection_ref = db.collection('speaking')
document_ref = collection_ref.add(speaking_module_to_insert)
print(f"Document added with ID: {document_ref}")
else:
print("Array sizes do not match. Video uploading failing is probably the cause.")