arjunbahuguna commited on
Commit
662e8df
·
verified ·
1 Parent(s): 8ab75d1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ import numpy as np
4
+ from scipy.io.wavfile import write
5
+
6
+ # Title of the Streamlit app
7
+ st.title("Text-to-Audio Generation App")
8
+
9
+ # Text area for user input
10
+ text_input = st.text_area('Enter text to generate audio!')
11
+
12
+ # Create the audio generation pipeline
13
+ try:
14
+ pipe = pipeline(model="suno/bark-small")
15
+ except ImportError as e:
16
+ st.error(f"Error importing pipeline from transformers: {e}")
17
+ st.stop()
18
+
19
+ # Generate audio based on user input
20
+ if text_input:
21
+ with st.spinner('Generating audio...'):
22
+ output = pipe(text_input)
23
+
24
+ # Extract audio array and sampling rate from the output
25
+ audio_array = output["audio"]
26
+ sampling_rate = output["sampling_rate"]
27
+
28
+ # Ensure the audio array is a numpy array
29
+ audio_array = np.array(audio_array, dtype=np.float32)
30
+
31
+ # Squeeze to remove single-dimensional entries from the shape of the array
32
+ audio_array = np.squeeze(audio_array)
33
+
34
+ # Save the audio array as a WAV file
35
+ write("output.wav", sampling_rate, audio_array)
36
+
37
+ # Read the saved WAV file
38
+ audio_file = open("output.wav", "rb")
39
+ audio_bytes = audio_file.read()
40
+
41
+ # Display the output audio
42
+ st.audio(audio_bytes, format="audio/wav")
43
+
44
+ # Optional: Display JSON output for debugging
45
+ if st.checkbox('Show raw output'):
46
+ st.json(output)