Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,50 +1,47 @@
|
|
1 |
import numpy as np
|
2 |
from pydub import AudioSegment
|
3 |
|
4 |
-
def
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
audio_segment = AudioSegment(
|
10 |
-
(
|
11 |
frame_rate=sample_rate,
|
12 |
-
sample_width=2,
|
13 |
-
channels=1
|
14 |
)
|
15 |
-
return audio_segment
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
# Define frequencies for notes
|
29 |
-
frequencies = {
|
30 |
-
"C4": 261.63,
|
31 |
-
"D4": 293.66,
|
32 |
-
"E4": 329.63,
|
33 |
-
"F4": 349.23,
|
34 |
-
"G4": 392.00,
|
35 |
-
"A4": 440.00,
|
36 |
-
"C5": 523.25
|
37 |
-
}
|
38 |
-
|
39 |
-
for note in selected_notes:
|
40 |
-
frequency = frequencies[note]
|
41 |
-
sine = sine_wave(frequency, duration_ms=500) # 0.5 second for each note
|
42 |
-
music += sine
|
43 |
-
|
44 |
-
# Export the generated music
|
45 |
-
output_file = f"{theme}_kids_music.wav"
|
46 |
-
music.export(output_file, format="wav")
|
47 |
-
return output_file
|
48 |
|
49 |
|
50 |
|
@@ -65,36 +62,54 @@ def script_to_audio(script):
|
|
65 |
tts.save(audio_file)
|
66 |
return audio_file
|
67 |
|
68 |
-
import
|
69 |
|
70 |
-
def
|
71 |
-
#
|
72 |
-
|
73 |
-
|
74 |
-
# Generate comedy script
|
75 |
-
comedy_script = generate_comedy_script(prompt)
|
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 |
|
|
|
|
1 |
import numpy as np
|
2 |
from pydub import AudioSegment
|
3 |
|
4 |
+
def generate_kids_music(theme):
|
5 |
+
# Define basic parameters
|
6 |
+
duration = 500 # milliseconds for each note
|
7 |
+
sample_rate = 44100 # Sample rate for the audio
|
8 |
+
|
9 |
+
# Define a simple scale of frequencies for the melody
|
10 |
+
frequencies = [261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88] # C4, D4, E4, F4, G4, A4, B4
|
11 |
+
melody = []
|
12 |
+
|
13 |
+
# Create a simple repetitive melody pattern
|
14 |
+
pattern = [0, 1, 2, 3, 4, 0] # Indices corresponding to the frequencies above
|
15 |
+
|
16 |
+
for note in pattern:
|
17 |
+
# Generate the sine wave for the note
|
18 |
+
t = np.linspace(0, duration / 1000.0, int(sample_rate * duration / 1000.0), False)
|
19 |
+
sine_wave = 0.5 * np.sin(2 * np.pi * frequencies[note] * t)
|
20 |
+
|
21 |
+
# Append to melody
|
22 |
+
melody.append(sine_wave)
|
23 |
+
|
24 |
+
# Concatenate all notes into a single array
|
25 |
+
melody = np.concatenate(melody)
|
26 |
+
|
27 |
+
# Convert to an AudioSegment
|
28 |
audio_segment = AudioSegment(
|
29 |
+
(melody * 32767).astype(np.int16),
|
30 |
frame_rate=sample_rate,
|
31 |
+
sample_width=2,
|
32 |
+
channels=1
|
33 |
)
|
|
|
34 |
|
35 |
+
# Save the audio to a file
|
36 |
+
audio_file = "kids_music.wav"
|
37 |
+
audio_segment.export(audio_file, format="wav")
|
38 |
+
|
39 |
+
# Create simple lyrics
|
40 |
+
lyrics = f"{theme} shark, doo doo doo doo doo doo\n" * 3 + \
|
41 |
+
f"{theme} shark, doo doo doo doo doo doo\n" + \
|
42 |
+
"It's time to play!\n" * 2
|
43 |
+
|
44 |
+
return audio_file, lyrics
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
|
47 |
|
|
|
62 |
tts.save(audio_file)
|
63 |
return audio_file
|
64 |
|
65 |
+
from moviepy.editor import TextClip, AudioFileClip, CompositeVideoClip
|
66 |
|
67 |
+
def create_animated_video(script, audio_file):
|
68 |
+
# Create text clips for each sentence in the script
|
69 |
+
sentences = script.split('. ')
|
70 |
+
clips = []
|
|
|
|
|
71 |
|
72 |
+
# Create an animation for each sentence
|
73 |
+
for i, sentence in enumerate(sentences):
|
74 |
+
text_clip = TextClip(sentence, fontsize=40, color='white', bg_color='black', size=(640, 480), print_cmd=True)
|
75 |
+
text_clip = text_clip.set_duration(3).set_position('center').set_start(i * 3) # Each sentence appears for 3 seconds
|
76 |
+
clips.append(text_clip)
|
77 |
+
|
78 |
+
# Load the audio file
|
79 |
+
audio_clip = AudioFileClip(audio_file)
|
80 |
+
|
81 |
+
# Set the duration of the video based on the audio
|
82 |
+
total_duration = audio_clip.duration
|
83 |
+
final_video = CompositeVideoClip(clips)
|
84 |
+
final_video = final_video.set_duration(total_duration).set_audio(audio_clip)
|
85 |
+
|
86 |
+
# Write the result to a file
|
87 |
+
video_file = "comedy_animation.mp4"
|
88 |
+
final_video.write_videofile(video_file, fps=24)
|
89 |
+
return video_file
|
90 |
+
|
91 |
+
import gradio as gr
|
92 |
|
93 |
+
import gradio as gr
|
94 |
+
|
95 |
+
def generate_music_and_comedy(theme, script):
|
96 |
+
# Generate kids music and lyrics
|
97 |
+
music_file, lyrics = generate_kids_music(theme)
|
98 |
+
|
99 |
+
# Generate audio from the comedic script
|
100 |
+
audio_file = generate_audio_from_script(script)
|
101 |
+
|
102 |
+
# Create an animated video from the comedic script and audio
|
103 |
+
video_file = create_animated_video(script, audio_file)
|
104 |
+
|
105 |
+
return video_file, music_file, lyrics
|
106 |
+
|
107 |
+
interface = gr.Interface(
|
108 |
+
fn=generate_music_and_comedy,
|
109 |
+
inputs=["text", "text"],
|
110 |
+
outputs=["video", "audio", "text"],
|
111 |
+
title="AI Music and Comedy Animation Generation",
|
112 |
+
description="Generate unique music tracks for kids and animated comedic videos."
|
113 |
+
)
|
114 |
|
115 |
+
interface.launch()
|