Rhueue commited on
Commit
bd01872
·
1 Parent(s): 69abee7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -63
app.py CHANGED
@@ -1,69 +1,35 @@
1
  import streamlit as st
2
- import speech_recognition as sr
3
- import os
4
  from pydub import AudioSegment
5
- from tempfile import NamedTemporaryFile
6
 
7
- def transcribe_audio(audio_file):
8
- recognizer = sr.Recognizer()
9
 
10
- with sr.AudioFile(audio_file) as source:
11
- audio = recognizer.record(source)
12
-
13
- try:
14
- transcription = recognizer.recognize_google(audio)
15
- return transcription
16
- except sr.UnknownValueError:
17
- return ""
18
-
19
- def filter_audio(audio_file, input_text):
20
- transcribed_text = transcribe_audio(audio_file)
21
- transcribed_words = transcribed_text.split()
22
- input_words = input_text.split()
23
-
24
- matching_words = [word for word in transcribed_words if word in input_words]
25
-
26
- return " ".join(matching_words)
27
-
28
- def create_filtered_audio(original_audio, matching_words):
29
- transcribed_text = filter_audio(original_audio, matching_words)
30
-
31
- original_audio = AudioSegment.from_file(original_audio, format="wav")
32
-
33
- matching_segments = []
34
- matching_words = matching_words.split()
35
-
36
- for word in matching_words:
37
- word_start = transcribed_text.index(word)
38
- word_end = word_start + len(word)
39
- matching_segments.append(original_audio[word_start * 1000 : word_end * 1000])
40
-
41
- filtered_audio = AudioSegment.silent(duration=len(original_audio))
42
- for segment in matching_segments:
43
- filtered_audio = filtered_audio.overlay(segment)
44
-
45
- with NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio_file:
46
- filtered_audio.export(temp_audio_file.name, format="wav")
47
-
48
- return temp_audio_file.name
49
-
50
- st.title("Audio Word Filter and Regeneration")
51
-
52
- uploaded_audio = st.file_uploader("Upload an audio file", type=["wav", "mp3", "ogg"])
53
- input_text = st.text_area("Enter input text")
54
 
55
  if uploaded_audio is not None:
56
- st.audio(uploaded_audio, format="audio/wav")
57
-
58
- if st.button("Filter Audio"):
59
- if uploaded_audio is not None:
60
- matching_words = filter_audio(uploaded_audio, input_text)
61
- st.subheader("Matching Words:")
62
- st.write(matching_words)
63
- filtered_audio = create_filtered_audio(uploaded_audio, matching_words)
64
- st.subheader("Filtered Audio:")
65
- st.audio(filtered_audio, format="audio/wav")
66
-
67
- st.write(
68
- "Note: This app transcribes the uploaded audio, filters it to retain only the words mentioned in the input text, and generates filtered audio without altering the original audio."
69
- )
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import noisereduce as nr
 
3
  from pydub import AudioSegment
 
4
 
5
+ # Define a Streamlit app
6
+ st.title("Audio Processing App")
7
 
8
+ # Upload the input audio file
9
+ uploaded_audio = st.file_uploader("Upload an audio file", type=["mp3", "wav"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  if uploaded_audio is not None:
12
+ # Clean the input audio using noisereduce
13
+ # You may need to adjust the parameters for your specific audio
14
+ st.write("Cleaning audio...")
15
+ # Load the audio file
16
+ audio = AudioSegment.from_file(uploaded_audio)
17
+ # Convert to numpy array for processing
18
+ audio_data = audio.get_array_of_samples()
19
+ # Apply noise reduction
20
+ reduced_audio = nr.reduce_noise(y=audio_data, sr=audio.frame_rate)
21
+
22
+ # Slow down the audio
23
+ st.write("Slowing down audio...")
24
+ slowed_audio = audio.speedup(playback_speed=0.7) # Adjust the speed factor as needed
25
+
26
+ # Generate a new audio file
27
+ st.write("Generating output audio...")
28
+ slowed_audio.export("output_audio.wav", format="wav")
29
+
30
+ # Provide the download link for the cleaned and slowed audio
31
+ st.audio("output_audio.wav")
32
+
33
+ # Run the Streamlit app
34
+ if __name__ == "__main__":
35
+ st.write("Upload an audio file to get started.")