File size: 1,627 Bytes
84a6095
ed34ab2
 
84a6095
ed34ab2
84a6095
 
ed34ab2
 
 
 
 
 
 
 
 
 
 
 
 
 
84a6095
ed34ab2
 
 
 
84a6095
ed34ab2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84a6095
 
ed34ab2
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
57
58
59
60
import gradio as gr
from pathlib import Path
import os

directory = "./audios"


def scan_audio_folder():
    """Scan directory for audio files and return list of paths."""
    supported_formats = ['.mp3', '.wav', '.m4a']
    audio_files = []
    
    try:
        for root, _, files in os.walk(directory):
            for file in files:
                if os.path.splitext(file)[1].lower() in supported_formats:
                    audio_files.append(os.path.join(root, file))
        return sorted(audio_files)
    except Exception as e:
        print(f"Error scanning directory: {str(e)}")
        return []

def update_audio_list(folder_path):
    """Update audio dropdown based on selected folder."""
    audio_files = scan_audio_folder(folder_path)
    return gr.Dropdown.update(choices=[Path(f).name for f in audio_files])

# Initialize the app
with gr.Blocks() as demo:
    # Input components
    folder_input = gr.Textbox(label="Audio Folder Path")
    
    # Dropdown for selecting audio files
    audio_dropdown = gr.Dropdown(label="Select Audio File")
    
    # Audio player component
    audio_player = gr.Audio(label="Audio Player")
    
    # Events
    folder_input.change(
        fn=update_audio_list,
        inputs=[folder_input],
        outputs=[audio_dropdown]
    )
    
    audio_dropdown.change(
        fn=lambda x, f: os.path.join(f, x),
        inputs=[audio_dropdown, folder_input],
        outputs=[audio_player]
    )
    
    # Layout
    demo.append(gr.Column([
        folder_input,
        audio_dropdown,
        audio_player
    ]))

if __name__ == "__main__":
    demo.launch(debug=True)