Ivan000 commited on
Commit
930e37b
·
verified ·
1 Parent(s): 8e4d19f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -16
app.py CHANGED
@@ -4,48 +4,49 @@ from pydub import AudioSegment
4
  from pydub.effects import compress_dynamic_range
5
  from pydub.silence import split_on_silence
6
 
7
- def remove_silence(audio_file, silence_thresh, min_silence_len):
8
- # Загрузка аудиофайла
9
  audio = AudioSegment.from_file(audio_file)
10
 
11
- # Применение компрессии к аудиофайлу
12
- compressed_audio = compress_dynamic_range(audio, threshold=-20.0)
13
 
14
- # Разделение аудио на части без тишины
15
  chunks = split_on_silence(compressed_audio,
16
- # Минимальная длина тишины (в миллисекундах)
17
  min_silence_len=min_silence_len,
18
 
19
- # Граница тишины (в dBFS)
20
  silence_thresh=silence_thresh
21
  )
22
 
23
- # Проверка, найдены ли какие-либо куски
24
  if not chunks:
25
- return audio_file # Возвращаем исходный файл, если не найдены куски тишины
26
 
27
- # Объединение кусков без тишины
28
  audio_without_silence = sum(chunks)
29
 
30
- # Экспорт аудиофайла без тишины
31
  output_file = "audio_without_silence.wav"
32
  audio_without_silence.export(output_file, format="wav")
33
 
34
  return output_file
35
 
36
- # Создание интерфейса Gradio
37
  iface = gr.Interface(
38
  fn=remove_silence,
39
  inputs=[
40
  gr.Audio(sources=["upload", "microphone"], type="filepath", label="Upload or Record Audio File"),
41
- gr.Slider(minimum=-100, maximum=0, step=1, value=-16, label="Silence Threshold (dBFS)"),
42
- gr.Slider(minimum=10, maximum=5000, step=10, value=500, label="Minimum Silence Length (ms)")
 
43
  ],
44
  outputs=gr.Audio(type="filepath", label="Audio Without Silence"),
45
  title="Remove Silence from Audio",
46
- description="Upload an audio file or record audio and get the audio with silence removed. Adjust the silence threshold and minimum silence length."
47
  )
48
 
49
- # Запуск приложения
50
  if __name__ == "__main__":
51
  iface.launch()
 
4
  from pydub.effects import compress_dynamic_range
5
  from pydub.silence import split_on_silence
6
 
7
+ def remove_silence(audio_file, silence_thresh, min_silence_len, compression_threshold):
8
+ # Load the audio file
9
  audio = AudioSegment.from_file(audio_file)
10
 
11
+ # Apply compression to the audio file
12
+ compressed_audio = compress_dynamic_range(audio, threshold=compression_threshold)
13
 
14
+ # Split the audio into parts without silence
15
  chunks = split_on_silence(compressed_audio,
16
+ # Minimum silence length (in milliseconds)
17
  min_silence_len=min_silence_len,
18
 
19
+ # Silence threshold (in dBFS)
20
  silence_thresh=silence_thresh
21
  )
22
 
23
+ # Check if any chunks were found
24
  if not chunks:
25
+ return audio_file # Return the original file if no silence chunks were found
26
 
27
+ # Combine chunks without silence
28
  audio_without_silence = sum(chunks)
29
 
30
+ # Export the audio file without silence
31
  output_file = "audio_without_silence.wav"
32
  audio_without_silence.export(output_file, format="wav")
33
 
34
  return output_file
35
 
36
+ # Create Gradio interface
37
  iface = gr.Interface(
38
  fn=remove_silence,
39
  inputs=[
40
  gr.Audio(sources=["upload", "microphone"], type="filepath", label="Upload or Record Audio File"),
41
+ gr.Slider(minimum=-100, maximum=0, step=1, value=-16, label="Silence Threshold (dBFS)", description="The threshold in decibels (dBFS) below which audio is considered silence."),
42
+ gr.Slider(minimum=10, maximum=5000, step=10, value=500, label="Minimum Silence Length (ms)", description="The minimum length of silence (in milliseconds) to be detected."),
43
+ gr.Slider(minimum=-100, maximum=0, step=1, value=-20, label="Compression Threshold (dBFS)", description="The threshold in decibels (dBFS) for audio compression. Lower values compress more.")
44
  ],
45
  outputs=gr.Audio(type="filepath", label="Audio Without Silence"),
46
  title="Remove Silence from Audio",
47
+ description="Upload an audio file or record audio and get the audio with silence removed. Adjust the silence threshold, minimum silence length, and compression threshold."
48
  )
49
 
50
+ # Run the app
51
  if __name__ == "__main__":
52
  iface.launch()