Spaces:
Runtime error
Runtime error
import numpy as np | |
from pydub import AudioSegment | |
def sine_wave(frequency, duration_ms): | |
"""Generate a sine wave tone at a given frequency and duration.""" | |
sample_rate = 44100 # Sample rate in Hz | |
t = np.linspace(0, duration_ms / 1000, int(sample_rate * (duration_ms / 1000)), False) | |
wave = 0.5 * np.sin(2 * np.pi * frequency * t) # Sine wave formula | |
audio_segment = AudioSegment( | |
wave.tobytes(), | |
frame_rate=sample_rate, | |
sample_width=2, # 16-bit audio | |
channels=1 # Mono audio | |
) | |
return audio_segment | |
def generate_kids_music(theme): | |
# Create a simple melody based on themes | |
notes = { | |
"birthday": ["C4", "E4", "G4", "C5"], | |
"ocean": ["C4", "D4", "E4", "F4"], | |
"adventure": ["E4", "G4", "A4", "C5"] | |
} | |
selected_notes = notes.get(theme, ["C4", "E4", "G4"]) # Default notes if theme not found | |
music = AudioSegment.silent(duration=0) # Start with silence | |
# Define frequencies for notes | |
frequencies = { | |
"C4": 261.63, | |
"D4": 293.66, | |
"E4": 329.63, | |
"F4": 349.23, | |
"G4": 392.00, | |
"A4": 440.00, | |
"C5": 523.25 | |
} | |
for note in selected_notes: | |
frequency = frequencies[note] | |
sine = sine_wave(frequency, duration_ms=500) # 0.5 second for each note | |
music += sine | |
# Export the generated music | |
output_file = f"{theme}_kids_music.wav" | |
music.export(output_file, format="wav") | |
return output_file | |
from transformers import pipeline | |
# Load the GPT-2 model for text generation | |
comedy_generator = pipeline('text-generation', model='gpt2') | |
def generate_comedy_script(prompt): | |
result = comedy_generator(prompt, max_length=150, num_return_sequences=1) | |
return result[0]['generated_text'] | |
from gtts import gTTS | |
def script_to_audio(script): | |
tts = gTTS(script, lang='en') | |
audio_file = "comedy_script.mp3" | |
tts.save(audio_file) | |
return audio_file | |
import gradio as gr | |
def generate_music_and_comedy(theme, prompt): | |
# Generate music | |
music_file = generate_kids_music(theme) | |
# Generate comedy script | |
comedy_script = generate_comedy_script(prompt) | |
# Convert the comedy script to audio | |
audio_file = script_to_audio(comedy_script) | |
return music_file, comedy_script, audio_file | |
# Gradio Interface | |
iface = gr.Interface( | |
fn=generate_music_and_comedy, | |
inputs=[ | |
gr.Dropdown(choices=["birthday", "ocean", "adventure"], label="Select Theme for Kids' Music"), | |
gr.Textbox(lines=2, placeholder="Enter a prompt for comedy...", label="Comedy Prompt") | |
], | |
outputs=[ | |
gr.Audio(label="Generated Kids' Music"), | |
gr.Textbox(label="Generated Comedy Script"), | |
gr.Audio(label="Comedy Audio") | |
], | |
title="AI Music and Comedy Generator", | |
description="Generate unique music for kids based on themes and comedic scripts from your prompts." | |
) | |
# Launch the app | |
iface.launch() | |