improve dan rapikan code berikut , dan pastikan semua fungsi tetap berfungsi semesti nya, lakukan improvisasi jika di perlukan, buatkan dengan benar dan lengkap , dan pastikan script dapat di jalankan dengan benar pada google collab dan tambahkan agar script di bagi per cell run nya dan gunakan agar dapat di pilih untuk show code atau keterangan cell nya saja : from google.colab import drive
drive.mount('/content/drive')

!pip install azure-cognitiveservices-speech
!pip install git+https://github.com/Zulko/moviepy.git
!pip install gradio_client
!pip install pydub
!apt-get update
!apt-get install imagemagick

import random
import asyncio
import os
import sys
import re
import time
import requests
import json
import math
import difflib
import uuid
import colorsys
import nest_asyncio
import azure.cognitiveservices.speech as speechsdk
import google.generativeai as genai
import numpy as np
import xml.etree.ElementTree as ET
from pydub import AudioSegment
from pydub.silence import detect_silence
from PIL import Image
from gradio_client import Client
from moviepy.editor import VideoFileClip, concatenate_audioclips, AudioFileClip, TextClip, CompositeVideoClip, CompositeAudioClip, ImageClip, ColorClip

!cat /etc/ImageMagick-6/policy.xml | sed 's/none/read,write/g'> /etc/ImageMagick-6/policy.xml
nest_asyncio.apply()
print("Library Added.")

file_path = '/etc/ImageMagick-6/type-ghostscript.xml'
ttf_path = '/content/drive/MyDrive/chubbShort/LuckiestGuy-Regular.ttf'
tree = ET.parse(file_path)
root = tree.getroot()
new_font = ET.Element('type', {
    'name': 'Luckiest-Guy',
    'fullname': 'Luckiest Guy',
    'family': 'NewFont',
    'foundry': 'URW',
    'weight': '400',
    'style': 'normal',
    'stretch': 'normal',
    'format': 'type1',
    'metrics': ttf_path,
    'glyphs': ttf_path
})
root.append(new_font)
tree.write(file_path, encoding='utf-8', xml_declaration=True)
print("Font Added.")

async def text_to_speech(text_to_speech, character, tts_model_token, count, audio_dir):
    def request_tts(tts_model_token, text):
        url = 'https://api.fakeyou.com/tts/inference'
        headers = {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        }
        payload = {
            "uuid_idempotency_token": str(uuid.uuid4()),
            "tts_model_token": tts_model_token,
            "inference_text": text
        }
        response = requests.post(url, json=payload, headers=headers)
        if response.status_code == 200:
            result = response.json()
            if result.get('success'):
                return result['inference_job_token']
            else:
                raise Exception("Failed to request TTS: " + result.get('error', 'Unknown error'))
        else:
            raise Exception(f"Failed to request TTS with status code {response.status_code}")

    def check_tts_status(job_token):
        url = f'https://api.fakeyou.com/tts/job/{job_token}'
        headers = {
            'Accept': 'application/json'
        }
        response = requests.get(url, headers=headers)
        if response.status_code == 200:
            result = response.json()
            if result.get('success'):
                status = result['state']['status']
                if status == 'complete_success':
                    return result['state']['maybe_public_bucket_wav_audio_path']
                elif status == 'pending' or status == 'started':
                    return None
                else:
                    raise Exception("TTS request failed: " + status)
            else:
                raise Exception("Failed to check TTS status: " + result.get('error', 'Unknown error'))
        else:
            raise Exception(f"Failed to check TTS status with status code {response.status_code}")

    def download_audio(file_path):
        base_url = 'https://storage.googleapis.com/vocodes-public'
        download_url = f'{base_url}{file_path}'
        response = requests.get(download_url)
        download_directory = audio_dir
        file_name = os.path.join(download_directory, f'{count}_{character}.wav')
        if response.status_code == 200:
            with open(file_name, 'wb') as audio_file:
                audio_file.write(response.content)
            print(f"Audio downloaded successfully as {file_name}")
            return file_name
        else:
            raise Exception(f"Failed to download audio with status code {response.status_code}")

    def check_audio(file_name):
        audio = AudioSegment.from_wav(file_name)
        silence_threshold = -40
        silent_ranges = detect_silence(audio, min_silence_len=1000, silence_thresh=silence_threshold)
        if silent_ranges:
            start_silence, end_silence = silent_ranges[0][0], silent_ranges[-1][-1]
            if start_silence == 0 or end_silence >= len(audio) - 1000:
                return False
        return True

    def main():
        try:
            print("Requesting TTS...")
            job_token = request_tts(tts_model_token, text_to_speech)
            print(f"TTS requested. Job Token: {job_token}")

            audio_path = None
            while audio_path is None:
                print("Checking status...")
                time.sleep(10)
                audio_path = check_tts_status(job_token)

            print(f"Audio available at: {audio_path}")
            file_name = download_audio(audio_path)
            if not check_audio(file_name):
                print("Audio has too much silence, retrying TTS request...")
                main()
            else:
                print("Audio passed the silence check.")
        except Exception as e:
            print(f"An error occurred: {e}")
    if __name__ == "__main__":
        main()

