Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -6,28 +6,37 @@ from PIL import Image, ImageDraw, ImageFont
|
|
6 |
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 |
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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,19 +47,15 @@ def create_images_from_script(script):
|
|
38 |
image_paths.append(img_path)
|
39 |
return image_paths
|
40 |
|
|
|
41 |
def generate_fun_music(prompt, output_music_file="fun_music.wav"):
|
42 |
-
|
43 |
-
response = music_generator(prompt)
|
44 |
-
|
45 |
-
# Extract audio and sampling rate from the response
|
46 |
audio_data = response["audio"]
|
47 |
sampling_rate = response["sampling_rate"]
|
48 |
-
|
49 |
-
# Save the generated music to a file
|
50 |
scipy.io.wavfile.write(output_music_file, rate=sampling_rate, data=audio_data)
|
51 |
-
|
52 |
return output_music_file
|
53 |
|
|
|
54 |
def generate_text_video(script):
|
55 |
image_paths = create_images_from_script(script)
|
56 |
clips = []
|
@@ -61,6 +66,7 @@ def generate_text_video(script):
|
|
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,6 +74,7 @@ def combine_audio_video(video_path, 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,7 +83,8 @@ def generate_comedy_and_animation(prompt):
|
|
76 |
final_video = combine_audio_video(video_file, fun_music)
|
77 |
return script, audio_file, final_video
|
78 |
|
79 |
-
|
|
|
80 |
music_file = generate_fun_music(theme, output_music_file="kids_music.wav")
|
81 |
clips = []
|
82 |
for i in range(5):
|
@@ -89,9 +97,15 @@ def generate_kids_content(theme):
|
|
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(
|
93 |
-
return music_file,
|
94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
with gr.Blocks() as app:
|
96 |
gr.Markdown("## AI Comedy and Kids Content Generator")
|
97 |
|
@@ -130,3 +144,4 @@ app.launch()
|
|
130 |
|
131 |
|
132 |
|
|
|
|
6 |
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 |
+
import os
|
10 |
+
from groq import Groq
|
11 |
|
12 |
+
# Load and Initialize Models
|
13 |
+
# Use Groq for text generation
|
14 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
15 |
|
16 |
# Use Stable Diffusion (open-source) for image generation
|
17 |
image_generator = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base", torch_dtype=torch.float16).to("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 |
+
# Generate Comedy Script
|
23 |
def generate_comedy_script(prompt):
|
24 |
+
response = client.chat.completions.create(
|
25 |
+
messages=[
|
26 |
+
{"role": "user", "content": prompt}
|
27 |
+
],
|
28 |
+
model="llama-3.1"
|
29 |
+
)
|
30 |
+
script = response["choices"][0]["message"]["content"]
|
31 |
return script
|
32 |
|
33 |
+
# Convert Text to Speech using Coqui TTS
|
34 |
def text_to_speech(script):
|
35 |
output_audio = 'output.wav'
|
36 |
tts.tts_to_file(text=script, file_path=output_audio)
|
37 |
return output_audio
|
38 |
|
39 |
+
# Create Images Using Stable Diffusion
|
40 |
def create_images_from_script(script):
|
41 |
lines = script.split('. ')
|
42 |
image_paths = []
|
|
|
47 |
image_paths.append(img_path)
|
48 |
return image_paths
|
49 |
|
50 |
+
# Generate Fun Music Track using Groq API
|
51 |
def generate_fun_music(prompt, output_music_file="fun_music.wav"):
|
52 |
+
response = client.music.generate(prompt=prompt)
|
|
|
|
|
|
|
53 |
audio_data = response["audio"]
|
54 |
sampling_rate = response["sampling_rate"]
|
|
|
|
|
55 |
scipy.io.wavfile.write(output_music_file, rate=sampling_rate, data=audio_data)
|
|
|
56 |
return output_music_file
|
57 |
|
58 |
+
# Create Video from Generated Images
|
59 |
def generate_text_video(script):
|
60 |
image_paths = create_images_from_script(script)
|
61 |
clips = []
|
|
|
66 |
final_video.write_videofile("/tmp/final_video.mp4", fps=24)
|
67 |
return "/tmp/final_video.mp4"
|
68 |
|
69 |
+
# Combine Audio and Video
|
70 |
def combine_audio_video(video_path, audio_path):
|
71 |
video = VideoFileClip(video_path)
|
72 |
audio = AudioFileClip(audio_path)
|
|
|
74 |
final_video.write_videofile("/tmp/final_comedy_video.mp4", fps=24)
|
75 |
return "/tmp/final_comedy_video.mp4"
|
76 |
|
77 |
+
# Main Function to Generate Comedy Animation
|
78 |
def generate_comedy_and_animation(prompt):
|
79 |
script = generate_comedy_script(prompt)
|
80 |
audio_file = text_to_speech(script)
|
|
|
83 |
final_video = combine_audio_video(video_file, fun_music)
|
84 |
return script, audio_file, final_video
|
85 |
|
86 |
+
# Generate Kids Music Animation
|
87 |
+
def generate_kids_animation_with_music(theme, output_video_file="kids_animation.mp4"):
|
88 |
music_file = generate_fun_music(theme, output_music_file="kids_music.wav")
|
89 |
clips = []
|
90 |
for i in range(5):
|
|
|
97 |
clips.append(ImageClip(frame_path).set_duration(1).set_position(('center', 'center')))
|
98 |
final_video = CompositeVideoClip(clips, size=(800, 400))
|
99 |
final_video = final_video.set_audio(AudioFileClip(music_file))
|
100 |
+
final_video.write_videofile(output_video_file, fps=24)
|
101 |
+
return music_file, output_video_file
|
102 |
|
103 |
+
# Main Function to Generate Kids Content
|
104 |
+
def generate_kids_content(theme):
|
105 |
+
music_file, video_file = generate_kids_animation_with_music(theme)
|
106 |
+
return music_file, video_file
|
107 |
+
|
108 |
+
# Gradio Interface
|
109 |
with gr.Blocks() as app:
|
110 |
gr.Markdown("## AI Comedy and Kids Content Generator")
|
111 |
|
|
|
144 |
|
145 |
|
146 |
|
147 |
+
|