ageraustine commited on
Commit
f7b5ba8
1 Parent(s): c9ef03f

add state management

Browse files
Files changed (1) hide show
  1. app.py +20 -10
app.py CHANGED
@@ -3,6 +3,7 @@ import requests
3
  import numpy as np
4
  import os
5
  import base64
 
6
 
7
  # Try to get API_URL from environment variables, if not found set to a default value
8
  try:
@@ -23,6 +24,13 @@ headers = {
23
  "Content-Type": "application/json"
24
  }
25
 
 
 
 
 
 
 
 
26
  # Streamlit app title
27
  st.title("Songlabai")
28
 
@@ -55,9 +63,9 @@ if st.button("Generate Audio"):
55
  payload = {"inputs": {"prompt": prompt, "duration": duration, "track": audio_base64}}
56
  st.text("Generating audio...")
57
  response = requests.post(API_URL, headers=headers, json=payload)
58
- audio = np.array(response.json()[0]['generated_audio'], dtype=np.float32)
59
  sample_rate = response.json()[0]['sample_rate']
60
- st.audio(audio, format="audio/wav", sample_rate=sample_rate, start_time=0)
61
 
62
  # Post-processing options
63
  st.sidebar.title("Post-processing Options")
@@ -86,30 +94,32 @@ if apply_stereo or reverse or change_speed or pitch_shift or add_reverb:
86
  st.text("Applying post-processing...")
87
 
88
  # Apply selected post-processing
 
 
89
  if apply_stereo:
90
  # Create a stereo effect by duplicating the audio and panning left and right
91
- audio_left = audio
92
- audio_right = audio
93
- audio = np.stack([audio_left, audio_right], axis=-1)
94
 
95
  if reverse:
96
  # Reverse the audio array
97
- audio = np.flip(audio)
98
 
99
  if change_speed:
100
  # Change the speed by resampling the audio
101
  new_sample_rate = int(sample_rate * speed_factor)
102
- audio = scipy.signal.resample(audio, int(len(audio) * speed_factor))
103
 
104
  if pitch_shift:
105
  # Pitch shift using the Fourier shift method
106
  pitch_shift_factor = 2 ** (pitch_semitones / 12)
107
- audio = scipy.signal.resample(audio, int(len(audio) / pitch_shift_factor))
108
 
109
  if add_reverb:
110
  # Apply reverb using the convolution method
111
  reverb_ir = scipy.signal.exponential(reverb_room_scale, reverb_damping, reverb_wet_only)
112
- audio = scipy.signal.fftconvolve(audio, reverb_ir)
113
 
114
  # Play the processed audio
115
- st.audio(audio, format="audio/wav", sample_rate=sample_rate, start_time=0)
 
3
  import numpy as np
4
  import os
5
  import base64
6
+ from streamlit import session_state as st_state
7
 
8
  # Try to get API_URL from environment variables, if not found set to a default value
9
  try:
 
24
  "Content-Type": "application/json"
25
  }
26
 
27
+ # Initialize session state variables
28
+ if 'audio' not in st_state:
29
+ st_state.audio = None
30
+
31
+ if 'augmented_audio' not in st_state:
32
+ st_state.augmented_audio = None
33
+
34
  # Streamlit app title
35
  st.title("Songlabai")
36
 
 
63
  payload = {"inputs": {"prompt": prompt, "duration": duration, "track": audio_base64}}
64
  st.text("Generating audio...")
65
  response = requests.post(API_URL, headers=headers, json=payload)
66
+ st_state.audio = np.array(response.json()[0]['generated_audio'], dtype=np.float32)
67
  sample_rate = response.json()[0]['sample_rate']
68
+ st.audio(st_state.audio, format="audio/wav", sample_rate=sample_rate, start_time=0)
69
 
70
  # Post-processing options
71
  st.sidebar.title("Post-processing Options")
 
94
  st.text("Applying post-processing...")
95
 
96
  # Apply selected post-processing
97
+ st_state.augmented_audio = st_state.audio.copy()
98
+
99
  if apply_stereo:
100
  # Create a stereo effect by duplicating the audio and panning left and right
101
+ audio_left = st_state.augmented_audio
102
+ audio_right = st_state.augmented_audio
103
+ st_state.augmented_audio = np.stack([audio_left, audio_right], axis=-1)
104
 
105
  if reverse:
106
  # Reverse the audio array
107
+ st_state.augmented_audio = np.flip(st_state.augmented_audio)
108
 
109
  if change_speed:
110
  # Change the speed by resampling the audio
111
  new_sample_rate = int(sample_rate * speed_factor)
112
+ st_state.augmented_audio = scipy.signal.resample(st_state.augmented_audio, int(len(st_state.augmented_audio) * speed_factor))
113
 
114
  if pitch_shift:
115
  # Pitch shift using the Fourier shift method
116
  pitch_shift_factor = 2 ** (pitch_semitones / 12)
117
+ st_state.augmented_audio = scipy.signal.resample(st_state.augmented_audio, int(len(st_state.augmented_audio) / pitch_shift_factor))
118
 
119
  if add_reverb:
120
  # Apply reverb using the convolution method
121
  reverb_ir = scipy.signal.exponential(reverb_room_scale, reverb_damping, reverb_wet_only)
122
+ st_state.augmented_audio = scipy.signal.fftconvolve(st_state.augmented_audio, reverb_ir)
123
 
124
  # Play the processed audio
125
+ st.audio(st_state.augmented_audio, format="audio/wav", sample_rate=sample_rate, start_time=0)