File size: 1,837 Bytes
5034d88
 
 
2a1a0fd
 
5034d88
 
2a1a0fd
 
5034d88
 
2a1a0fd
5034d88
 
2a1a0fd
5034d88
 
 
 
 
 
2a1a0fd
 
5034d88
2a1a0fd
 
 
 
 
5034d88
14a227f
 
 
2a1a0fd
 
14a227f
2a1a0fd
5034d88
2a1a0fd
 
 
 
 
 
 
14a227f
2a1a0fd
14a227f
2a1a0fd
 
 
5034d88
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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)