ageraustine commited on
Commit
e4beb59
1 Parent(s): bfcf19f

add mixups

Browse files
Files changed (1) hide show
  1. app.py +99 -101
app.py CHANGED
@@ -1,108 +1,106 @@
1
  import streamlit as st
2
- import librosa
3
- import numpy as np
4
  import requests
 
5
  import os
6
- # import noisereduce as nr
7
-
8
- # Replace with your actual API endpoint URL
9
- API_URL = "https://jwry1smafa0ql7pc.us-east-1.aws.endpoints.huggingface.cloud"
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- # Retrieve the token from environment variables using `os.getenv()`
12
- TOKEN = os.getenv("ACCESS_TOKEN")
13
-
14
- # Set headers with the retrieved token
15
  headers = {
16
- "Authorization": f"Bearer {TOKEN}",
 
17
  }
18
 
19
-
20
- def query(payload):
21
- """Sends a POST request to the API endpoint with the given payload."""
22
- response = requests.post(API_URL, headers=headers, json=payload)
23
- return response.json()
24
-
25
-
26
- def main():
27
- """
28
- Creates the Streamlit app for uploading audio (optional),
29
- and sending it to an API for music generation.
30
- """
31
- st.title("SonglabAI")
32
-
33
- # 25 genres
34
- genres = [
35
- "Pop", "Rock", "Jazz", "Electronic", "Hip-Hop",
36
- "Classical", "Lofi", "Chillpop", "Bossa Nova",
37
- "Country", "Reggae", "Funk", "Blues", "Metal",
38
- "R&B", "Indie", "Soul", "Folk", "Disco",
39
- "Techno", "Ambient", "Dance", "Ska", "Trap"
40
- ]
41
-
42
- selected_genre = st.selectbox("Select Genre", genres)
43
- prompt = st.text_input("Enter Music Description")
44
-
45
- # bpm
46
- bpm = st.number_input("Enter Speed in BPM", min_value=60)
47
-
48
- # Duration input
49
- duration = st.slider("Select Duration (in seconds)", 15, 300, 15, step=5)
50
-
51
- uploaded_audio = None
52
-
53
- # Optional audio upload
54
- if st.checkbox("Upload Melody"):
55
- uploaded_file = st.file_uploader("Choose an audio file", type=["mp3", "wav"])
56
- if uploaded_file is not None:
57
- try:
58
- audio_bytes = uploaded_file.read()
59
- audio, sample_rate = librosa.load(audio_bytes, sr=None) # Avoid resampling
60
- if sample_rate != 32000:
61
- st.text("Resampling audio to 32,000 Hz...")
62
- audio = librosa.resample(audio, sample_rate, 32000)
63
- uploaded_audio = audio[:int(32000 * 15)] # Extract first 15 seconds
64
- except Exception as e:
65
- st.error(f"Error processing audio: {e}")
66
-
67
- if st.button("Generate Music"):
68
- if not prompt:
69
- st.error("Please enter a music description.")
70
- elif selected_genre:
71
- st.text("Preparing request...")
72
-
73
- # Prepare the payload based on presence/absence of uploaded audio
74
- payload = {
75
- "inputs": {"prompt": f"{prompt} {selected_genre} {bpm} bpm", "duration": int(duration)}
76
- }
77
-
78
- if uploaded_audio is not None:
79
- payload = {"inputs": {"prompt": f"{prompt} {selected_genre} {bpm} bpm", "duration": int(duration),
80
- "melody": uploaded_audio.tolist()}}
81
-
82
- try:
83
- # Send the request and handle the response
84
- response = query(payload)
85
-
86
- if "error" in response:
87
- st.error(f"Error from API: {response['error']}")
88
- else:
89
- arr = np.array(response[0]["generated_audio"])
90
- sr = 32000
91
- audio = arr.astype(np.float32)
92
- # reduced_noise = nr.reduce_noise(audio, sr=sr) # Apply noise reduction
93
- # audio = reduced_noise # Use the noise-reduced audio
94
- # stereo_audio = np.stack((audio, audio), axis=1)
95
- sr = 32000
96
- if audio is not None:
97
- st.success("Audio generation complete.")
98
- st.audio(audio, format="audio/wav", sample_rate=sr)
99
- else:
100
- st.warning(
101
- "Unexpected response structure. Missing 'generated_audio'."
102
- )
103
- except Exception as e:
104
- st.error(f"Unexpected error: {e}")
105
-
106
-
107
- if __name__ == "__main__":
108
- main()
 
1
  import streamlit as st
 
 
2
  import requests
3
+ import numpy as np
4
  import os
