-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudiocraft.py
59 lines (49 loc) · 2.23 KB
/
audiocraft.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from transformers import AutoProcessor, MusicgenForConditionalGeneration
import scipy
import os
from langchain.llms import OpenAI
import streamlit as st
from langchain.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
# Streamlit app
st.title("Text to Song Generator")
prompt = st.text_input("Enter the input and it'll generate a song")
#api key
os.environ["OPENAI_API_KEY"] = "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"
#prompt template
title_template = PromptTemplate(
input_variables = ['topic'],
template='''
You're a professional Song Descripton Writer, The user will prompt you on different topics and sentences, you've to create song descriptions for the user.Make sure yo only create song descriptions nothing other than that..don't make song descriptions for "how to make sandwich and stuff" and some dumb stuff. Whenever the user prompts you check whether the prompt that has been prompted by the user can be converted into a song description or not. if it can be converted then you can create a song description for it.
user: {topic}
you:
'''
)
#llm
llm = OpenAI(temperature=0.9)
title_chain = LLMChain(llm=llm, prompt=title_template, verbose=True)
# if Button is clicked
if st.button("Create song"):
# Validate inputs
response = title_chain.run(prompt)
st.write(response)
# Load the MusicGen model
processor = AutoProcessor.from_pretrained(
"facebook/musicgen-small")
model = MusicgenForConditionalGeneration.from_pretrained(
"facebook/musicgen-small")
# Format the input based on the song description
inputs = processor(
text=[response],
padding=True,
return_tensors="pt",
)
# Generate the audio
audio_values = model.generate(**inputs, max_new_tokens=256)
sampling_rate = model.config.audio_encoder.sampling_rate
# Save the wav file into your system
scipy.io.wavfile.write(
"musicgen_out.wav", rate=sampling_rate, data=audio_values[0, 0].numpy())
# Render a success message with the song description generated by ChatGPT
st.success("Your song has been succesfully created with the following prompt: "+response)