File size: 2,416 Bytes
4c12039
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""HuggingFace Spaces app to host the stem conditioned results overview."""
import os
import subprocess
import sys
import gradio as gr
from pathlib import Path

# Determine project root (works whether app.py is in root or subdirectory)
# Try parent.parent first (if in huggingface/), then just parent (if in root)
app_dir = Path(__file__).parent
parent_dir = app_dir.parent
PROJECT_ROOT = parent_dir if (parent_dir / "stem_cond_overview.html").exists() else app_dir

# Path to the HTML file
HTML_FILE = PROJECT_ROOT / "stem_cond_overview.html"


def download_audio_files():
    """Download audio files on startup."""
    print("Checking if audio files need to be downloaded...")
    audio_dir = PROJECT_ROOT / "data" / "fullmix_mp3s"
    
    # Check if download is needed
    if audio_dir.exists() and len(list(audio_dir.glob("*.mp3"))) > 0:
        print(f"Found {len(list(audio_dir.glob('*.mp3')))} existing audio files.")
        return
    
    print("No audio files found. Starting download...")
    try:
        # Run the download script
        script_path = Path(__file__).parent / "download_audio.py"
        result = subprocess.run(
            [sys.executable, str(script_path)],
            capture_output=True,
            text=True,
            timeout=3600  # 1 hour timeout
        )
        print("Download script output:")
        print(result.stdout)
        if result.stderr:
            print("Download script errors:")
            print(result.stderr)
    except subprocess.TimeoutExpired:
        print("Download script timed out after 1 hour")
    except Exception as e:
        print(f"Error running download script: {e}")


def render_html():
    """Load and return the HTML content."""
    if HTML_FILE.exists():
        with open(HTML_FILE, 'r', encoding='utf-8') as f:
            return f.read()
    return "<h1>HTML file not found</h1><p>Please generate the overview first.</p>"


# Create Gradio interface
def create_interface():
    """Create the Gradio interface."""
    html_content = render_html()
    
    # Create a Gradio HTML component
    demo = gr.HTML(
        value=html_content,
        label="Stem Conditioned Results Overview"
    )
    
    return demo


if __name__ == "__main__":
    # Download audio files on startup
    download_audio_files()
    
    # Launch the interface
    demo = create_interface()
    demo.launch(server_name="0.0.0.0", server_port=7860)