Manasa1 commited on
Commit
ac48b9a
·
verified ·
1 Parent(s): 6e53060

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -34
app.py CHANGED
@@ -1,49 +1,111 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- from pydub.generators import Sine
 
4
  from pydub import AudioSegment
 
5
 
6
  # Load pre-trained Hugging Face text generation model for comedy scripts
7
  script_generator = pipeline("text-generation", model="gpt2")
8
 
9
- # Function to generate a kids' music track using Sine waves
10
- def generate_kids_music(theme):
11
- # Use sine wave for simple tone-based music
12
- frequencies = [220, 247, 262, 294, 330, 349] # A sequence of basic notes (Hz)
13
- duration_per_note = 500 # 0.5 seconds per note
14
- song = AudioSegment.silent(duration=0)
15
-
16
- # Generate a basic sequence of notes
17
- for freq in frequencies:
18
- sine_wave = Sine(freq).to_audio_segment(duration=duration_per_note)
19
- song += sine_wave
20
-
21
- # Export the generated song
22
- file_path = f"kids_music_{theme}.mp3"
23
- song.export(file_path, format="mp3")
24
- return file_path
25
-
26
  # Function to generate a comedy script using GPT-2
27
  def generate_comedy_script(prompt):
28
- result = script_generator(prompt, max_length=100, num_return_sequences=1)
29
  return result[0]["generated_text"]
30
 
31
- # Gradio interface
32
- def generate_music_and_comedy(theme, prompt):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  music_file = generate_kids_music(theme)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  script = generate_comedy_script(prompt)
35
- return script, music_file
36
-
37
- # Gradio interface
38
- demo = gr.Interface(
39
- fn=generate_music_and_comedy,
40
- inputs=[gr.Textbox(label="Music Theme"), gr.Textbox(label="Comedy Prompt")],
41
- outputs=[gr.Textbox(label="Generated Comedy Script"), gr.Audio(label="Generated Kids Music")],
42
- title="AI Kids Music and Comedy Generator",
43
- description="Generate kids' music based on a theme and comedy scripts based on a prompt.",
44
- )
45
-
46
- if __name__ == "__main__":
47
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import pyttsx3
4
+ from manim import Scene, Text, Write, config
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
  def generate_comedy_script(prompt):
13
+ result = script_generator(prompt, max_length=150, num_return_sequences=1)
14
  return result[0]["generated_text"]
15
 
16
+ # Text-to-Speech (TTS) using pyttsx3 for comedy
17
+ def text_to_speech(script, output_audio_file="script_audio.mp3"):
18
+ engine = pyttsx3.init()
19
+ engine.save_to_file(script, output_audio_file)
20
+ engine.runAndWait()
21
+ return output_audio_file
22
+
23
+ # Function to generate animated video using Manim (for comedy script)
24
+ def generate_animation(script, output_video_file="animated_script.mp4"):
25
+ class ScriptAnimation(Scene):
26
+ def construct(self):
27
+ # Create a text object and animate it
28
+ script_text = Text(script[:200]) # Limited text for simplicity
29
+ self.play(Write(script_text))
30
+
31
+ config.media_dir = "./media"
32
+ config.output_file = output_video_file
33
+ animation = ScriptAnimation()
34
+ animation.render()
35
+
36
+ return os.path.join(config.media_dir, "videos", output_video_file)
37
+
38
+ # Function to generate simple music (for kids content)
39
+ def generate_kids_music(theme, output_music_file="kids_music.wav"):
40
+ # Simple sine wave generation using pydub for kids' music
41
+ music = AudioSegment.sine(frequency=261.63) # C4 note
42
+ music += AudioSegment.sine(frequency=293.66) # D4 note
43
+ music += AudioSegment.sine(frequency=329.63) # E4 note
44
+
45
+ # Extend for more complexity, then export
46
+ music.export(output_music_file, format="wav")
47
+ return output_music_file
48
+
49
+ # Function to combine kids music with an animation
50
+ def generate_kids_animation_with_music(theme, output_video_file="kids_animation.mp4"):
51
+ # Step 1: Generate kids music
52
  music_file = generate_kids_music(theme)
53
+
54
+ # Step 2: Generate simple animation for the music (using text animation as a placeholder)
55
+ class KidsMusicAnimation(Scene):
56
+ def construct(self):
57
+ music_text = Text("Kids Music: " + theme)
58
+ self.play(Write(music_text))
59
+
60
+ config.media_dir = "./media"
61
+ config.output_file = output_video_file
62
+ animation = KidsMusicAnimation()
63
+ animation.render()
64
+
65
+ return output_video_file, music_file
66
+
67
+ # Gradio interface to bring everything together
68
+ def generate_comedy_and_animation(prompt):
69
+ # Comedy Script Generation
70
  script = generate_comedy_script(prompt)
71
+ audio_file = text_to_speech(script)
72
+ video_file = generate_animation(script)
73
+
74
+ return script, audio_file, video_file
75
+
76
+ def generate_kids_content(theme):
77
+ video_file, music_file = generate_kids_animation_with_music(theme)
78
+ return music_file, video_file
79
+
80
+ # Gradio app for Comedy and Kids Content
81
+ with gr.Blocks() as app:
82
+ gr.Markdown("## AI Comedy and Kids Content Generator")
83
+
84
+ with gr.Tab("Generate Comedy Animation"):
85
+ prompt_input = gr.Textbox(label="Comedy Prompt")
86
+ generate_btn = gr.Button("Generate Comedy Script and Animation")
87
+ comedy_script = gr.Textbox(label="Generated Script")
88
+ comedy_audio = gr.Audio(label="Generated Audio")
89
+ comedy_video = gr.Video(label="Generated Animation")
90
+
91
+ generate_btn.click(
92
+ generate_comedy_and_animation,
93
+ inputs=prompt_input,
94
+ outputs=[comedy_script, comedy_audio, comedy_video]
95
+ )
96
+
97
+ with gr.Tab("Generate Kids Music Animation"):
98
+ theme_input = gr.Textbox(label="Kids Music Theme")
99
+ generate_music_btn = gr.Button("Generate Kids Music and Animation")
100
+ kids_music_audio = gr.Audio(label="Generated Music")
101
+ kids_music_video = gr.Video(label="Generated Kids Animation")
102
+
103
+ generate_music_btn.click(
104
+ generate_kids_content,
105
+ inputs=theme_input,
106
+ outputs=[kids_music_audio, kids_music_video]
107
+ )
108
+
109
+ app.launch()
110
 
111