# async def generate(text_file, script_dir):
#     print("Generating content for dialogue...")

#     file = os.path.join(script_dir, 'content.json')
#     with open(file, 'r') as f:
#         data = json.load(f)
#     category = random.choice(['Dad', 'Battery', 'Birthday', 'Name', 'Mom'])
#     print(f"Selected category: {category}")
#     text = data[category]
#     prompt = (
#         "I want you to act as a dynamic dialogue generator to create a humorous dialogue between SpongeBob and Patrick with occasional appearances by Sandy, Mr. Krabs, Plankton, or Squidward. that lasts at least 50 seconds. "
#         "The conversation should have 10 turns, with SpongeBob starting first, followed by Patrick and others. "
#         "Each character will guess something fun about the viewers, making it silly and engaging."
#         "You should generate new content each time, using this pattern: "
#         "For the first []: integrate it into a sentence like this example: '[IF I GUESS YOUR ----] YOU ARE ELIMINATED' as a title and (limit to 5 words in brackets). "
#         "For the second [], should end with the word 'edition' and use exactly one word (e.g., '[funny word] edition'). "
#         "For the remaining 3-12 [], only use exactly one word in the brackets. "
#         "Ensure the total bracketed count in text is exactly 12. "
#         "Make sure the dialogue is creative, funny, lighthearted, and corrected for grammar."
#         f"{text}"
#     )

#     client = Client("yuntian-deng/ChatGPT4Turbo")
#     try:
#         result = client.predict(
#             inputs=prompt,
#             top_p=1.0,
#             temperature=1.0,
#             chat_counter=0,
#             chatbot=[],
#             api_name="/predict"
#         )
#         response_message = result[0][0][1]
#         cleaned_response = re.sub(r'<.*?>|\*+', '', response_message)

#         characters = ['SpongeBob', 'Patrick', 'Sandy', 'Mr. Krabs', 'Plankton', 'Squidward']
#         for character in characters:
#             cleaned_response = re.sub(rf'({character}:)', rf'\n{character}:', cleaned_response)

#         brackets = re.findall(r'\[(.*?)\]', cleaned_response)
#         if len(brackets) != 12:
#             raise ValueError(f"Validation failed: Expected 12 brackets, but found {len(brackets)}. Aborting the process.")

#         with open(text_file, 'w', encoding='utf-8') as f:
#             f.write(cleaned_response)
#         print(f"Generated content: {cleaned_response}")
#         print(f"Generated content saved to {text_file}")

#     except Exception as e:
#         print(f"Failed to generate content: {e}")
#         raise

