Manasa1 commited on
Commit
c6b8f95
·
verified ·
1 Parent(s): abf8f9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -72
app.py CHANGED
@@ -1,146 +1,92 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- import pyttsx3
4
  from moviepy.editor import TextClip, CompositeVideoClip, concatenate_videoclips
 
 
5
  from pydub import AudioSegment
6
- import os
7
-
8
- # Load pre-trained Hugging Face text generation model for comedy scripts
9
- script_generator = pipeline("text-generation", model="gpt2")
10
-
11
- # Function to generate a comedy script using GPT-2
12
- from transformers import pipeline
13
-
14
- script_generator = pipeline("text-generation", model="gpt2", truncation=True, max_length=100) # Adjust max_length as needed
15
-
16
-
17
- from gtts import gTTS
18
- import os
19
-
20
- from gtts import gTTS
21
 
22
-
23
- from gtts import gTTS
24
 
25
  def text_to_speech(script):
26
- # Check if script is a list and extract text
27
  if isinstance(script, list):
28
- # Extract text from dictionaries if needed
29
  texts = [item['text'] for item in script if isinstance(item, dict) and 'text' in item]
30
- script = " ".join(texts) # Join extracted texts
31
-
32
- print("Final script for TTS:", script) # Debugging line
33
 
34
- # Check if script is not empty
35
  if not script.strip():
36
  raise ValueError("No text to convert to speech.")
37
 
38
  tts = gTTS(text=script, lang='en')
39
- audio_file = 'output.mp3' # Specify your output audio file name
40
  tts.save(audio_file)
41
  return audio_file
42
 
43
-
44
-
45
-
46
- from moviepy.editor import TextClip
47
-
48
  def generate_animation(script):
49
  lines = script.split('\n')
50
  clips = []
51
-
52
  for line in lines:
53
- text_clip = TextClip(line, fontsize=40, color='white', size=(800, 400), bg_color='black', method='caption', print_cmd=True)
54
- text_clip = text_clip.set_duration(3) # Set duration for each clip
55
  clips.append(text_clip)
56
-
57
- # Concatenate all text clips into one video clip
58
  final_video = concatenate_videoclips(clips)
59
  return final_video
60
 
61
-
62
- import numpy as np
63
- from scipy.io.wavfile import write
64
- from pydub import AudioSegment
65
-
66
  def generate_sine_wave(frequency, duration, sample_rate=44100, amplitude=0.5):
67
- """Generate a sine wave."""
68
  t = np.linspace(0, duration, int(sample_rate * duration), False)
69
  wave = amplitude * np.sin(2 * np.pi * frequency * t)
70
  return wave
71
 
72
  def generate_kids_music(theme, output_music_file="kids_music.wav"):
73
- # Generate a sine wave for C4, D4, E4 notes
74
  sample_rate = 44100
75
- duration = 2 # 2 seconds per note
76
-
77
- c4_wave = generate_sine_wave(261.63, duration) # C4
78
- d4_wave = generate_sine_wave(293.66, duration) # D4
79
- e4_wave = generate_sine_wave(329.63, duration) # E4
80
-
81
- # Combine the waves
82
  wave = np.concatenate([c4_wave, d4_wave, e4_wave])
83
-
84
- # Normalize to 16-bit PCM format
85
  audio_wave = np.int16(wave * 32767)
86
-
87
- # Save as a wav file
88
  write(output_music_file, sample_rate, audio_wave)
89
-
90
- # Convert to AudioSegment (optional if you need to modify)
91
- audio_segment = AudioSegment.from_wav(output_music_file)
92
-
93
  return output_music_file
94
 
95
-
96
- # Function to combine kids music with a simple animation
97
  def generate_kids_animation_with_music(theme, output_video_file="kids_animation.mp4"):
98
- # Step 1: Generate kids music
99
  music_file = generate_kids_music(theme)
100
-
101
- # Step 2: Generate simple animation for the music (using text animation as a placeholder)
102
- clips = [TextClip("Kids Music: " + theme, fontsize=70, color='yellow', size=(800, 400), bg_color='blue', method='caption').set_duration(5)]
103
  video = CompositeVideoClip(clips)
104
  video.write_videofile(output_video_file, fps=24)
105
-
106
  return output_video_file, music_file
107
 
108
- # Gradio interface to bring everything together
109
  def generate_comedy_and_animation(prompt):
110
- # Comedy Script Generation
111
  script = script_generator(prompt)
 
112
  audio_file = text_to_speech(script)
113
  video_file = generate_animation(script)
114
-
115
  return script, audio_file, video_file
116
 
117
  def generate_kids_content(theme):
118
  video_file, music_file = generate_kids_animation_with_music(theme)
119
  return music_file, video_file
120
 
121
- # Gradio app for Comedy and Kids Content
122
  with gr.Blocks() as app:
123
  gr.Markdown("## AI Comedy and Kids Content Generator")
124
-
125
  with gr.Tab("Generate Comedy Animation"):
126
  prompt_input = gr.Textbox(label="Comedy Prompt")
127
  generate_btn = gr.Button("Generate Comedy Script and Animation")
128
  comedy_script = gr.Textbox(label="Generated Script")
129
  comedy_audio = gr.Audio(label="Generated Audio")
