Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,47 +1,63 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
from gtts import gTTS
|
4 |
-
from moviepy.editor import
|
5 |
-
from
|
|
|
|
|
6 |
import numpy as np
|
7 |
from scipy.io.wavfile import write
|
8 |
|
9 |
# Load and Initialize Models
|
10 |
script_generator = pipeline("text-generation", model="gpt2", truncation=True, max_length=100)
|
|
|
|
|
11 |
|
|
|
12 |
def generate_comedy_script(prompt):
|
13 |
script = script_generator(prompt)[0]['generated_text']
|
14 |
return script
|
15 |
|
|
|
16 |
def text_to_speech(script):
|
17 |
tts = gTTS(text=script, lang='en')
|
18 |
audio_file = 'output.mp3'
|
19 |
tts.save(audio_file)
|
20 |
return audio_file
|
21 |
|
|
|
22 |
def create_images_from_script(script):
|
23 |
lines = script.split('. ')
|
24 |
image_paths = []
|
25 |
for i, line in enumerate(lines):
|
26 |
-
img =
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
image_path = f'/tmp/image_{i}.png'
|
31 |
-
img.save(image_path)
|
32 |
-
image_paths.append(image_path)
|
33 |
return image_paths
|
34 |
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
image_paths = create_images_from_script(script)
|
37 |
clips = []
|
38 |
-
for
|
39 |
image_clip = ImageClip(img_path).set_duration(3).set_position(('center', 'center'))
|
40 |
clips.append(image_clip)
|
|
|
41 |
final_video = concatenate_videoclips(clips, method="compose")
|
42 |
final_video.write_videofile("/tmp/final_video.mp4", fps=24)
|
43 |
return "/tmp/final_video.mp4"
|
44 |
|
|
|
45 |
def combine_audio_video(video_path, audio_path):
|
46 |
video = VideoFileClip(video_path)
|
47 |
audio = AudioFileClip(audio_path)
|
@@ -49,78 +65,24 @@ def combine_audio_video(video_path, audio_path):
|
|
49 |
final_video.write_videofile("/tmp/final_comedy_video.mp4", fps=24)
|
50 |
return "/tmp/final_comedy_video.mp4"
|
51 |
|
|
|
52 |
def generate_comedy_and_animation(prompt):
|
53 |
script = generate_comedy_script(prompt)
|
54 |
audio_file = text_to_speech(script)
|
55 |
-
video_file =
|
56 |
-
|
|
|
57 |
return script, audio_file, final_video
|
58 |
|
59 |
-
|
60 |
-
t = np.linspace(0, duration, int(sample_rate * duration), False)
|
61 |
-
wave = amplitude * np.sin(2 * np.pi * frequency * t)
|
62 |
-
return wave
|
63 |
-
|
64 |
-
def generate_kids_music(theme, output_music_file="kids_music.wav"):
|
65 |
-
sample_rate = 44100
|
66 |
-
duration = 2
|
67 |
-
c4_wave = generate_sine_wave(261.63, duration)
|
68 |
-
d4_wave = generate_sine_wave(293.66, duration)
|
69 |
-
e4_wave = generate_sine_wave(329.63, duration)
|
70 |
-
wave = np.concatenate([c4_wave, d4_wave, e4_wave])
|
71 |
-
audio_wave = np.int16(wave * 32767)
|
72 |
-
write(output_music_file, sample_rate, audio_wave)
|
73 |
-
return output_music_file
|
74 |
-
|
75 |
def generate_kids_animation_with_music(theme, output_video_file="kids_animation.mp4"):
|
76 |
-
music_file =
|
77 |
clips = []
|
78 |
for i in range(5):
|
79 |
img = Image.new('RGB', (800, 400), color=(0, 0, 255))
|
80 |
d = ImageDraw.Draw(img)
|
81 |
fnt = ImageFont.load_default()
|
82 |
-
d.text((10, 180
|
83 |
-
frame_path = f'/tmp/kids_temp_{i}.png'
|
84 |
-
img.save(frame_path)
|
85 |
-
clips.append(ImageClip(frame_path).set_duration(1).set_position(('center', 'center')))
|
86 |
-
final_video = CompositeVideoClip(clips, size=(800, 400))
|
87 |
-
final_video = final_video.set_audio(AudioFileClip(music_file))
|
88 |
-
final_video.write_videofile(output_video_file, fps=24)
|
89 |
-
return output_video_file, music_file
|
90 |
-
|
91 |
-
def generate_kids_content(theme):
|
92 |
-
video_file, music_file = generate_kids_animation_with_music(theme)
|
93 |
-
return music_file, video_file
|
94 |
-
|
95 |
-
with gr.Blocks() as app:
|
96 |
-
gr.Markdown("## AI Comedy and Kids Content Generator")
|
97 |
-
|
98 |
-
with gr.Tab("Generate Comedy Animation"):
|
99 |
-
prompt_input = gr.Textbox(label="Comedy Prompt")
|
100 |
-
generate_btn = gr.Button("Generate Comedy Script and Animation")
|
101 |
-
comedy_script = gr.Textbox(label="Generated Script")
|
102 |
-
comedy_audio = gr.Audio(label="Generated Audio")
|
103 |
-
comedy_video = gr.Video(label="Generated Animation")
|
104 |
-
|
105 |
-
generate_btn.click(
|
106 |
-
generate_comedy_and_animation,
|
107 |
-
inputs=prompt_input,
|
108 |
-
outputs=[comedy_script, comedy_audio, comedy_video]
|
109 |
-
)
|
110 |
-
|
111 |
-
with gr.Tab("Generate Kids Music Animation"):
|
112 |
-
theme_input = gr.Textbox(label="Kids Music Theme")
|
113 |
-
generate_music_btn = gr.Button("Generate Kids Music and Animation")
|
114 |
-
kids_music_audio = gr.Audio(label="Generated Music")
|
115 |
-
kids_music_video = gr.Video(label="Generated Kids Animation")
|
116 |
-
|
117 |
-
generate_music_btn.click(
|
118 |
-
generate_kids_content,
|
119 |
-
inputs=theme_input,
|
120 |
-
outputs=[kids_music_audio, kids_music_video]
|
121 |
-
)
|
122 |
-
|
123 |
-
app.launch()
|
124 |
|
125 |
|
126 |
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
from gtts import gTTS
|
4 |
+
from moviepy.editor import CompositeVideoClip, ImageClip, AudioFileClip, VideoFileClip, concatenate_videoclips
|
5 |
+
from diffusers import StableDiffusionPipeline
|
6 |
+
import torch
|
7 |
+
from PIL import Image
|
8 |
import numpy as np
|
9 |
from scipy.io.wavfile import write
|
10 |
|
11 |
# Load and Initialize Models
|
12 |
script_generator = pipeline("text-generation", model="gpt2", truncation=True, max_length=100)
|
13 |
+
image_generator = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16).to("cuda")
|
14 |
+
music_generator = pipeline("text-to-music", model="HuggingFace/MusicGen") # Replace with actual model if available
|
15 |
|
16 |
+
# Generate Comedy Script
|
17 |
def generate_comedy_script(prompt):
|
18 |
script = script_generator(prompt)[0]['generated_text']
|
19 |
return script
|
20 |
|
21 |
+
# Convert Text to Speech
|
22 |
def text_to_speech(script):
|
23 |
tts = gTTS(text=script, lang='en')
|
24 |
audio_file = 'output.mp3'
|
25 |
tts.save(audio_file)
|
26 |
return audio_file
|
27 |
|
28 |
+
# Create Images Using Stable Diffusion
|
29 |
def create_images_from_script(script):
|
30 |
lines = script.split('. ')
|
31 |
image_paths = []
|
32 |
for i, line in enumerate(lines):
|
33 |
+
img = image_generator(line).images[0]
|
34 |
+
img_path = f'/tmp/image_{i}.png'
|
35 |
+
img.save(img_path)
|
36 |
+
image_paths.append(img_path)
|
|
|
|
|
|
|
37 |
return image_paths
|
38 |
|
39 |
+
# Generate Fun Music Track
|
40 |
+
def generate_fun_music(prompt, output_music_file="fun_music.wav"):
|
41 |
+
# Generate music based on the prompt
|
42 |
+
music = music_generator(prompt)[0]['generated_music']
|
43 |
+
# Save the generated music to a file
|
44 |
+
with open(output_music_file, 'wb') as f:
|
45 |
+
f.write(music)
|
46 |
+
return output_music_file
|
47 |
+
|
48 |
+
# Create Video from Generated Images
|
49 |
+
def generate_text_video(script):
|
50 |
image_paths = create_images_from_script(script)
|
51 |
clips = []
|
52 |
+
for img_path in image_paths:
|
53 |
image_clip = ImageClip(img_path).set_duration(3).set_position(('center', 'center'))
|
54 |
clips.append(image_clip)
|
55 |
+
|
56 |
final_video = concatenate_videoclips(clips, method="compose")
|
57 |
final_video.write_videofile("/tmp/final_video.mp4", fps=24)
|
58 |
return "/tmp/final_video.mp4"
|
59 |
|
60 |
+
# Combine Audio and Video
|
61 |
def combine_audio_video(video_path, audio_path):
|
62 |
video = VideoFileClip(video_path)
|
63 |
audio = AudioFileClip(audio_path)
|
|
|
65 |
final_video.write_videofile("/tmp/final_comedy_video.mp4", fps=24)
|
66 |
return "/tmp/final_comedy_video.mp4"
|
67 |
|
68 |
+
# Main Function to Generate Comedy Animation
|
69 |
def generate_comedy_and_animation(prompt):
|
70 |
script = generate_comedy_script(prompt)
|
71 |
audio_file = text_to_speech(script)
|
72 |
+
video_file = generate_text_video(script)
|
73 |
+
fun_music = generate_fun_music(prompt)
|
74 |
+
final_video = combine_audio_video(video_file, fun_music)
|
75 |
return script, audio_file, final_video
|
76 |
|
77 |
+
# Generate Kids Music Animation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
def generate_kids_animation_with_music(theme, output_video_file="kids_animation.mp4"):
|
79 |
+
music_file = generate_fun_music(theme, output_music_file="kids_music.wav")
|
80 |
clips = []
|
81 |
for i in range(5):
|
82 |
img = Image.new('RGB', (800, 400), color=(0, 0, 255))
|
83 |
d = ImageDraw.Draw(img)
|
84 |
fnt = ImageFont.load_default()
|
85 |
+
d.text((10, 180
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
|
88 |
|