fabiogra commited on
Commit
444aecb
1 Parent(s): 72ad181

fix: file_size_is_valid, add link to remove limits

Browse files
Files changed (3) hide show
  1. app/helpers.py +10 -8
  2. app/pages/Karaoke.py +21 -25
  3. app/pages/Separate.py +15 -4
app/helpers.py CHANGED
@@ -166,14 +166,16 @@ def st_local_audio(pathname, key):
166
  )
167
 
168
 
169
- def file_size_is_valid(response):
170
- file_size = response.headers.get("Content-Length")
171
- file_size = int(file_size)
172
- max_size_mb = int(os.environ["STREAMLIT_SERVER_MAX_UPLOAD_SIZE"])
173
- if max_size_mb and file_size > max_size_mb * 1024 * 1024:
174
- st.error(
175
- f"The file is too large to download. Maximum size allowed: {max_size_mb}MB. Duplicate this space to remove any limit."
176
- )
 
 
177
 
178
 
179
  def _get_files_to_not_delete():
 
166
  )
167
 
168
 
169
+ def file_size_is_valid(file_size):
170
+ if file_size is not None:
171
+ file_size = int(file_size)
172
+ max_size_mb = int(os.environ["STREAMLIT_SERVER_MAX_UPLOAD_SIZE"])
173
+ if max_size_mb and file_size > max_size_mb * 1024 * 1024:
174
+ st.error(
175
+ f"The file is too large to download. Maximum size allowed: {max_size_mb}MB.\nDuplicate this space to [remove any limit](https://github.com/fabiogra/moseca#are-there-any-limitations)."
176
+ )
177
+ return False
178
+ return True
179
 
180
 
181
  def _get_files_to_not_delete():
app/pages/Karaoke.py CHANGED
@@ -147,31 +147,27 @@ def body():
147
  sess.executed = False
148
  if sess.random_song is None:
149
  if not sess.executed:
150
- cols_spinners = st.columns([1, 2, 1])
151
- with cols_spinners[1]:
152
- with st.spinner(
153
- "Separating vocals from music, it could take a few minutes... Don't close this page!"
154
- ):
155
- sess.filename = download_audio_from_youtube(sess.url, in_path)
156
- if sess.filename is None:
157
- st.stop()
158
- sess.url = None
159
- filename = sess.filename
160
- song = load_audio_segment(
161
- in_path / filename, filename.split(".")[-1]
162
- )
163
- song.export(in_path / filename, format=filename.split(".")[-1])
164
- model, device = load_model(pretrained_model="baseline.pth")
165
- separate(
166
- input=in_path / filename,
167
- model=model,
168
- device=device,
169
- output_dir=out_path,
170
- only_no_vocals=True,
171
- )
172
- selected_value = None
173
- sess.last_dir = ".".join(sess.filename.split(".")[:-1])
174
- sess.executed = True
175
  else:
176
  sess.executed = True
177
 
 
147
  sess.executed = False
148
  if sess.random_song is None:
149
  if not sess.executed:
150
+ with st.spinner(
151
+ "Separating vocals from music, it could take a few minutes... Don't close this page!"
152
+ ):
153
+ sess.filename = download_audio_from_youtube(sess.url, in_path)
154
+ if sess.filename is None:
155
+ st.stop()
156
+ sess.url = None
157
+ filename = sess.filename
158
+ song = load_audio_segment(in_path / filename, filename.split(".")[-1])
159
+ song.export(in_path / filename, format=filename.split(".")[-1])
160
+ model, device = load_model(pretrained_model="baseline.pth")
161
+ separate(
162
+ input=in_path / filename,
163
+ model=model,
164
+ device=device,
165
+ output_dir=out_path,
166
+ only_no_vocals=True,
167
+ )
168
+ selected_value = None
169
+ sess.last_dir = ".".join(sess.filename.split(".")[:-1])
170
+ sess.executed = True
 
 
 
 
171
  else:
172
  sess.executed = True
173
 
app/pages/Separate.py CHANGED
@@ -161,14 +161,25 @@ def body():
161
  with st.spinner("Downloading audio..."):
162
  filename = url.split("/")[-1]
163
  response = requests.get(url, stream=True)
164
- if response.status_code == 200 and file_size_is_valid(response):
 
 
 
165
  with open(in_path / filename, "wb") as audio_file:
166
  for chunk in response.iter_content(chunk_size=1024):
167
  if chunk:
168
  audio_file.write(chunk)
 
 
 
 
 
 
169
  st_local_audio(in_path / filename, key="input_from_url")
170
  else:
171
- st.error("Failed to download audio file.")
 
 
172
  filename = None
173
 
174
  elif option == "Examples":
@@ -215,14 +226,14 @@ def body():
215
  max_value=n_secs,
216
  step=1,
217
  value=0,
218
- help=f"Maximum duration is {max_duration} seconds for this separation mode. Duplicate this space to remove any limit.",
219
  format="%d",
220
  )
221
  st.session_state.start_time = start_time
222
  end_time = min(start_time + max_duration, n_secs)
223
  song = song[start_time * 1000 : end_time * 1000]
224
  st.info(
225
- f"Audio source will be processed from {start_time} to {end_time} seconds. Duplicate this space to remove any limit.",
226
  icon="⏱",
227
  )
228
  else:
 
161
  with st.spinner("Downloading audio..."):
162
  filename = url.split("/")[-1]
163
  response = requests.get(url, stream=True)
164
+ if response.status_code == 200 and file_size_is_valid(
165
+ response.headers.get("Content-Length")
166
+ ):
167
+ file_size = 0
168
  with open(in_path / filename, "wb") as audio_file:
169
  for chunk in response.iter_content(chunk_size=1024):
170
  if chunk:
171
  audio_file.write(chunk)
172
+ file_size += len(chunk)
173
+ if not file_size_is_valid(file_size):
174
+ audio_file.close()
175
+ os.remove(in_path / filename)
176
+ filename = None
177
+ return
178
  st_local_audio(in_path / filename, key="input_from_url")
179
  else:
180
+ st.error(
181
+ "Failed to download audio file. Try to download it manually and upload it."
182
+ )
183
  filename = None
184
 
185
  elif option == "Examples":
 
226
  max_value=n_secs,
227
  step=1,
228
  value=0,
229
+ help=f"Maximum duration is {max_duration} seconds for this separation mode.\nDuplicate this space to [remove any limit](https://github.com/fabiogra/moseca#are-there-any-limitations).",
230
  format="%d",
231
  )
232
  st.session_state.start_time = start_time
233
  end_time = min(start_time + max_duration, n_secs)
234
  song = song[start_time * 1000 : end_time * 1000]
235
  st.info(
236
+ f"Audio source will be processed from {start_time} to {end_time} seconds.\nDuplicate this space to [remove any limit](https://github.com/fabiogra/moseca#are-there-any-limitations).",
237
  icon="⏱",
238
  )
239
  else: