ageraustine commited on
Commit
3993abe
1 Parent(s): 68f3ef1

dependencies

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from transformers import AutoProcessor, MusicgenForConditionalGeneration
3
+ import scipy
4
+ import streamlit as st
5
+
6
+ BASE_MODEL = "facebook/musicgen-small"
7
+
8
+
9
+ processor = AutoProcessor.from_pretrained(BASE_MODEL)
10
+ model = MusicgenForConditionalGeneration.from_pretrained(BASE_MODEL)
11
+
12
+ def generate_audio(text):
13
+ inputs = processor(
14
+ text=[text],
15
+ padding=True,
16
+ return_tensors="pt",
17
+ )
18
+ audio_values = model.generate(**inputs, do_sample=True, guidance_scale=3, max_new_tokens=256)
19
+ cleaned_text = text.replace(" ", "_")
20
+ sampling_rate = model.config.audio_encoder.sampling_rate
21
+ scipy.io.wavfile.write(f'${cleaned_text}.wav', rate=sampling_rate, data=audio_values[0, 0].numpy())
22
+ return f'${cleaned_text}.wav'
23
+
24
+ # Streamlit app title and description
25
+ st.title("Text-to-Audio Generation")
26
+
27
+ # User input for text prompt
28
+ text = st.text_area("Enter Text Prompt:")
29
+
30
+ # Generate audio when the user clicks the button
31
+ if st.button("Generate Audio"):
32
+ if text:
33
+ # Generate audio using the ModelManager
34
+ audio_file_path = generate_audio(text)
35
+
36
+ # Display the audio player
37
+ audio_data = open(audio_file_path, "rb").read()
38
+ st.audio(audio_data, format="audio/wav")
39
+
40
+ # Provide a download link for the audio file
41
+ st.write("Audio Generated Successfully!")
42
+ st.success(f"Download the audio file [here](/{audio_file_path}).")
43
+ else:
44
+ st.warning("Please enter a text prompt.")
45
+