Manasa1 commited on
Commit
5b0d83d
·
verified ·
1 Parent(s): c0b37ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -108
app.py CHANGED
@@ -1,115 +1,81 @@
 
 
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
-
48
- from transformers import pipeline
49
-
50
- # Load the GPT-2 model for text generation
51
- comedy_generator = pipeline('text-generation', model='gpt2')
52
-
53
- def generate_comedy_script(prompt):
54
- result = comedy_generator(prompt, max_length=150, num_return_sequences=1)
55
- return result[0]['generated_text']
56
-
57
- from gtts import gTTS
58
-
59
- def script_to_audio(script):
60
- tts = gTTS(script, lang='en')
61
- audio_file = "comedy_script.mp3"
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
- def generate_music_and_comedy(theme, prompt):
94
- # Generate kids music and lyrics
95
- music_file, lyrics = generate_kids_music(theme)
96
-
97
- script = generate_comedy_script(prompt)
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()
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
  import numpy as np
4
+ from magenta.music import melodies, midi_io
5
+ from moviepy.editor import ImageSequenceClip, AudioFileClip
6
+ from transformers import GPT2LMHeadModel, GPT2Tokenizer
7
+ from PIL import Image
8
+ import requests
9
+ import torch
10
+
11
+ # Load pre-trained GPT-Neo model from Hugging Face
12
+ model_name = "EleutherAI/gpt-neo-125M" # Open-source GPT-Neo
13
+ tokenizer = GPT2Tokenizer.from_pretrained(model_name)
14
+ model = GPT2LMHeadModel.from_pretrained(model_name)
15
+
16
+ # Function to generate kids' music using Magenta's MelodyGenerator
17
  def generate_kids_music(theme):
18
+ melody = melodies.MelodyGenerator() # Create melody generator instance
19
+ generated_melody = melody.generate() # Generate a random melody
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
+ # Save the generated melody to a MIDI file
22
+ midi_file_path = 'output_music.mid'
23
+ midi_io.sequence_proto_to_midi_file(generated_melody, midi_file_path)
24
+
25
+ return midi_file_path
26
+
27
+ # Function to generate a comedic script
28
+ def generate_script(theme):
29
+ prompt = f"Create a funny script for kids based on the theme: {theme}."
30
+ inputs = tokenizer.encode(prompt, return_tensors="pt")
31
+ outputs = model.generate(inputs, max_length=150, num_return_sequences=1)
32
+ script = tokenizer.decode(outputs[0], skip_special_tokens=True)
33
+ return script
34
+
35
+ # Function to generate image frames based on script
36
+ def generate_frames_from_script(script):
37
+ frames = []
38
+ for line in script.split('.'):
39
+ line = line.strip()
40
+ if line:
41
+ image = generate_image(line) # Placeholder image generation
42
+ frames.append(image)
43
+ return frames
44
+
45
+ # Function to generate placeholder images (replace with DALL-E Mini if needed)
46
+ def generate_image(text):
47
+ image = Image.new('RGB', (1024, 1024), color=(255, 255, 255)) # Blank image
48
+ return image
49
+
50
+ # Create a video based on the generated frames
51
+ def create_video_from_frames(frames):
52
+ images = []
53
+ for frame in frames:
54
+ images.append(frame) # Placeholder images
55
+ video_file_path = 'output_video.mp4'
56
+ clip = ImageSequenceClip(images, fps=24)
57
+ clip.write_videofile(video_file_path, codec='libx264')
58
+ return video_file_path
59
+
60
+ # Main function to handle both music and comedy generation
61
+ def generate_music_and_comedy(theme):
62
+ script = generate_script(theme) # Generate a comedic script
63
+ music_file = generate_kids_music(theme) # Generate kid-friendly music
64
+ frames = generate_frames_from_script(script) # Generate frames from script
65
+ video_file = create_video_from_frames(frames) # Create video from frames
66
+
67
+ return video_file, music_file
68
+
69
+ # Gradio UI setup
70
  interface = gr.Interface(
71
  fn=generate_music_and_comedy,
72
+ inputs=gr.Textbox(label="Theme for Kids Music & Comedy"),
73
+ outputs=[gr.Video(label="Generated Animated Video"), gr.File(label="Generated Music")],
74
+ title="AI Kids Music & Comedy Generator",
75
+ description="Generate unique music and animated videos for kids based on a theme."
76
  )
77
 
78
+ # Launch the interface
79
+ if __name__ == "__main__":
80
+ interface.launch()
81
+