5
+ from pydub import AudioSegment
6
+
7
+ # Try to get API_URL from environment variables, if not found set to a default value
8
+ try:
9
+ API_URL = os.environ["API_URL"]
10
+ except KeyError:
11
+ st.error("API_URL environment variable is not set.")
12
+ st.stop()
13
+
14
+ # Try to get the Bearer token from environment variables, if not found set to a default value
15
+ try:
16
+ BEARER_TOKEN = os.environ["BEARER_TOKEN"]
17
+ except KeyError:
18
+ st.error("BEARER_TOKEN environment variable is not set.")
19
+ st.stop()
20
 
 
 
 
 
21
  headers = {
22
+ "Authorization": f"Bearer {BEARER_TOKEN}",
23
+ "Content-Type": "application/json"
24
  }
25
 
26
+ # Streamlit app title
27
+ st.title("Songlabai")
28
+
29
+ genres = [
30
+ "Pop", "Rock", "Hip Hop", "Jazz", "Blues",
31
+ "Country", "Classical", "Electronic", "Reggae",
32
+ "Folk", "R&B", "Metal", "Punk", "Indie",
33
+ "Dance", "World", "Gospel", "Soul", "Funk",
34
+ "Ambient", "Techno", "Disco", "House", "Trance",
35
+ "Dubstep"
36
+ ]
37
+
38
+ genre = st.selectbox("Select Genre:", genres)
39
+ energy_levels = ["Low", "Medium", "High"]
40
+ energy_level = st.radio("Energy Level:", energy_levels)
41
+ description = st.text_input("Description:", "")
42
+
43
+ # Duration input
44
+ duration = st.slider("Duration (in seconds):", min_value=15, max_value=90, value=30, step=1)
45
+
46
+ # Generate audio based on the user's prompt
47
+ if st.button("Generate Audio"):
48
+ prompt = f"{genre}, Energy: {energy_level}, Description: {description}"
49
+ st.text("Generating audio...")
50
+ response = requests.post(API_URL, headers=headers, json={"inputs": {"prompt": prompt, "duration": duration}})
51
+ audio = np.array(response.json()[0]['generated_audio'], dtype=np.float32)
52
+ sample_rate = response.json()[0]['sample_rate']
53
+ st.audio(audio, format="audio/wav", sample_rate=sample_rate, start_time=0)
54
+
55
+ # Post-processing options
56
+ st.sidebar.title("Post-processing Options")
57
+ apply_stereo = st.sidebar.checkbox("Apply Stereo Effect")
58
+ reverse = st.sidebar.checkbox("Reverse Audio")
59
+ change_speed = st.sidebar.checkbox("Change Speed")
60
+ if change_speed:
61
+ speed_factor = st.sidebar.slider("Speed Factor:", min_value=0.1, max_value=2.0, value=1.0, step=0.1)
62
+
63
+ # Pitch shifting
64
+ st.sidebar.title("Pitch Shifting")
65
+ pitch_shift = st.sidebar.checkbox("Pitch Shift")
66
+ if pitch_shift:
67
+ pitch_semitones = st.sidebar.slider("Pitch (semitones):", min_value=-12, max_value=12, value=0, step=1)
68
+
69
+ # Reverb
70
+ st.sidebar.title("Reverb")
71
+ add_reverb = st.sidebar.checkbox("Add Reverb")
72
+ if add_reverb:
73
+ reverb_room_scale = st.sidebar.slider("Room Scale:", min_value=0.0, max_value=100.0, value=50.0)
74
+ reverb_damping = st.sidebar.slider("Damping:", min_value=0.0, max_value=100.0, value=50.0)
75
+ reverb_wet_only = st.sidebar.checkbox("Wet Only", value=False)
76
+
77
+ # Apply selected post-processing
78
+ if apply_stereo or reverse or change_speed or pitch_shift or add_reverb:
79
+ st.text("Applying post-processing...")
80
+
81
+ # Convert audio to pydub AudioSegment
82
+ audio_segment = AudioSegment(audio.tobytes(), frame_rate=sample_rate, sample_width=audio.itemsize, channels=1)
83
+
84
+ # Apply selected post-processing
85
+ if apply_stereo:
86
+ audio_segment = audio_segment.pan(-0.5).overlay(audio_segment.pan(0.5))
87
+
88
+ if reverse:
89
+ audio_segment = audio_segment.reverse()
90
+
91
+ if change_speed:
92
+ audio_segment = audio_segment.speedup(playback_speed=speed_factor)
93
+
94
+ if pitch_shift:
95
+ audio_segment = audio_segment._spawn(audio_segment.raw_data, overrides={
96
+ "frame_rate": int(audio_segment.frame_rate * (2 ** (pitch_semitones / 12.0)))
97
+ })
98
+
99
+ if add_reverb:
100
+ audio_segment = audio_segment.reverb(reverberance=reverb_room_scale,
101
+ damping=reverb_damping,
102
+ wet_only=reverb_wet_only,
103
+ room_scale=100)
104
+
105
+ # Play the processed audio
106
+ st.audio(audio_segment.raw_data, format="audio/wav", start_time=0)