Rhueue commited on
Commit
83db5aa
·
1 Parent(s): 14a227f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -32
app.py CHANGED
@@ -1,54 +1,42 @@
1
  import streamlit as st
2
  import speech_recognition as sr
3
  from pydub import AudioSegment
4
- import tempfile
5
- import os
6
 
7
  # Title and description
8
- st.title("Arabic Audio Trimming App")
9
- st.write("Upload an Arabic audio file and enter the desired text. The app will trim the audio to match the specified text.")
10
 
11
  # Upload an audio file
12
- uploaded_audio = st.file_uploader("Upload an Arabic audio file", type=["wav", "mp3"])
13
 
14
  # Input text box
15
- input_text = st.text_area("Enter the desired Arabic text")
16
 
17
  # Function to trim audio
18
  def trim_audio(input_audio, input_text):
19
  if not input_audio:
20
  return
21
 
22
- # Create a temporary directory to store audio files
23
- temp_dir = tempfile.TemporaryDirectory()
 
 
 
24
 
25
- try:
26
- # Save uploaded audio to a temporary file
27
- temp_audio_path = os.path.join(temp_dir.name, "temp_audio.wav")
28
- with open(temp_audio_path, "wb") as audio_file:
29
- audio_file.write(input_audio.read())
30
 
31
- # Initialize the recognizer with the Arabic language
32
- recognizer = sr.Recognizer(language="ar-AE")
 
33
 
34
- # Load the audio file and recognize text
35
- with sr.AudioFile(temp_audio_path) as source:
36
- audio = recognizer.record(source)
37
- audio_text = recognizer.recognize_google(audio, language="ar-AE")
38
 
39
- # Process the audio and create a trimmed audio
40
- trimmed_audio = AudioSegment.silent(duration=0)
41
- text_to_match = input_text.lower()
42
- audio_text = audio_text.lower()
43
-
44
- for word in audio_text.split():
45
- if word in text_to_match:
46
- trimmed_audio += AudioSegment.from_file(temp_audio_path, format="wav")
47
-
48
- st.audio(trimmed_audio.export(format="wav"), format="audio/wav")
49
-
50
- finally:
51
- temp_dir.cleanup()
52
 
53
  # Process button
54
  if st.button("Process"):
 
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
+ # Export the trimmed audio to a file
36
+ trimmed_audio.export("trimmed_audio.mp3", format="mp3")
 
 
37
 
38
+ # Display the trimmed audio
39
+ st.audio("trimmed_audio.mp3", format="audio/mp3")
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  # Process button
42
  if st.button("Process"):