Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,47 @@
|
|
| 1 |
from transformers import AutoProcessor, MusicgenForConditionalGeneration
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
import streamlit as st
|
| 4 |
|
| 5 |
-
def mu_gen(prompt):
|
| 6 |
-
processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
|
| 7 |
-
model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
padding=True,
|
| 15 |
-
return_tensors="pt",
|
| 16 |
-
)
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# Generate audio on CPU
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
return audio_values, sampling_rate
|
| 25 |
|
| 26 |
def main():
|
| 27 |
-
st.title("Text to
|
| 28 |
|
| 29 |
# Input text prompt
|
| 30 |
-
|
| 31 |
|
| 32 |
-
if st.button(
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
generated_music, sampling_rate = mu_gen(prompt)
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
else:
|
| 40 |
-
st.warning("Please enter a text prompt.")
|
| 41 |
|
| 42 |
-
if __name__ ==
|
| 43 |
-
main()
|
|
|
|
| 1 |
from transformers import AutoProcessor, MusicgenForConditionalGeneration
|
| 2 |
+
from IPython.display import Audio
|
| 3 |
+
import scipy
|
| 4 |
import torch
|
| 5 |
import streamlit as st
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
def mu_gen(prompt):
|
| 9 |
+
processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
|
| 10 |
+
model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
|
| 11 |
|
| 12 |
+
device = torch.device("cpu")
|
| 13 |
+
model.to(device)
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
inputs = processor(
|
| 16 |
+
text = [str(prompt)], # This line is correct
|
| 17 |
+
padding=True,
|
| 18 |
+
return_tensors="pt",
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
inputs = {key: value.to(device) for key, value in inputs.items()}
|
| 22 |
|
| 23 |
# Generate audio on CPU
|
| 24 |
+
audio_values = model.generate(**inputs, max_new_tokens=256)
|
| 25 |
+
sampling_rate = model.config.audio_encoder.sampling_rate
|
| 26 |
+
|
| 27 |
+
# Create an Audio object from the generated audio
|
| 28 |
+
result = Audio(audio_values[0].numpy(), rate=sampling_rate)
|
| 29 |
+
|
| 30 |
+
return result
|
| 31 |
|
|
|
|
| 32 |
|
| 33 |
def main():
|
| 34 |
+
st.title("Text to music")
|
| 35 |
|
| 36 |
# Input text prompt
|
| 37 |
+
title = st.text_input('Write a prompt (จะใช้เวลาค่อนข้างมากในการสร้างเนื่องจากใช้ CPU ในการรันโมเดล)', "")
|
| 38 |
|
| 39 |
+
if st.button('Generate Image'):
|
| 40 |
+
# Call the pic_mo function to generate an image
|
| 41 |
+
generated_music = mu_gen(prompt)
|
|
|
|
| 42 |
|
| 43 |
+
# Display the generated image
|
| 44 |
+
st.image(generated_music, caption='Generated Music', use_column_width=True)
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
if __name__ == '__main__':
|
| 47 |
+
main()
|