Spaces:
Sleeping
Sleeping
Tirath5504
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,59 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import numpy as np
|
3 |
-
import torch
|
4 |
-
import transformers
|
5 |
-
from packaging.version import parse
|
6 |
-
import sys
|
7 |
-
import io
|
8 |
-
import importlib.metadata as importlib_metadata
|
9 |
-
import soundfile as sf
|
10 |
-
import importlib.metadata as importlib_metadata
|
11 |
-
|
12 |
-
loading_kwargs = {}
|
13 |
-
if parse(importlib_metadata.version("transformers")) >= parse("4.40.0"):
|
14 |
-
loading_kwargs["attn_implementation"] = "eager"
|
15 |
-
|
16 |
-
def generate(prompt):
|
17 |
-
model = transformers.MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small", torchscript=True, return_dict=False, **loading_kwargs)
|
18 |
-
sample_length = 8
|
19 |
-
n_tokens = sample_length * model.config.audio_encoder.frame_rate + 3
|
20 |
-
sampling_rate = model.config.audio_encoder.sampling_rate
|
21 |
-
processor = transformers.AutoProcessor.from_pretrained("facebook/musicgen-small")
|
22 |
-
inputs = processor(
|
23 |
-
text=[
|
24 |
-
prompt,
|
25 |
-
],
|
26 |
-
padding=True,
|
27 |
-
return_tensors="pt",
|
28 |
-
)
|
29 |
-
audio_values = model.generate(**inputs, do_sample=True, guidance_scale=3, max_new_tokens=n_tokens)
|
30 |
-
waveform = audio_values[0].cpu().squeeze() * 2**15
|
31 |
-
audio_buffer = io.BytesIO()
|
32 |
-
sf.write(audio_buffer, waveform.numpy().astype(np.int16), sampling_rate, format='WAV')
|
33 |
-
audio_buffer.seek(0)
|
34 |
-
return audio_buffer
|
35 |
-
|
36 |
-
st.title("Music Generator")
|
37 |
-
|
38 |
-
text_prompt = st.text_input("Text Prompt", "")
|
39 |
-
|
40 |
-
examples = [
|
41 |
-
"80s pop track with bassy drums and synth",
|
42 |
-
"Earthy tones, environmentally conscious, ukulele-infused, harmonic, breezy, easygoing, organic instrumentation, gentle grooves",
|
43 |
-
"90s rock song with loud guitars and heavy drums",
|
44 |
-
"Heartful EDM with beautiful synths and chords",
|
45 |
-
]
|
46 |
-
|
47 |
-
st.sidebar.title("Examples")
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
st.warning("Please enter a text prompt.")
|
60 |
-
|
61 |
-
# Debugging
|
62 |
-
if st.checkbox("Show debug info"):
|
63 |
st.write("Text Prompt:", text_prompt)
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
import transformers
|
5 |
+
from packaging.version import parse
|
6 |
+
import sys
|
7 |
+
import io
|
8 |
+
import importlib.metadata as importlib_metadata
|
9 |
+
import soundfile as sf
|
10 |
+
import importlib.metadata as importlib_metadata
|
11 |
+
|
12 |
+
loading_kwargs = {}
|
13 |
+
if parse(importlib_metadata.version("transformers")) >= parse("4.40.0"):
|
14 |
+
loading_kwargs["attn_implementation"] = "eager"
|
15 |
+
|
16 |
+
def generate(prompt):
|
17 |
+
model = transformers.MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small", torchscript=True, return_dict=False, **loading_kwargs)
|
18 |
+
sample_length = 8
|
19 |
+
n_tokens = sample_length * model.config.audio_encoder.frame_rate + 3
|
20 |
+
sampling_rate = model.config.audio_encoder.sampling_rate
|
21 |
+
processor = transformers.AutoProcessor.from_pretrained("facebook/musicgen-small")
|
22 |
+
inputs = processor(
|
23 |
+
text=[
|
24 |
+
prompt,
|
25 |
+
],
|
26 |
+
padding=True,
|
27 |
+
return_tensors="pt",
|
28 |
+
)
|
29 |
+
audio_values = model.generate(**inputs, do_sample=True, guidance_scale=3, max_new_tokens=n_tokens)
|
30 |
+
waveform = audio_values[0].cpu().squeeze() * 2**15
|
31 |
+
audio_buffer = io.BytesIO()
|
32 |
+
sf.write(audio_buffer, waveform.numpy().astype(np.int16), sampling_rate, format='WAV')
|
33 |
+
audio_buffer.seek(0)
|
34 |
+
return audio_buffer
|
35 |
+
|
36 |
+
st.title("Music Generator")
|
37 |
+
|
38 |
+
text_prompt = st.text_input("Text Prompt", "")
|
39 |
+
|
40 |
+
examples = [
|
41 |
+
"80s pop track with bassy drums and synth",
|
42 |
+
"Earthy tones, environmentally conscious, ukulele-infused, harmonic, breezy, easygoing, organic instrumentation, gentle grooves",
|
43 |
+
"90s rock song with loud guitars and heavy drums",
|
44 |
+
"Heartful EDM with beautiful synths and chords",
|
45 |
+
]
|
46 |
+
|
47 |
+
st.sidebar.title("Examples")
|
48 |
+
selected_example = st.sidebar.radio("Select an example", examples)
|
49 |
+
|
50 |
+
if st.button("Generate Audio"):
|
51 |
+
if selected_example or text_prompt:
|
52 |
+
with st.spinner("Generating audio..."):
|
53 |
+
audio_output = generate(selected_example)
|
54 |
+
st.audio(audio_output, format='audio/wav')
|
55 |
+
else:
|
56 |
+
st.warning("Please select or enter a text prompt.")
|
57 |
+
|
58 |
+
if st.checkbox("Show debug info"):
|
|
|
|
|
|
|
|
|
59 |
st.write("Text Prompt:", text_prompt)
|