imrafarafarafa commited on
Commit
4c12039
·
verified ·
1 Parent(s): f826054

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """HuggingFace Spaces app to host the stem conditioned results overview."""
2
+ import os
3
+ import subprocess
4
+ import sys
5
+ import gradio as gr
6
+ from pathlib import Path
7
+
8
+ # Determine project root (works whether app.py is in root or subdirectory)
9
+ # Try parent.parent first (if in huggingface/), then just parent (if in root)
10
+ app_dir = Path(__file__).parent
11
+ parent_dir = app_dir.parent
12
+ PROJECT_ROOT = parent_dir if (parent_dir / "stem_cond_overview.html").exists() else app_dir
13
+
14
+ # Path to the HTML file
15
+ HTML_FILE = PROJECT_ROOT / "stem_cond_overview.html"
16
+
17
+
18
+ def download_audio_files():
19
+ """Download audio files on startup."""
20
+ print("Checking if audio files need to be downloaded...")
21
+ audio_dir = PROJECT_ROOT / "data" / "fullmix_mp3s"
22
+
23
+ # Check if download is needed
24
+ if audio_dir.exists() and len(list(audio_dir.glob("*.mp3"))) > 0:
25
+ print(f"Found {len(list(audio_dir.glob('*.mp3')))} existing audio files.")
26
+ return
27
+
28
+ print("No audio files found. Starting download...")
29
+ try:
30
+ # Run the download script
31
+ script_path = Path(__file__).parent / "download_audio.py"
32
+ result = subprocess.run(
33
+ [sys.executable, str(script_path)],
34
+ capture_output=True,
35
+ text=True,
36
+ timeout=3600 # 1 hour timeout
37
+ )
38
+ print("Download script output:")
39
+ print(result.stdout)
40
+ if result.stderr:
41
+ print("Download script errors:")
42
+ print(result.stderr)
43
+ except subprocess.TimeoutExpired:
44
+ print("Download script timed out after 1 hour")
45
+ except Exception as e:
46
+ print(f"Error running download script: {e}")
47
+
48
+
49
+ def render_html():
50
+ """Load and return the HTML content."""
51
+ if HTML_FILE.exists():
52
+ with open(HTML_FILE, 'r', encoding='utf-8') as f:
53
+ return f.read()
54
+ return "<h1>HTML file not found</h1><p>Please generate the overview first.</p>"
55
+
56
+
57
+ # Create Gradio interface
58
+ def create_interface():
59
+ """Create the Gradio interface."""
60
+ html_content = render_html()
61
+
62
+ # Create a Gradio HTML component
63
+ demo = gr.HTML(
64
+ value=html_content,
65
+ label="Stem Conditioned Results Overview"
66
+ )
67
+
68
+ return demo
69
+
70
+
71
+ if __name__ == "__main__":
72
+ # Download audio files on startup
73
+ download_audio_files()
74
+
75
+ # Launch the interface
76
+ demo = create_interface()
77
+ demo.launch(server_name="0.0.0.0", server_port=7860)
78
+