Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -7,32 +7,27 @@ import scipy.io.wavfile
|
|
7 |
from TTS.api import TTS # Coqui TTS (open source)
|
8 |
from moviepy.editor import CompositeVideoClip, ImageClip, AudioFileClip, concatenate_videoclips
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
script_generator = pipeline("text-generation", model="gpt2", truncation=True, max_length=100)
|
13 |
|
14 |
# Use Stable Diffusion (open-source) for image generation
|
15 |
image_generator = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base", torch_dtype=torch.float16).to("cpu")
|
16 |
|
17 |
-
# Use MusicGen (open-source) for music generation
|
18 |
music_generator = pipeline("text-to-audio", model="facebook/musicgen-small", device="cpu")
|
19 |
|
20 |
# Use Coqui TTS (open-source) for text-to-speech
|
21 |
tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False, gpu=False)
|
22 |
|
23 |
-
|
24 |
-
# Generate Comedy Script
|
25 |
def generate_comedy_script(prompt):
|
26 |
script = script_generator(prompt)[0]['generated_text']
|
27 |
return script
|
28 |
|
29 |
-
# Convert Text to Speech using Coqui TTS
|
30 |
def text_to_speech(script):
|
31 |
output_audio = 'output.wav'
|
32 |
tts.tts_to_file(text=script, file_path=output_audio)
|
33 |
return output_audio
|
34 |
|
35 |
-
# Create Images Using Stable Diffusion
|
36 |
def create_images_from_script(script):
|
37 |
lines = script.split('. ')
|
38 |
image_paths = []
|
@@ -43,7 +38,6 @@ def create_images_from_script(script):
|
|
43 |
image_paths.append(img_path)
|
44 |
return image_paths
|
45 |
|
46 |
-
# Generate Fun Music Track using MusicGen
|
47 |
def generate_fun_music(prompt, output_music_file="fun_music.wav"):
|
48 |
# Generate music based on the prompt using MusicGen
|
49 |
response = music_generator(prompt)
|
@@ -57,19 +51,16 @@ def generate_fun_music(prompt, output_music_file="fun_music.wav"):
|
|
57 |
|
58 |
return output_music_file
|
59 |
|
60 |
-
# Create Video from Generated Images
|
61 |
def generate_text_video(script):
|
62 |
image_paths = create_images_from_script(script)
|
63 |
clips = []
|
64 |
for img_path in image_paths:
|
65 |
image_clip = ImageClip(img_path).set_duration(3).set_position(('center', 'center'))
|
66 |
clips.append(image_clip)
|
67 |
-
|
68 |
final_video = concatenate_videoclips(clips, method="compose")
|
69 |
final_video.write_videofile("/tmp/final_video.mp4", fps=24)
|
70 |
return "/tmp/final_video.mp4"
|
71 |
|
72 |
-
# Combine Audio and Video
|
73 |
def combine_audio_video(video_path, audio_path):
|
74 |
video = VideoFileClip(video_path)
|
75 |
audio = AudioFileClip(audio_path)
|
@@ -77,7 +68,6 @@ def combine_audio_video(video_path, audio_path):
|
|
77 |
final_video.write_videofile("/tmp/final_comedy_video.mp4", fps=24)
|
78 |
return "/tmp/final_comedy_video.mp4"
|
79 |
|
80 |
-
# Main Function to Generate Comedy Animation
|
81 |
def generate_comedy_and_animation(prompt):
|
82 |
script = generate_comedy_script(prompt)
|
83 |
audio_file = text_to_speech(script)
|
@@ -86,8 +76,7 @@ def generate_comedy_and_animation(prompt):
|
|
86 |
final_video = combine_audio_video(video_file, fun_music)
|
87 |
return script, audio_file, final_video
|
88 |
|
89 |
-
|
90 |
-
def generate_kids_animation_with_music(theme, output_video_file="kids_animation.mp4"):
|
91 |
music_file = generate_fun_music(theme, output_music_file="kids_music.wav")
|
92 |
clips = []
|
93 |
for i in range(5):
|
@@ -98,18 +87,11 @@ def generate_kids_animation_with_music(theme, output_video_file="kids_animation.
|
|
98 |
frame_path = f'/tmp/kids_temp_{i}.png'
|
99 |
img.save(frame_path)
|
100 |
clips.append(ImageClip(frame_path).set_duration(1).set_position(('center', 'center')))
|
101 |
-
|
102 |
final_video = CompositeVideoClip(clips, size=(800, 400))
|
103 |
final_video = final_video.set_audio(AudioFileClip(music_file))
|
104 |
-
final_video.write_videofile(
|
105 |
-
return music_file,
|
106 |
-
|
107 |
-
# Main Function to Generate Kids Content
|
108 |
-
def generate_kids_content(theme):
|
109 |
-
music_file, video_file = generate_kids_animation_with_music(theme)
|
110 |
-
return music_file, video_file
|
111 |
|
112 |
-
# Gradio Interface
|
113 |
with gr.Blocks() as app:
|
114 |
gr.Markdown("## AI Comedy and Kids Content Generator")
|
115 |
|
|
|
7 |
from TTS.api import TTS # Coqui TTS (open source)
|
8 |
from moviepy.editor import CompositeVideoClip, ImageClip, AudioFileClip, concatenate_videoclips
|
9 |
|
10 |
+
# Use GPT-2 Medium (lighter version) for text generation
|
11 |
+
script_generator = pipeline("text-generation", model="gpt2-medium", truncation=True, max_length=100)
|
|
|
12 |
|
13 |
# Use Stable Diffusion (open-source) for image generation
|
14 |
image_generator = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base", torch_dtype=torch.float16).to("cpu")
|
15 |
|
16 |
+
# Use MusicGen Small (open-source) for music generation
|
17 |
music_generator = pipeline("text-to-audio", model="facebook/musicgen-small", device="cpu")
|
18 |
|
19 |
# Use Coqui TTS (open-source) for text-to-speech
|
20 |
tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False, gpu=False)
|
21 |
|
|
|
|
|
22 |
def generate_comedy_script(prompt):
|
23 |
script = script_generator(prompt)[0]['generated_text']
|
24 |
return script
|
25 |
|
|
|
26 |
def text_to_speech(script):
|
27 |
output_audio = 'output.wav'
|
28 |
tts.tts_to_file(text=script, file_path=output_audio)
|
29 |
return output_audio
|
30 |
|
|
|
31 |
def create_images_from_script(script):
|
32 |
lines = script.split('. ')
|
33 |
image_paths = []
|
|
|
38 |
image_paths.append(img_path)
|
39 |
return image_paths
|
40 |
|
|
|
41 |
def generate_fun_music(prompt, output_music_file="fun_music.wav"):
|
42 |
# Generate music based on the prompt using MusicGen
|
43 |
response = music_generator(prompt)
|
|
|
51 |
|
52 |
return output_music_file
|
53 |
|
|
|
54 |
def generate_text_video(script):
|
55 |
image_paths = create_images_from_script(script)
|
56 |
clips = []
|
57 |
for img_path in image_paths:
|
58 |
image_clip = ImageClip(img_path).set_duration(3).set_position(('center', 'center'))
|
59 |
clips.append(image_clip)
|
|
|
60 |
final_video = concatenate_videoclips(clips, method="compose")
|
61 |
final_video.write_videofile("/tmp/final_video.mp4", fps=24)
|
62 |
return "/tmp/final_video.mp4"
|
63 |
|
|
|
64 |
def combine_audio_video(video_path, audio_path):
|
65 |
video = VideoFileClip(video_path)
|
66 |
audio = AudioFileClip(audio_path)
|
|
|
68 |
final_video.write_videofile("/tmp/final_comedy_video.mp4", fps=24)
|
69 |
return "/tmp/final_comedy_video.mp4"
|
70 |
|
|
|
71 |
def generate_comedy_and_animation(prompt):
|
72 |
script = generate_comedy_script(prompt)
|
73 |
audio_file = text_to_speech(script)
|
|
|
76 |
final_video = combine_audio_video(video_file, fun_music)
|
77 |
return script, audio_file, final_video
|
78 |
|
79 |
+
def generate_kids_content(theme):
|
|
|
80 |
music_file = generate_fun_music(theme, output_music_file="kids_music.wav")
|
81 |
clips = []
|
82 |
for i in range(5):
|
|
|
87 |
frame_path = f'/tmp/kids_temp_{i}.png'
|
88 |
img.save(frame_path)
|
89 |
clips.append(ImageClip(frame_path).set_duration(1).set_position(('center', 'center')))
|
|
|
90 |
final_video = CompositeVideoClip(clips, size=(800, 400))
|
91 |
final_video = final_video.set_audio(AudioFileClip(music_file))
|
92 |
+
final_video.write_videofile("/tmp/kids_animation.mp4", fps=24)
|
93 |
+
return music_file, "/tmp/kids_animation.mp4"
|
|
|
|
|
|
|
|
|
|
|
94 |
|
|
|
95 |
with gr.Blocks() as app:
|
96 |
gr.Markdown("## AI Comedy and Kids Content Generator")
|
97 |
|