130
  comedy_video = gr.Video(label="Generated Animation")
131
-
132
  generate_btn.click(
133
  generate_comedy_and_animation,
134
  inputs=prompt_input,
135
  outputs=[comedy_script, comedy_audio, comedy_video]
136
  )
137
-
138
  with gr.Tab("Generate Kids Music Animation"):
139
  theme_input = gr.Textbox(label="Kids Music Theme")
140
  generate_music_btn = gr.Button("Generate Kids Music and Animation")
141
  kids_music_audio = gr.Audio(label="Generated Music")
142
  kids_music_video = gr.Video(label="Generated Kids Animation")
143
-
144
  generate_music_btn.click(
145
  generate_kids_content,
146
  inputs=theme_input,
@@ -151,3 +97,4 @@ app.launch()
151
 
152
 
153
 
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ from gtts import gTTS
4
  from moviepy.editor import TextClip, CompositeVideoClip, concatenate_videoclips
5
+ import numpy as np
6
+ from scipy.io.wavfile import write
7
  from pydub import AudioSegment
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ script_generator = pipeline("text-generation", model="gpt2", truncation=True, max_length=100)
 
10
 
11
  def text_to_speech(script):
 
12
  if isinstance(script, list):
 
13
  texts = [item['text'] for item in script if isinstance(item, dict) and 'text' in item]
14
+ script = " ".join(texts)
 
 
15
 
 
16
  if not script.strip():
17
  raise ValueError("No text to convert to speech.")
18
 
19
  tts = gTTS(text=script, lang='en')
20
+ audio_file = 'output.mp3'
21
  tts.save(audio_file)
22
  return audio_file
23
 
 
 
 
 
 
24
  def generate_animation(script):
25
  lines = script.split('\n')
26
  clips = []
 
27
  for line in lines:
28
+ text_clip = TextClip(line, fontsize=40, color='white', size=(800, 400), bg_color='black', method='caption')
29
+ text_clip = text_clip.set_duration(3)
30
  clips.append(text_clip)
 
 
31
  final_video = concatenate_videoclips(clips)
32
  return final_video
33
 
 
 
 
 
 
34
  def generate_sine_wave(frequency, duration, sample_rate=44100, amplitude=0.5):
 
35
  t = np.linspace(0, duration, int(sample_rate * duration), False)
36
  wave = amplitude * np.sin(2 * np.pi * frequency * t)
37
  return wave
38
 
39
  def generate_kids_music(theme, output_music_file="kids_music.wav"):
 
40
  sample_rate = 44100
41
+ duration = 2
42
+ c4_wave = generate_sine_wave(261.63, duration)
43
+ d4_wave = generate_sine_wave(293.66, duration)
44
+ e4_wave = generate_sine_wave(329.63, duration)
 
 
 
45
  wave = np.concatenate([c4_wave, d4_wave, e4_wave])
 
 
46
  audio_wave = np.int16(wave * 32767)
 
 
47
  write(output_music_file, sample_rate, audio_wave)
 
 
 
 
48
  return output_music_file
49
 
 
 
50
  def generate_kids_animation_with_music(theme, output_video_file="kids_animation.mp4"):
 
51
  music_file = generate_kids_music(theme)
52
+ clips = [TextClip(f"Kids Music: {theme}", fontsize=70, color='yellow', size=(800, 400), bg_color='blue', method='caption').set_duration(5)]
 
 
53
  video = CompositeVideoClip(clips)
54
  video.write_videofile(output_video_file, fps=24)
 
55
  return output_video_file, music_file
56
 
 
57
  def generate_comedy_and_animation(prompt):
 
58
  script = script_generator(prompt)
59
+ print("Generated script:", script) # Debugging line
60
  audio_file = text_to_speech(script)
61
  video_file = generate_animation(script)
 
62
  return script, audio_file, video_file
63
 
64
  def generate_kids_content(theme):
65
  video_file, music_file = generate_kids_animation_with_music(theme)
66
  return music_file, video_file
67
 
 
68
  with gr.Blocks() as app:
69
  gr.Markdown("## AI Comedy and Kids Content Generator")
70
+
71
  with gr.Tab("Generate Comedy Animation"):
72
  prompt_input = gr.Textbox(label="Comedy Prompt")
73
  generate_btn = gr.Button("Generate Comedy Script and Animation")
74
  comedy_script = gr.Textbox(label="Generated Script")
75
  comedy_audio = gr.Audio(label="Generated Audio")
76
  comedy_video = gr.Video(label="Generated Animation")
77
+
78
  generate_btn.click(
79
  generate_comedy_and_animation,
80
  inputs=prompt_input,
81
  outputs=[comedy_script, comedy_audio, comedy_video]
82
  )
83
+
84
  with gr.Tab("Generate Kids Music Animation"):
85
  theme_input = gr.Textbox(label="Kids Music Theme")
86
  generate_music_btn = gr.Button("Generate Kids Music and Animation")
87
  kids_music_audio = gr.Audio(label="Generated Music")
88
  kids_music_video = gr.Video(label="Generated Kids Animation")
89
+
90
  generate_music_btn.click(
91
  generate_kids_content,
92
  inputs=theme_input,
 
97
 
98
 
99
 
100
+