Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,40 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
from gtts import gTTS
|
|
|
4 |
from PIL import Image, ImageDraw, ImageFont
|
5 |
-
from moviepy.editor import CompositeVideoClip, concatenate_videoclips, ImageClip
|
6 |
import numpy as np
|
7 |
from scipy.io.wavfile import write
|
8 |
-
from pydub import AudioSegment
|
9 |
-
|
10 |
|
11 |
script_generator = pipeline("text-generation", model="gpt2", truncation=True, max_length=100)
|
12 |
|
|
|
|
|
|
|
13 |
|
14 |
def text_to_speech(script):
|
15 |
-
if isinstance(script, list):
|
16 |
-
texts = [item['generated_text'] for item in script if isinstance(item, dict) and 'generated_text' in item]
|
17 |
-
script = " ".join(texts)
|
18 |
-
print("Final script for TTS:", script) # Debugging line
|
19 |
-
|
20 |
-
if not script.strip():
|
21 |
-
raise ValueError("No text to convert to speech.")
|
22 |
-
|
23 |
tts = gTTS(text=script, lang='en')
|
24 |
audio_file = 'output.mp3'
|
25 |
tts.save(audio_file)
|
26 |
return audio_file
|
27 |
|
28 |
-
|
29 |
def generate_animation(script):
|
30 |
-
if isinstance(script, list):
|
31 |
-
texts = [item['generated_text'] for item in script if isinstance(item, dict) and 'generated_text' in item]
|
32 |
-
script = " ".join(texts)
|
33 |
-
|
34 |
lines = script.split('. ')
|
35 |
-
|
36 |
-
for line in lines:
|
37 |
img = Image.new('RGB', (800, 400), color=(0, 0, 0))
|
38 |
d = ImageDraw.Draw(img)
|
39 |
fnt = ImageFont.load_default()
|
40 |
d.text((10, 180), line, font=fnt, fill=(255, 255, 255))
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
45 |
return "/tmp/final_video.mp4"
|
46 |
|
47 |
|
48 |
-
|
49 |
def generate_sine_wave(frequency, duration, sample_rate=44100, amplitude=0.5):
|
50 |
t = np.linspace(0, duration, int(sample_rate * duration), False)
|
51 |
wave = amplitude * np.sin(2 * np.pi * frequency * t)
|
@@ -64,17 +53,30 @@ def generate_kids_music(theme, output_music_file="kids_music.wav"):
|
|
64 |
|
65 |
def generate_kids_animation_with_music(theme, output_video_file="kids_animation.mp4"):
|
66 |
music_file = generate_kids_music(theme)
|
67 |
-
clips = [
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
video = CompositeVideoClip(clips)
|
69 |
video.write_videofile(output_video_file, fps=24)
|
70 |
return output_video_file, music_file
|
71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
def generate_comedy_and_animation(prompt):
|
73 |
-
script =
|
74 |
-
print("Generated script:", script) # Debugging line
|
75 |
audio_file = text_to_speech(script)
|
76 |
video_file = generate_animation(script)
|
77 |
-
|
|
|
78 |
|
79 |
def generate_kids_content(theme):
|
80 |
video_file, music_file = generate_kids_animation_with_music(theme)
|
@@ -113,3 +115,4 @@ app.launch()
|
|
113 |
|
114 |
|
115 |
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
from gtts import gTTS
|
4 |
+
from moviepy.editor import ImageSequenceClip, CompositeVideoClip, ImageClip, AudioFileClip
|
5 |
from PIL import Image, ImageDraw, ImageFont
|
|
|
6 |
import numpy as np
|
7 |
from scipy.io.wavfile import write
|
|
|
|
|
8 |
|
9 |
script_generator = pipeline("text-generation", model="gpt2", truncation=True, max_length=100)
|
10 |
|
11 |
+
def generate_comedy_script(prompt):
|
12 |
+
script = script_generator(prompt)[0]['generated_text']
|
13 |
+
return script
|
14 |
|
15 |
def text_to_speech(script):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
tts = gTTS(text=script, lang='en')
|
17 |
audio_file = 'output.mp3'
|
18 |
tts.save(audio_file)
|
19 |
return audio_file
|
20 |
|
|
|
21 |
def generate_animation(script):
|
|
|
|
|
|
|
|
|
22 |
lines = script.split('. ')
|
23 |
+
frames = []
|
24 |
+
for i, line in enumerate(lines):
|
25 |
img = Image.new('RGB', (800, 400), color=(0, 0, 0))
|
26 |
d = ImageDraw.Draw(img)
|
27 |
fnt = ImageFont.load_default()
|
28 |
d.text((10, 180), line, font=fnt, fill=(255, 255, 255))
|
29 |
+
frame_path = f'/tmp/frame_{i}.png'
|
30 |
+
img.save(frame_path)
|
31 |
+
frames.append(frame_path)
|
32 |
+
|
33 |
+
clip = ImageSequenceClip(frames, fps=1)
|
34 |
+
clip.write_videofile("/tmp/final_video.mp4", fps=24)
|
35 |
return "/tmp/final_video.mp4"
|
36 |
|
37 |
|
|
|
38 |
def generate_sine_wave(frequency, duration, sample_rate=44100, amplitude=0.5):
|
39 |
t = np.linspace(0, duration, int(sample_rate * duration), False)
|
40 |
wave = amplitude * np.sin(2 * np.pi * frequency * t)
|
|
|
53 |
|
54 |
def generate_kids_animation_with_music(theme, output_video_file="kids_animation.mp4"):
|
55 |
music_file = generate_kids_music(theme)
|
56 |
+
clips = []
|
57 |
+
img = Image.new('RGB', (800, 400), color=(0, 0, 255))
|
58 |
+
d = ImageDraw.Draw(img)
|
59 |
+
fnt = ImageFont.load_default()
|
60 |
+
d.text((10, 180), f"Kids Music: {theme}", font=fnt, fill=(255, 255, 0))
|
61 |
+
img.save('/tmp/kids_temp.png')
|
62 |
+
clips.append(ImageClip('/tmp/kids_temp.png').set_duration(5))
|
63 |
video = CompositeVideoClip(clips)
|
64 |
video.write_videofile(output_video_file, fps=24)
|
65 |
return output_video_file, music_file
|
66 |
|
67 |
+
def combine_audio_video(video_path, audio_path):
|
68 |
+
video = VideoFileClip(video_path)
|
69 |
+
audio = AudioFileClip(audio_path)
|
70 |
+
final_video = video.set_audio(audio)
|
71 |
+
final_video.write_videofile("/tmp/final_comedy_video.mp4", fps=24)
|
72 |
+
return "/tmp/final_comedy_video.mp4"
|
73 |
+
|
74 |
def generate_comedy_and_animation(prompt):
|
75 |
+
script = generate_comedy_script(prompt)
|
|
|
76 |
audio_file = text_to_speech(script)
|
77 |
video_file = generate_animation(script)
|
78 |
+
final_video = combine_audio_video(video_file, audio_file)
|
79 |
+
return script, audio_file, final_video
|
80 |
|
81 |
def generate_kids_content(theme):
|
82 |
video_file, music_file = generate_kids_animation_with_music(theme)
|
|
|
115 |
|
116 |
|
117 |
|
118 |
+
|