File size: 1,518 Bytes
946da56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import streamlit as st
from transformers import pipeline
import scipy

# Load the Bark model
@st.cache_resource  # Caches the model to avoid reloading
def load_model():
    return pipeline("text-to-speech", model="suno/bark")

synthesiser = load_model()

# Streamlit app layout
st.title("AI Song Generator")
st.markdown("Generate AI songs from text using Suno's Bark model! 🎵 Add special cues like `♪ lyrics ♪` or `[laughs]` for customization.")

# Input field for song lyrics
lyrics = st.text_area("Enter your song lyrics or text (e.g., ♪ Twinkle, twinkle, little star... ♪):", height=150)

# Button to trigger song generation
if st.button("Generate Song"):
    if lyrics.strip():
        st.info("Generating the song... This may take a few moments.")
        try:
            # Generate audio
            result = synthesiser(lyrics, forward_params={"do_sample": True})
            
            # Save audio to a file
            audio_path = "generated_song.wav"
            scipy.io.wavfile.write(audio_path, rate=result["sampling_rate"], data=result["audio"])
            
            # Play audio
            st.audio(audio_path, format="audio/wav")
            st.success("Song generation complete!")
        except Exception as e:
            st.error(f"An error occurred: {e}")
    else:
        st.warning("Please provide song lyrics to generate audio.")

# Optional cleanup (use if audio files are temporary)
import os
if os.path.exists("generated_song.wav"):
    os.remove("generated_song.wav")