Manasa1 commited on
Commit
e95a1cd
·
verified ·
1 Parent(s): f0141f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -37
app.py CHANGED
@@ -1,12 +1,11 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
  from gtts import gTTS
4
- from moviepy.editor import ImageSequenceClip, CompositeVideoClip, ImageClip, AudioFileClip, VideoFileClip
5
  from PIL import Image, ImageDraw, ImageFont
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):
@@ -19,22 +18,49 @@ def text_to_speech(script):
19
  tts.save(audio_file)
20
  return audio_file
21
 
22
- def generate_animation(script):
23
  lines = script.split('. ')
24
- frames = []
25
  for i, line in enumerate(lines):
26
  img = Image.new('RGB', (800, 400), color=(0, 0, 0))
27
  d = ImageDraw.Draw(img)
28
  fnt = ImageFont.load_default()
29
  d.text((10, 180), line, font=fnt, fill=(255, 255, 255))
30
- frame_path = f'/tmp/frame_{i}.png'
31
- img.save(frame_path)
32
- frames.append(frame_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
- clip = ImageSequenceClip(frames, fps=1)
35
- clip.write_videofile("/tmp/final_video.mp4", fps=24)
36
  return "/tmp/final_video.mp4"
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)
@@ -54,30 +80,20 @@ def generate_kids_music(theme, output_music_file="kids_music.wav"):
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)
83
  return music_file, video_file
@@ -110,10 +126,5 @@ with gr.Blocks() as app:
110
  outputs=[kids_music_audio, kids_music_video]
111
  )
112
 
113
- app.launch()
114
-
115
-
116
-
117
-
118
-
119
 
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
  from gtts import gTTS
4
+ from moviepy.editor import ImageSequenceClip, CompositeVideoClip, ImageClip, AudioFileClip, VideoFileClip, TextClip
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):
 
18
  tts.save(audio_file)
19
  return audio_file
20
 
21
+ def create_images_from_script(script):
22
  lines = script.split('. ')
23
+ image_paths = []
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
+ image_path = f'/tmp/image_{i}.png'
30
+ img.save(image_path)
31
+ image_paths.append(image_path)
32
+ return image_paths
33
+
34
+ def generate_animation_with_images(script):
35
+ image_paths = create_images_from_script(script)
36
+ clips = []
37
+ for i, img_path in enumerate(image_paths):
38
+ text_clip = TextClip(script.split('. ')[i], fontsize=40, color='white', size=(800, 400), method='caption')
39
+ text_clip = text_clip.set_duration(3).set_position(('center', 'bottom'))
40
+
41
+ image_clip = ImageClip(img_path).set_duration(3).set_position(('center', 'center'))
42
+
43
+ composite_clip = CompositeVideoClip([image_clip, text_clip])
44
+ clips.append(composite_clip)
45
 
46
+ final_video = concatenate_videoclips(clips, method="compose")
47
+ final_video.write_videofile("/tmp/final_video.mp4", fps=24)
48
  return "/tmp/final_video.mp4"
49
 
50
+ def combine_audio_video(video_path, audio_path):
51
+ video = VideoFileClip(video_path)
52
+ audio = AudioFileClip(audio_path)
53
+ final_video = video.set_audio(audio)
54
+ final_video.write_videofile("/tmp/final_comedy_video.mp4", fps=24)
55
+ return "/tmp/final_comedy_video.mp4"
56
+
57
+ def generate_comedy_and_animation(prompt):
58
+ script = generate_comedy_script(prompt)
59
+ audio_file = text_to_speech(script)
60
+ video_file = generate_animation_with_images(script)
61
+ final_video = combine_audio_video(video_file, audio_file)
62
+ return script, audio_file, final_video
63
+
64
  def generate_sine_wave(frequency, duration, sample_rate=44100, amplitude=0.5):
65
  t = np.linspace(0, duration, int(sample_rate * duration), False)
66
  wave = amplitude * np.sin(2 * np.pi * frequency * t)
 
80
  def generate_kids_animation_with_music(theme, output_video_file="kids_animation.mp4"):
81
  music_file = generate_kids_music(theme)
82
  clips = []
83
+ for i in range(5):
84
+ img = Image.new('RGB', (800, 400), color=(0, 0, 255))
85
+ d = ImageDraw.Draw(img)
86
+ fnt = ImageFont.load_default()
87
+ d.text((10, 180), f"Kids Music: {theme}", font=fnt, fill=(255, 255, 0))
88
+ frame_path = f'/tmp/kids_temp_{i}.png'
89
+ img.save(frame_path)
90
+ clips.append(ImageClip(frame_path).set_duration(1).set_position(('center', 'center')))
91
+
92
+ final_video = CompositeVideoClip(clips, size=(800, 400))
93
+ final_video = final_video.set_audio(AudioFileClip(music_file))
94
+ final_video.write_videofile(output_video_file, fps=24)
95
  return output_video_file, music_file
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  def generate_kids_content(theme):
98
  video_file, music_file = generate_kids_animation_with_music(theme)
99
  return music_file, video_file
 
126
  outputs=[kids_music_audio, kids_music_video]
127
  )
128
 
 
 
 
 
 
 
129
 
130
+ app.launch()