Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import speech_recognition as sr
|
3 |
+
from pydub import AudioSegment
|
4 |
+
|
5 |
+
# Title and description
|
6 |
+
st.title("Audio Trimming App")
|
7 |
+
st.write("Upload an audio file and enter the desired text. The app will trim the audio to match the specified text.")
|
8 |
+
|
9 |
+
# Upload an audio file
|
10 |
+
uploaded_audio = st.file_uploader("Upload an audio file", type=["wav", "mp3"])
|
11 |
+
|
12 |
+
# Input text box
|
13 |
+
input_text = st.text_area("Enter the desired text")
|
14 |
+
|
15 |
+
# Function to trim audio
|
16 |
+
def trim_audio(input_audio, input_text):
|
17 |
+
if not input_audio:
|
18 |
+
return
|
19 |
+
|
20 |
+
# Load the audio file and recognize text
|
21 |
+
recognizer = sr.Recognizer()
|
22 |
+
with sr.AudioFile(input_audio) as source:
|
23 |
+
audio = recognizer.record(source)
|
24 |
+
audio_text = recognizer.recognize_google(audio)
|
25 |
+
|
26 |
+
# Process the audio and create a trimmed audio
|
27 |
+
trimmed_audio = AudioSegment.silent(duration=0)
|
28 |
+
text_to_match = input_text.lower()
|
29 |
+
audio_text = audio_text.lower()
|
30 |
+
|
31 |
+
for word in audio_text.split():
|
32 |
+
if word in text_to_match:
|
33 |
+
trimmed_audio += AudioSegment.from_file(input_audio)
|
34 |
+
|
35 |
+
st.audio(trimmed_audio.export(), format="audio/mp3")
|
36 |
+
|
37 |
+
# Process button
|
38 |
+
if st.button("Process"):
|
39 |
+
trim_audio(uploaded_audio, input_text)
|