Files
encoach_backend/generate_speaking_questions.py
2023-09-03 18:05:13 +01:00

72 lines
2.4 KiB
Python

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
load_dotenv()
# Initialize Firebase Admin SDK
cred = credentials.Certificate(os.getenv("GOOGLE_APPLICATION_CREDENTIALS"))
firebase_admin.initialize_app(cred)
FIREBASE_BUCKET = 'mti-ielts.appspot.com'
VIDEO_FILES_PATH = 'download-video/'
FIREBASE_SPEAKING_VIDEO_FILES_PATH = 'speaking_videos/'
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?"
]
}
questions = []
for question in questions_json["questions"]:
result = create_video(question)
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)
if len(questions) == len(questions_json["questions"]):
speaking_pt3_to_insert = {
"exercises": [
{
"id": str(uuid.uuid4()),
"prompts": questions,
"text": "Listen carefully and respond.",
"title": questions_json["topic"],
"type": "speakingPart3"
}
],
"isDiagnostic": True,
"minTimer": 5,
"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_pt3_to_insert)
print(f"Document added with ID: {document_ref}")
else:
print("Array sizes do not match. Video uploading failing is probably the cause.")