async def speech_to_text(text, audio_file, alignment_file):
    print(f"Running Azure Speech SDK for {audio_file}")
    speech_config = speechsdk.SpeechConfig(subscription="874b3eb84cf749fe90eb4443dd9ff0a5", region="eastasia")
    speech_config.output_format = speechsdk.OutputFormat.Detailed
    speech_config.request_word_level_timestamps()
    audio_config = speechsdk.AudioConfig(filename=audio_file)
    reference_text = text
    pronunciation_config = speechsdk.PronunciationAssessmentConfig(
        reference_text=reference_text,
        grading_system=speechsdk.PronunciationAssessmentGradingSystem.HundredMark,
        granularity=speechsdk.PronunciationAssessmentGranularity.Phoneme)
    speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, language='en-US', audio_config=audio_config)
    pronunciation_config.apply_to(speech_recognizer)
    done = False
    word_details = []
    def stop_cb(evt: speechsdk.SessionEventArgs):
        nonlocal done
        done = True
    def recognized(evt: speechsdk.SpeechRecognitionEventArgs):
        json_result = evt.result.properties.get(speechsdk.PropertyId.SpeechServiceResponse_JsonResult)
        try:
            jo = json.loads(json_result)
            nb = jo.get("NBest", [{}])[0]
            words = nb.get("Words", [])
            nonlocal word_details
            word_details.extend([
                (
                    w.get("Word", ""),
                    int(w.get("Offset", 0)),
                    int(w.get("Duration", 0))
                )
                for w in words
            ])
        except json.JSONDecodeError as e:
            print(f"JSON decode error: {e}")
        except Exception as e:
            print(f"Unexpected error: {e}")

    speech_recognizer.recognized.connect(recognized)
    speech_recognizer.session_stopped.connect(stop_cb)
    speech_recognizer.canceled.connect(stop_cb)
    speech_recognizer.start_continuous_recognition()
    while not done:
        time.sleep(.5)
    speech_recognizer.stop_continuous_recognition()
    with open(alignment_file, 'w') as f:
        json.dump(word_details, f)
    print(f"Word details saved to: {alignment_file}")

async def process_dialog(text, audio_dir):
    print("Processing dialogue and scraping audio")
    with open(text, 'r', encoding='utf-8') as f:
        text_content = f.read()
    brackets = re.findall(r'\[(.*?)\]', text_content)
    if len(brackets) != 12:
        raise ValueError(f"Validation failed: Expected 12 brackets, but found {len(brackets)}. Aborting the process.")
    count = 1
    lines = text_content.splitlines()
    for line in lines:
        print(f"Processing line: {line}")
        if "SpongeBob:" in line:
            character = "SpongeBob"
            url = 'weight_qdtx8h0mvn945md9tm2zaq422'
        elif "Patrick:" in line:
            character = "Patrick"
            url = 'weight_17enezbhvjmzxkcp87v46zv8n'
        elif "Sandy:" in line:
            character = "Sandy"
            url = 'weight_89asn9bw0xghm9be0ths6pmxq'
        elif "Squidward:" in line:
            character = "Squidward"
            url = 'weight_pdf4b4pknzq89d011cd95029e'
        elif "Mr. Krabs:" in line:
            character = "Mr. Krabs"
            url = 'weight_zsqq6mtdcr0c5n0shxcpy96tt'
        elif "Plankton:" in line:
            character = "Plankton"
            url = 'weight_vdpxnv3frpkk0mrpvs8jz62xa'
        else:
            continue
        dialog_text = line.split(": ", 1)[1].replace("[", "").replace("]", "").lower()
        await text_to_speech(dialog_text, character, url, count, audio_dir)
        count += 1

async def combine_audio(directory):
    print(f"Combining audio files in directory: {directory}")
    audio_files = sorted(
        [os.path.join(directory, file) for file in os.listdir(directory) if file.endswith(".wav")],
        key=lambda f: int(re.match(r'(\d+)_', os.path.basename(f)).group(1))
    )
    if not audio_files:
        print("No audio files found to combine")
        return
    combined_audio = concatenate_audioclips([AudioFileClip(file) for file in audio_files])
    combined_audio_file = os.path.join(directory, "combined_audio.wav")
    combined_audio.write_audiofile(combined_audio_file)
    print(f"Combined audio saved at: {combined_audio_file}")

