Spaces:
Runtime error
Runtime error
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() | |