Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,81 +1,49 @@
|
|
1 |
-
import os
|
2 |
import gradio as gr
|
3 |
-
|
4 |
-
from
|
5 |
-
from
|
6 |
-
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
7 |
-
from PIL import Image
|
8 |
-
import requests
|
9 |
-
import torch
|
10 |
|
11 |
-
# Load pre-trained
|
12 |
-
|
13 |
-
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
14 |
-
model = GPT2LMHeadModel.from_pretrained(model_name)
|
15 |
|
16 |
-
# Function to generate kids' music using
|
17 |
def generate_kids_music(theme):
|
18 |
-
|
19 |
-
|
|
|
|
|
20 |
|
21 |
-
#
|
22 |
-
|
23 |
-
|
|
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
return
|
34 |
-
|
35 |
-
#
|
36 |
-
def
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
return frames
|
44 |
-
|
45 |
-
# Function to generate placeholder images (replace with DALL-E Mini if needed)
|
46 |
-
def generate_image(text):
|
47 |
-
image = Image.new('RGB', (1024, 1024), color=(255, 255, 255)) # Blank image
|
48 |
-
return image
|
49 |
-
|
50 |
-
# Create a video based on the generated frames
|
51 |
-
def create_video_from_frames(frames):
|
52 |
-
images = []
|
53 |
-
for frame in frames:
|
54 |
-
images.append(frame) # Placeholder images
|
55 |
-
video_file_path = 'output_video.mp4'
|
56 |
-
clip = ImageSequenceClip(images, fps=24)
|
57 |
-
clip.write_videofile(video_file_path, codec='libx264')
|
58 |
-
return video_file_path
|
59 |
-
|
60 |
-
# Main function to handle both music and comedy generation
|
61 |
-
def generate_music_and_comedy(theme):
|
62 |
-
script = generate_script(theme) # Generate a comedic script
|
63 |
-
music_file = generate_kids_music(theme) # Generate kid-friendly music
|
64 |
-
frames = generate_frames_from_script(script) # Generate frames from script
|
65 |
-
video_file = create_video_from_frames(frames) # Create video from frames
|
66 |
-
|
67 |
-
return video_file, music_file
|
68 |
-
|
69 |
-
# Gradio UI setup
|
70 |
-
interface = gr.Interface(
|
71 |
fn=generate_music_and_comedy,
|
72 |
-
inputs=gr.Textbox(label="
|
73 |
-
outputs=[gr.
|
74 |
-
title="AI Kids Music
|
75 |
-
description="Generate
|
76 |
)
|
77 |
|
78 |
-
# Launch the interface
|
79 |
if __name__ == "__main__":
|
80 |
-
|
|
|
81 |
|
|
|
|
|
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 |
|