Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from audiocraft.models import MusicGen
|
2 |
+
import streamlit as st
|
3 |
+
import torch
|
4 |
+
import torchaudio
|
5 |
+
import io
|
6 |
+
import base64
|
7 |
+
|
8 |
+
@st.cache_resource
|
9 |
+
def load_model():
|
10 |
+
model = MusicGen.get_pretrained('facebook/musicgen-small')
|
11 |
+
return model
|
12 |
+
|
13 |
+
def generate_music_tensors(description, duration: int):
|
14 |
+
st.write(f"Generating music for: '{description}' (Duration: {duration}s)")
|
15 |
+
model = load_model()
|
16 |
+
model.set_generation_params(use_sampling=True, top_k=250, duration=duration)
|
17 |
+
output = model.generate(descriptions=[description], progress=True)
|
18 |
+
return output[0]
|
19 |
+
|
20 |
+
def create_audio_buffer(samples: torch.Tensor):
|
21 |
+
"""Generate an in-memory audio buffer."""
|
22 |
+
sample_rate = 32000
|
23 |
+
samples = samples.detach().cpu()
|
24 |
+
|
25 |
+
if samples.dim() == 2:
|
26 |
+
samples = samples[None, ...]
|
27 |
+
|
28 |
+
# Create an in-memory buffer to store the audio
|
29 |
+
buffer = io.BytesIO()
|
30 |
+
torchaudio.save(buffer, samples[0], sample_rate, format="wav")
|
31 |
+
buffer.seek(0)
|
32 |
+
return buffer
|
33 |
+
|
34 |
+
def generate_download_link(buffer, file_label="Download Music"):
|
35 |
+
"""Create a download link for the generated audio."""
|
36 |
+
data = buffer.read()
|
37 |
+
b64 = base64.b64encode(data).decode()
|
38 |
+
href = f'<a href="data:audio/wav;base64,{b64}" download="generated_music.wav">{file_label}</a>'
|
39 |
+
return href
|
40 |
+
|
41 |
+
# Apply CSS for improved UI styling
|
42 |
+
st.markdown(
|
43 |
+
"""
|
44 |
+
<style>
|
45 |
+
.title {
|
46 |
+
font-size: 3em;
|
47 |
+
text-align: center;
|
48 |
+
color: #4A90E2;
|
49 |
+
margin-top: 0;
|
50 |
+
}
|
51 |
+
.footer {
|
52 |
+
position: fixed;
|
53 |
+
left: 0;
|
54 |
+
bottom: 0;
|
55 |
+
width: 100%;
|
56 |
+
background-color: #f1f1f1;
|
57 |
+
text-align: center;
|
58 |
+
padding: 10px;
|
59 |
+
font-size: 0.8em;
|
60 |
+
color: #555;
|
61 |
+
}
|
62 |
+
</style>
|
63 |
+
""", unsafe_allow_html=True
|
64 |
+
)
|
65 |
+
|
66 |
+
# Set Streamlit page configuration
|
67 |
+
|
68 |
+
def main():
|
69 |
+
st.markdown('<h1 class="title">Theaimart: Music Generator 🎵</h1>', unsafe_allow_html=True)
|
70 |
+
st.write("Generate music based on your text input using Meta's Audiocraft library!")
|
71 |
+
|
72 |
+
description = st.text_area("Enter a description:")
|
73 |
+
duration = st.slider("Select duration (seconds)", 1, 20, 10)
|
74 |
+
|
75 |
+
if description and duration:
|
76 |
+
music_tensors = generate_music_tensors(description, duration)
|
77 |
+
audio_buffer = create_audio_buffer(music_tensors)
|
78 |
+
|
79 |
+
st.audio(audio_buffer, format="audio/wav")
|
80 |
+
st.markdown(generate_download_link(audio_buffer), unsafe_allow_html=True)
|
81 |
+
|
82 |
+
# Add footer message
|
83 |
+
st.markdown('<div class="footer">Made with ❤️ by Theaimart</div>', unsafe_allow_html=True)
|
84 |
+
|
85 |
+
if __name__ == "__main__":
|
86 |
+
main()
|