async def resize(video_clip):
    print("Resizing video")
    target_width, target_height = 1080, 1920
    target_aspect_ratio = target_width / target_height
    video_aspect_ratio = video_clip.w / video_clip.h
    if video_aspect_ratio > target_aspect_ratio:
        new_width = int(target_aspect_ratio * video_clip.h)
        video_clip = video_clip.crop(x_center=video_clip.w // 2, width=new_width)
    elif video_aspect_ratio < target_aspect_ratio:
        new_height = int(video_clip.w / target_aspect_ratio)
        video_clip = video_clip.crop(y_center=video_clip.h // 2, height=new_height)
    return video_clip.resize((target_width, target_height))

async def designing(text, video, audio_file, output_file, alignment_file, spongebob_dir, patrick_dir, squid_dir, sandy_dir, plankton_dir, krabs_dir, sfx_file):
    print(f"Creating design for {audio_file}")
    print("Cleaning and preparing the text")
    characters = ['SpongeBob', 'Patrick', 'Sandy', 'Mr. Krabs', 'Plankton', 'Squidward']
    cleaned_text = re.sub(rf"({'|'.join(characters)}):", '', text)
    cleaned_text = re.sub(r'[^\w\s!\'?[\]]', '', cleaned_text)
    cleaned_text = re.sub(r'\[.*?\]', '', cleaned_text, count=2)

    await speech_to_text(cleaned_text, audio_file, alignment_file)
    print(f"Loading alignment data from {alignment_file}")
    with open(alignment_file, 'r') as f:
        alignment = json.load(f)

    print("Matching cleaned text with alignment data")
    text_words = cleaned_text.split()
    def normalize(word):
        return re.sub(r"^\W*|\W+$", '', word).lower()
    def align_words(text_words, alignment):
        normalized_text_words = [normalize(w) for w in text_words]
        alignment_words = [w[0] for w in alignment]
        normalized_alignment_words = [normalize(w) for w in alignment_words]
        matcher = difflib.SequenceMatcher(None, normalized_alignment_words, normalized_text_words)
        opcodes = matcher.get_opcodes()
        align_idx = 0
        matched_alignment = []
        for tag, i1, i2, j1, j2 in opcodes:
            if tag == 'equal':
                for idx in range(i1, i2):
                    text_word = text_words[j1 + (idx - i1)]
                    offset, duration = alignment[align_idx][1], alignment[align_idx][2]
                    matched_alignment.append([text_word, offset, duration])
                    align_idx += 1
            elif tag == 'replace':
                for idx in range(i1, i2):
                    text_word = text_words[j1 + (idx - i1)]
                    offset, duration = alignment[align_idx][1], alignment[align_idx][2]
                    matched_alignment.append([text_word, offset, duration])
                    align_idx += 1
            elif tag == 'insert':
                for idx in range(j1, j2):
                    text_word = text_words[idx]
                    prev_offset = matched_alignment[-1][1] + matched_alignment[-1][2] if matched_alignment else 0
                    next_offset = alignment[align_idx][1] if align_idx < len(alignment) else prev_offset + 1000000
                    estimated_duration = (next_offset - prev_offset) // (j2 - j1)
                    matched_alignment.append([text_word, prev_offset, estimated_duration])
                    prev_offset += estimated_duration
            elif tag == 'delete':
                align_idx += (i2 - i1)
        return matched_alignment
    matched_alignment = align_words(text_words, alignment)

    print("Handling bracketed words for combining text")
    combined_alignment = []
    combining = False
    combined_word = ""
    combined_offset = 0
    combined_duration = 0
    for word, offset, duration in matched_alignment:
        if '[' in word and ']' in word:
            combined_alignment.append((word, offset, duration))
        elif '[' in word:
            combining = True
            combined_word = word
            combined_offset = offset
            combined_duration = duration
        elif combining:
            combined_word += " " + word
            combined_duration += duration
            if ']' in word:
                combining = False
                combined_alignment.append((combined_word, combined_offset, combined_duration))
        else:
            combined_alignment.append((word, offset, duration))

    print("Preparing to display images based on text and alignment")
    clips = []
    sfx_clips = []
    font = 'Luckiest-Guy'
    bracket_index = 0
    video_duration = video.duration
    lines = text.strip().split('\n')
    dialogues = []
    for line in lines:
        if any(line.startswith(f"{character}:") for character in characters):
            character, dialogue = line.split(": ", 1)
            words = dialogue.split()[:2] if len(dialogue.split()) >= 2 else dialogue.split()
            dialogues.append((character, words))

    comb_words = [(word.lower().replace(',', '').replace('!', ''), offset, duration) for word, offset, duration in matched_alignment]
    dialogues = [(character.lower(), [w.replace('*', '').replace(',', '').replace('!', '').lower() for w in dialogue_words]) for character, dialogue_words in dialogues]
    SIMILARITY_THRESHOLD = 0.5
    found_offsets = []
    last_matched_offset = 0

    for character, words in dialogues:
        for i in range(len(comb_words) - 1):
            word_1 = comb_words[i][0]
            word_2 = comb_words[i + 1][0]
            offset_1 = comb_words[i][1]
            if offset_1 <= last_matched_offset:
                continue
            match_first_word = difflib.SequenceMatcher(None, word_1, words[0]).ratio() > SIMILARITY_THRESHOLD
            match_second_word = difflib.SequenceMatcher(None, word_2, words[1]).ratio() > SIMILARITY_THRESHOLD
            if match_first_word and match_second_word:
                found_offsets.append((character, comb_words[i][1]))
                last_matched_offset = comb_words[i][1]
                break

    def load_image(image_path, target_size=(800, 800), resample=Image.LANCZOS):
        try:
            print(f"Loading image from: {image_path}")
            img = Image.open(image_path)
            print(f"Image mode: {img.mode}, Image size: {img.size}")
            if img.mode != 'RGBA':
                img = img.convert('RGBA')
            if target_size is not None:
                original_size = img.size
                original_ratio = original_size[0] / original_size[1]
                target_ratio = target_size[0] / target_size[1]
                if original_ratio > target_ratio:
                    new_width = target_size[0]
                    new_height = int(new_width / original_ratio)
                else:
                    new_height = target_size[1]
                    new_width = int(new_height * original_ratio)
                img = img.resize((new_width, new_height), resample=resample)
                print(f"Resized image to: {target_size}")
            img_np = np.array(img)
            return img_np
        except Exception as e:
            print(f"An error occurred while loading the image: {e}")
            return None

    print(f"Adding Image")
    character_dirs = {
        "spongebob": spongebob_dir,
        "patrick": patrick_dir,
        "sandy": sandy_dir,
        "squidward": squid_dir,
        "mr. krabs": krabs_dir,
        "plankton": plankton_dir
    }

    for i in range(len(found_offsets)):
        character, start_offset = found_offsets[i]
        start_time = start_offset / 1e7
        if i + 1 < len(found_offsets):
            next_offset = found_offsets[i + 1][1]
        else:
            next_offset = video.duration * 1e7

        duration = (next_offset - start_offset) / 1e7

        image_dir = character_dirs[character.lower()]
        image_path = os.path.join(image_dir, random.choice(os.listdir(image_dir)))
        print(f"Adding {character} image from {start_time:.2f} to {start_time + duration:.2f}")
        image_path = load_image(image_path)
        image_clip = (
            ImageClip(image_path, transparent=True)
            .with_start(start_time)
            .with_duration(duration)
            .with_position(('center', 1200))
            .resize(lambda t:
                1.5 + (0.8 - 1.5) * (t / 0.05) if t < 0.05 else
                0.8 + (1.2 - 0.8) * ((t - 0.05) / 0.05) if t < 0.1 else
                1.2 + (0.9 - 1.2) * ((t - 0.1) / 0.05) if t < 0.15 else
                0.9 + (1.0 - 0.9) * ((t - 0.15) / 0.05) if t < 0.2 else
                1.0 if t < (duration - 0.3) else
                (1.0 + (0.1 - 1.0) * ((t - (duration - 0.3)) / 0.3)) if t < duration else
                0.1
            )
            .rotate(lambda t: 5 * math.sin(2 * math.pi * t * 0.3))
        )
        clips.append(image_clip)
        sound_effect = AudioFileClip(sfx_file).with_start(start_time)
        sfx_clips.append(sound_effect)

    matches = re.findall(r'\[(.*?)\]', text)
    text1 = matches[0]
    text2 = matches[1]

    def color_hunt():
      def is_bright(hex_color):
        hex_color = hex_color.lstrip('#')
        rgb = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
        luminance = 0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]
        r, g, b = [x / 255.0 for x in rgb]
        h, s, v = colorsys.rgb_to_hsv(r, g, b)
        return luminance >= 130 and s >= 0.3
      print(f"Selecting Colors")
      while True:
          url = 'https://colorhunt.co/php/feed.php'
          headers = {
              'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
          }
          data = {
              'step': 0,
              'sort': 'random',
              'tags': ''
          }
          response = requests.post(url, headers=headers, data=data)
          if response.status_code == 200:
              palettes = response.json()
              color_pairs = []
              for palette in palettes:
                  code = palette['code']
                  hex_colors = [f'#{code[i:i+6].upper()}' for i in range(0, len(code), 6)]
                  if len(hex_colors) == 4 and all(is_bright(hex_color) for hex_color in hex_colors):
                      color_pairs.append(tuple(hex_colors))
              if color_pairs:
                  selected_color_pair = random.choice(color_pairs)
                  print(f"Selected Color Pair: {selected_color_pair}")
                  return selected_color_pair
                  break
          else:
            raise Exception(f"Failed to retrieve data. Status code: {response.status_code}")

    selected_colors = color_hunt()
    color1, color2, color3, color4 = selected_colors
    print(f"Adding Top Text")
    max_text_width = 1080 * 0.9
    top_text = (
        TextClip(f"{text1}, YOU'ARE ELIMINATED", font_size=90, color=color1, font=font, stroke_color='black', stroke_width=6, align='center', method='caption', size=(max_text_width, None))
        .with_duration(video_duration)
        .with_position(('center', 100))
    )
    clips.append(top_text)

    top_text = (
        TextClip(text2, font_size=90, color=color2, font=font, stroke_color='black', stroke_width=6, align='center')
        .with_duration(video_duration)
        .with_position(('center', 400))
    )
    clips.append(top_text)

    print(f"Adding Number")
    positions = [
        (150, 510), (150, 610), (150, 710), (150, 810), (150, 910),
        (620, 510), (620, 610), (620, 710), (620, 810), (620, 910)
    ]

    for i, position in enumerate(positions):
      number_clip = (
          TextClip(f"{i + 1}.", font_size=65, color=color3, font=font, stroke_color='black', method='caption', stroke_width=5)
          .with_duration(video_duration - i)
          .with_position((position[0]-80, position[1]))
          .with_start(i)
          .resize(lambda t:
              1.5 + (0.8 - 1.5) * (t / 0.05) if t < 0.05 else
              0.8 + (1.2 - 0.8) * ((t - 0.05) / 0.05) if t < 0.1 else
              1.2 + (0.9 - 1.2) * ((t - 0.1) / 0.05) if t < 0.15 else
              0.9 + (1.0 - 0.9) * ((t - 0.15) / 0.05) if t < 0.2 else
              1.0
          )
      )
      clips.append(number_clip)

    print(f"Adding Caption")
    bracketed_words = set()
    for word, offset, duration in combined_alignment:
        offset_sec = offset / 1e7
        duration_sec = duration / 1e7
        is_bracketed = '[' in word and ']' in word
        color = color4 if is_bracketed else 'white'
        word = word.replace('[', '').replace(']', '') if is_bracketed else word
        if is_bracketed and word in bracketed_words:
          continue
        subtitle_clip = (
            TextClip(word, font_size=75, color=color, font=font, stroke_color='black', method='caption', stroke_width=6)
            .with_duration(duration_sec)
            .with_position(('center', 1100))
            .with_start(offset_sec)
            .resize(lambda t:
                0.6 + (1.2 - 0.6) * (t / 0.05) if t < 0.05 else
                1.2 + (0.9 - 1.2) * ((t - 0.05) / 0.05) if t < 0.1 else
                0.9 + (1.1 - 0.9) * ((t - 0.1) / 0.05) if t < 0.15 else
                1.1 + (1.0 - 1.1) * ((t - 0.15) / 0.05) if t < 0.2 else
                1.0
            )
        )
        clips.append(subtitle_clip)
        month_abbr = {
            "January": "Jan",
            "February": "Feb",
            "March": "Mar",
            "April": "Apr",
            "May": "May",
            "June": "Jun",
            "July": "Jul",
            "August": "Aug",
            "September": "Sep",
            "October": "Oct",
            "November": "Nov",
            "December": "Dec"
        }
        def replace_month_abbr(text):
            for full_month, abbr in month_abbr.items():
                if full_month in text:
                    return text.replace(full_month, abbr)
            return text
        if is_bracketed:
            word = replace_month_abbr(word)
            bracketed_words.add(word)
            temp_clip = TextClip(word, font_size=65, font=font)
            text_width, _ = temp_clip.size
            max_column_width = 385
            if text_width > max_column_width:
                scale_factor = max_column_width / text_width
                new_font_size = int(65 * scale_factor)
                print(f"Resizing text '{word}' to fit within column. New font size: {new_font_size}")
            else:
                new_font_size = 65
            position = positions[bracket_index % len(positions)]
            bracket_index += 1
            print(f"Creating bracketed word clip for '{word}' at position {position}")
            bracket_word_clip = (
                TextClip(word, font_size=new_font_size, color='white', font=font, stroke_color='black', stroke_width=5, align='West', method='caption', size=(max_text_width, None))
                .with_duration(video_duration - offset_sec)
                .with_position(position)
                .with_start(offset_sec)
                .resize(lambda t:
                    1.5 + (0.8 - 1.5) * (t / 0.05) if t < 0.05 else
                    0.8 + (1.2 - 0.8) * ((t - 0.05) / 0.05) if t < 0.1 else
                    1.2 + (0.9 - 1.2) * ((t - 0.1) / 0.05) if t < 0.15 else
                    0.9 + (1.0 - 0.9) * ((t - 0.15) / 0.05) if t < 0.2 else
                    1.0
                )
            )
            clips.append(bracket_word_clip)
    audio = video.audio
    final_audio = CompositeAudioClip([audio] + sfx_clips)
    print(f"Writing final video to {output_file}")
    video = CompositeVideoClip([video] + clips)
    video.audio = final_audio
    video.write_videofile(output_file, codec="libx264", fps=60)
    print(f"Video with captions saved at: {output_file}")

async def process_video(video_file, audio_dir, text_file, output_video, alignment_file, spongebob_dir, patrick_dir, squid_dir, sandy_dir, plankton_dir, krabs_dir, sfx_file):
    print("Processing video with combined audio")
    with open(text_file, 'r', encoding='utf-8') as f:
        text = f.read()
    combined_audio_file = os.path.join(audio_dir, "combined_audio.wav")
    video = VideoFileClip(video_file)
    audio = AudioFileClip(combined_audio_file)
    video_duration = video.duration
    audio_duration = audio.duration
    start_time = random.uniform(0, video_duration - audio_duration)
    end_time = start_time + audio_duration
    video_clip = video.subclip(start_time, end_time)
    video_clip = await resize(video_clip)
    video_clip = video_clip.with_audio(audio)
    await designing(text, video_clip, combined_audio_file, output_video, alignment_file, spongebob_dir, patrick_dir, squid_dir, sandy_dir, plankton_dir, krabs_dir, sfx_file)

print("Function Added.")

script_dir = '/content/drive/MyDrive/chubbShort'
audio_dir = os.path.join(script_dir, 'audio')
char_dir = os.path.join(script_dir, 'char')
spongebob_dir = os.path.join(char_dir, 'spongebob')
patrick_dir = os.path.join(char_dir, 'patrick')
squid_dir = os.path.join(char_dir, 'squid')
sandy_dir = os.path.join(char_dir, 'sandy')
plankton_dir = os.path.join(char_dir, 'plankton')
krabs_dir = os.path.join(char_dir, 'krabs')
output_dir = os.path.join(script_dir, 'output')

video_file = os.path.join(script_dir, 'input1.mp4')
text_file = os.path.join(script_dir, 'generated_text.txt')
sfx_file = os.path.join(script_dir, 'whoosh.mp3')
alignment_file = os.path.join(script_dir, 'alignment.json')

if not os.path.exists(output_dir):
    os.makedirs(output_dir)
async def main():
    while True:
      print("Starting main process")
      timestamp = time.strftime("%Y%m%d-%H%M%S")
      output_video = os.path.join(output_dir, f"output_{timestamp}.mp4")

      if os.path.exists(audio_dir):
          for file in os.listdir(audio_dir):
              file_path = os.path.join(audio_dir, file)
              if os.path.isfile(file_path):
                  os.remove(file_path)
          print(f"Audio directory cleaned: {audio_dir}")
      else:
          os.makedirs(audio_dir)
      try:
          # await generate(text_file, script_dir)
          await process_dialog(text_file, audio_dir)
          await combine_audio(audio_dir)
          await process_video(video_file, audio_dir, text_file, output_video, alignment_file, spongebob_dir, patrick_dir, squid_dir, sandy_dir, plankton_dir, krabs_dir, sfx_file)
          print("Main process completed successfully")
          break
      except ValueError as ve:
          print(f"Validation error: {ve}. Restarting the process.")
          continue
      except Exception as e:
          print(f"An error occurred: {e}")
          time.sleep(5)

try:
    await main()
except KeyboardInterrupt:
    print("Script stopped by user. Exiting gracefully.")