Trim / app.py
Rhueue's picture
Update app.py
14a227f
raw
history blame
1.84 kB
import streamlit as st
import speech_recognition as sr
from pydub import AudioSegment
import tempfile
import os
# Title and description
st.title("Arabic Audio Trimming App")
st.write("Upload an Arabic audio file and enter the desired text. The app will trim the audio to match the specified text.")
# Upload an audio file
uploaded_audio = st.file_uploader("Upload an Arabic audio file", type=["wav", "mp3"])
# Input text box
input_text = st.text_area("Enter the desired Arabic text")
# Function to trim audio
def trim_audio(input_audio, input_text):
if not input_audio:
return
# Create a temporary directory to store audio files
temp_dir = tempfile.TemporaryDirectory()
try:
# Save uploaded audio to a temporary file
temp_audio_path = os.path.join(temp_dir.name, "temp_audio.wav")
with open(temp_audio_path, "wb") as audio_file:
audio_file.write(input_audio.read())
# Initialize the recognizer with the Arabic language
recognizer = sr.Recognizer(language="ar-AE")
# Load the audio file and recognize text
with sr.AudioFile(temp_audio_path) as source:
audio = recognizer.record(source)
audio_text = recognizer.recognize_google(audio, language="ar-AE")
# Process the audio and create a trimmed audio
trimmed_audio = AudioSegment.silent(duration=0)
text_to_match = input_text.lower()
audio_text = audio_text.lower()
for word in audio_text.split():
if word in text_to_match:
trimmed_audio += AudioSegment.from_file(temp_audio_path, format="wav")
st.audio(trimmed_audio.export(format="wav"), format="audio/wav")
finally:
temp_dir.cleanup()
# Process button
if st.button("Process"):
trim_audio(uploaded_audio, input_text)