Update pages/music_recommendations.py
Browse files- pages/music_recommendations.py +15 -13
pages/music_recommendations.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
| 2 |
from openai import OpenAI
|
| 3 |
import time
|
| 4 |
-
import
|
| 5 |
-
import torchaudio
|
| 6 |
-
from torchaudio.pipelines import MusicGen
|
| 7 |
|
| 8 |
# Initialize the OpenAI client
|
| 9 |
client = OpenAI(
|
|
@@ -11,10 +11,6 @@ client = OpenAI(
|
|
| 11 |
base_url="https://api.aimlapi.com",
|
| 12 |
)
|
| 13 |
|
| 14 |
-
# Load MusicGen model
|
| 15 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 16 |
-
model = MusicGen.get_pretrained("facebook/musicgen-small", device=device)
|
| 17 |
-
|
| 18 |
# Streamlit app layout
|
| 19 |
st.title("Mood-based Music Generator")
|
| 20 |
|
|
@@ -39,19 +35,25 @@ if st.button("Generate Music"):
|
|
| 39 |
},
|
| 40 |
],
|
| 41 |
)
|
|
|
|
| 42 |
message = response.choices[0].message.content
|
| 43 |
st.write(f"Assistant: {message}")
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
|
| 51 |
# Save the generated audio to a file
|
| 52 |
audio_filename = "musicgen_out.wav"
|
| 53 |
-
|
| 54 |
|
| 55 |
st.success("Music has been generated!")
|
|
|
|
| 56 |
# Play the generated audio in Streamlit
|
| 57 |
-
st.audio(audio_filename)
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import scipy.io.wavfile
|
| 4 |
from openai import OpenAI
|
| 5 |
import time
|
| 6 |
+
import numpy as np
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Initialize the OpenAI client
|
| 9 |
client = OpenAI(
|
|
|
|
| 11 |
base_url="https://api.aimlapi.com",
|
| 12 |
)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# Streamlit app layout
|
| 15 |
st.title("Mood-based Music Generator")
|
| 16 |
|
|
|
|
| 35 |
},
|
| 36 |
],
|
| 37 |
)
|
| 38 |
+
|
| 39 |
message = response.choices[0].message.content
|
| 40 |
st.write(f"Assistant: {message}")
|
| 41 |
|
| 42 |
+
# Load the synthesizer model for music generation
|
| 43 |
+
synthesiser = pipeline("text-to-audio", "facebook/musicgen-small")
|
| 44 |
+
|
| 45 |
+
# Simulate a short wait to represent loading time for music generation
|
| 46 |
+
time.sleep(2)
|
| 47 |
|
| 48 |
+
# Generate the music using the synthesizer model based on the message
|
| 49 |
+
music = synthesiser(message, forward_params={"do_sample": True, "guidance_scale": 1})
|
| 50 |
|
| 51 |
# Save the generated audio to a file
|
| 52 |
audio_filename = "musicgen_out.wav"
|
| 53 |
+
scipy.io.wavfile.write(audio_filename, rate=music["sampling_rate"], data=np.array(music["audio"]))
|
| 54 |
|
| 55 |
st.success("Music has been generated!")
|
| 56 |
+
|
| 57 |
# Play the generated audio in Streamlit
|
| 58 |
+
st.audio(audio_filename)
|
| 59 |
+
|