Manasa1 commited on
Commit
24cc23d
·
verified ·
1 Parent(s): e38508f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -69
app.py CHANGED
@@ -1,50 +1,47 @@
1
  import numpy as np
2
  from pydub import AudioSegment
3
 
4
- def sine_wave(frequency, duration_ms):
5
- """Generate a sine wave tone at a given frequency and duration."""
6
- sample_rate = 44100 # Sample rate in Hz
7
- t = np.linspace(0, duration_ms / 1000, int(sample_rate * (duration_ms / 1000)), endpoint=False)
8
- wave = 0.5 * np.sin(2 * np.pi * frequency * t) # Sine wave formula
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  audio_segment = AudioSegment(
10
- (wave * 32767).astype(np.int16).tobytes(), # Convert to 16-bit PCM
11
  frame_rate=sample_rate,
12
- sample_width=2, # 16 bits
13
- channels=1 # Mono audio
14
  )
15
- return audio_segment
16
 
17
- def generate_kids_music(theme):
18
- # Create a simple melody based on themes
19
- notes = {
20
- "birthday": ["C4", "E4", "G4", "C5"],
21
- "ocean": ["C4", "D4", "E4", "F4"],
22
- "adventure": ["E4", "G4", "A4", "C5"]
23
- }
24
-
25
- selected_notes = notes.get(theme, ["C4", "E4", "G4"]) # Default notes if theme not found
26
- music = AudioSegment.silent(duration=0) # Start with silence
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 gradio as gr
69
 
70
- def generate_music_and_comedy(theme, prompt):
71
- # Generate music
72
- music_file = generate_kids_music(theme)
73
-
74
- # Generate comedy script
75
- comedy_script = generate_comedy_script(prompt)
76
 
77
- # Convert the comedy script to audio
78
- audio_file = script_to_audio(comedy_script)
79
-
80
- return music_file, comedy_script, audio_file
81
-
82
- # Gradio Interface
83
- iface = gr.Interface(
84
- fn=generate_music_and_comedy,
85
- inputs=[
86
- gr.Dropdown(choices=["birthday", "ocean", "adventure"], label="Select Theme for Kids' Music"),
87
- gr.Textbox(lines=2, placeholder="Enter a prompt for comedy...", label="Comedy Prompt")
88
- ],
89
- outputs=[
90
- gr.Audio(label="Generated Kids' Music"),
91
- gr.Textbox(label="Generated Comedy Script"),
92
- gr.Audio(label="Comedy Audio")
93
- ],
94
- title="AI Music and Comedy Generator",
95
- description="Generate unique music for kids based on themes and comedic scripts from your prompts."
96
- )
97
 
98
- # Launch the app
99
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()