|
import streamlit as st |
|
import speech_recognition as sr |
|
from pydub import AudioSegment |
|
import tempfile |
|
import os |
|
|
|
|
|
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.") |
|
|
|
|
|
uploaded_audio = st.file_uploader("Upload an Arabic audio file", type=["wav", "mp3"]) |
|
|
|
|
|
input_text = st.text_area("Enter the desired Arabic text") |
|
|
|
|
|
def trim_audio(input_audio, input_text): |
|
if not input_audio: |
|
return |
|
|
|
|
|
temp_dir = tempfile.TemporaryDirectory() |
|
|
|
try: |
|
|
|
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()) |
|
|
|
|
|
recognizer = sr.Recognizer(language="ar-AE") |
|
|
|
|
|
with sr.AudioFile(temp_audio_path) as source: |
|
audio = recognizer.record(source) |
|
audio_text = recognizer.recognize_google(audio, language="ar-AE") |
|
|
|
|
|
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() |
|
|
|
|
|
if st.button("Process"): |
|
trim_audio(uploaded_audio, input_text) |
|
|