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_admin.initialize_app(cred) FIREBASE_BUCKET = 'mti-ielts.appspot.com' VIDEO_FILES_PATH = 'download-video/' FIREBASE_SPEAKING_VIDEO_FILES_PATH = 'speaking_videos/' # 1 # 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 # 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 # 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 # 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?" # ] # } # 5 questions_json = { "topic": "Hobbies and Interests", "questions": [ "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?", ] } questions = [] for question in questions_json["questions"]: result = create_video(question, AvatarEnum.MATTHEW_NOAH) 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": "interactiveSpeaking" } ], "isDiagnostic": False, "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_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.")