File size: 3,684 Bytes
c4494a9
0d07348
 
24cc23d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4494a9
24cc23d
c4494a9
24cc23d
 
c4494a9
 
24cc23d
 
 
 
 
 
 
 
 
 
0d07348
c4494a9
e38508f
0d07348
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24cc23d
0d07348
24cc23d
 
 
 
0d07348
24cc23d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d07348
24cc23d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d07348
24cc23d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import numpy as np
from pydub import AudioSegment

def generate_kids_music(theme):
    # Define basic parameters
    duration = 500  # milliseconds for each note
    sample_rate = 44100  # Sample rate for the audio

    # Define a simple scale of frequencies for the melody
    frequencies = [261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88]  # C4, D4, E4, F4, G4, A4, B4
    melody = []

    # Create a simple repetitive melody pattern
    pattern = [0, 1, 2, 3, 4, 0]  # Indices corresponding to the frequencies above

    for note in pattern:
        # Generate the sine wave for the note
        t = np.linspace(0, duration / 1000.0, int(sample_rate * duration / 1000.0), False)
        sine_wave = 0.5 * np.sin(2 * np.pi * frequencies[note] * t)
        
        # Append to melody
        melody.append(sine_wave)

    # Concatenate all notes into a single array
    melody = np.concatenate(melody)

    # Convert to an AudioSegment
    audio_segment = AudioSegment(
        (melody * 32767).astype(np.int16), 
        frame_rate=sample_rate,
        sample_width=2,
        channels=1
    )

    # Save the audio to a file
    audio_file = "kids_music.wav"
    audio_segment.export(audio_file, format="wav")

    # Create simple lyrics
    lyrics = f"{theme} shark, doo doo doo doo doo doo\n" * 3 + \
             f"{theme} shark, doo doo doo doo doo doo\n" + \
             "It's time to play!\n" * 2

    return audio_file, lyrics



from transformers import pipeline

# Load the GPT-2 model for text generation
comedy_generator = pipeline('text-generation', model='gpt2')

def generate_comedy_script(prompt):
    result = comedy_generator(prompt, max_length=150, num_return_sequences=1)
    return result[0]['generated_text']

from gtts import gTTS

def script_to_audio(script):
    tts = gTTS(script, lang='en')
    audio_file = "comedy_script.mp3"
    tts.save(audio_file)
    return audio_file

from moviepy.editor import TextClip, AudioFileClip, CompositeVideoClip

def create_animated_video(script, audio_file):
    # Create text clips for each sentence in the script
    sentences = script.split('. ')
    clips = []
    
    # Create an animation for each sentence
    for i, sentence in enumerate(sentences):
        text_clip = TextClip(sentence, fontsize=40, color='white', bg_color='black', size=(640, 480), print_cmd=True)
        text_clip = text_clip.set_duration(3).set_position('center').set_start(i * 3)  # Each sentence appears for 3 seconds
        clips.append(text_clip)

    # Load the audio file
    audio_clip = AudioFileClip(audio_file)

    # Set the duration of the video based on the audio
    total_duration = audio_clip.duration
    final_video = CompositeVideoClip(clips)
    final_video = final_video.set_duration(total_duration).set_audio(audio_clip)

    # Write the result to a file
    video_file = "comedy_animation.mp4"
    final_video.write_videofile(video_file, fps=24)
    return video_file

import gradio as gr

import gradio as gr

def generate_music_and_comedy(theme, script):
    # Generate kids music and lyrics
    music_file, lyrics = generate_kids_music(theme)

    # Generate audio from the comedic script
    audio_file = generate_audio_from_script(script)

    # Create an animated video from the comedic script and audio
    video_file = create_animated_video(script, audio_file)

    return video_file, music_file, lyrics

interface = gr.Interface(
    fn=generate_music_and_comedy,
    inputs=["text", "text"],
    outputs=["video", "audio", "text"],
    title="AI Music and Comedy Animation Generation",
    description="Generate unique music tracks for kids and animated comedic videos."
)

interface.launch()