Spaces:
Sleeping
Sleeping
fix path
Browse files- src/streamlit_app.py +35 -30
src/streamlit_app.py
CHANGED
@@ -2,12 +2,32 @@ import os
|
|
2 |
# Fix Streamlit permission issues by redirecting HOME and XDG_CONFIG_HOME to writable path
|
3 |
cwd = os.getcwd()
|
4 |
os.environ.setdefault('XDG_CONFIG_HOME', cwd)
|
5 |
-
# Some Streamlit operations use HOME; override HOME to cwd
|
6 |
os.environ.setdefault('HOME', cwd)
|
7 |
|
8 |
import streamlit as st
|
|
|
9 |
|
10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
def main():
|
12 |
# Configure page for better mobile experience; sidebar collapses nicely on small screens
|
13 |
st.set_page_config(page_title="TTS Samples Player", layout="wide")
|
@@ -16,37 +36,22 @@ def main():
|
|
16 |
"""
|
17 |
Select a TTS engine provider and a voice to play the corresponding WAV sample.
|
18 |
Use the sidebar to select provider and voice for better mobile experience.
|
19 |
-
Ensure your `tts_smaples/` folder with subfolders (e.g., `azure/`, `google/`, etc.) is present in the repo.
|
20 |
"""
|
21 |
)
|
22 |
-
|
23 |
-
BASE_DIR
|
24 |
-
|
25 |
-
@st.cache_data
|
26 |
-
def get_providers_and_voices(base_dir: str):
|
27 |
-
"""
|
28 |
-
Scan the base_dir for subfolders (providers) and .wav files under each.
|
29 |
-
Returns a dict: { provider_name: [voice_filename1.wav, ...], ... }
|
30 |
-
"""
|
31 |
-
providers = {}
|
32 |
-
if not os.path.isdir(base_dir):
|
33 |
-
return providers
|
34 |
-
for provider in sorted(os.listdir(base_dir)):
|
35 |
-
prov_path = os.path.join(base_dir, provider)
|
36 |
-
if os.path.isdir(prov_path):
|
37 |
-
voices = [f for f in sorted(os.listdir(prov_path)) if f.lower().endswith(".wav")]
|
38 |
-
if voices:
|
39 |
-
providers[provider] = voices
|
40 |
-
return providers
|
41 |
-
|
42 |
providers = get_providers_and_voices(BASE_DIR)
|
43 |
if not providers:
|
44 |
st.warning(f"No providers found in `{BASE_DIR}`. Please ensure the folder exists with subfolders containing .wav files.")
|
|
|
|
|
|
|
|
|
45 |
return
|
46 |
|
47 |
# Sidebar for selection (better for mobile)
|
48 |
st.sidebar.header("Select TTS Sample")
|
49 |
-
provider_list =
|
50 |
provider = st.sidebar.selectbox("Provider", options=provider_list)
|
51 |
|
52 |
voices = providers.get(provider, [])
|
@@ -58,10 +63,10 @@ def main():
|
|
58 |
|
59 |
# Main area shows playback
|
60 |
st.markdown(f"### Playing: **{provider} / {voice}**")
|
61 |
-
file_path =
|
62 |
-
if
|
63 |
try:
|
64 |
-
audio_bytes =
|
65 |
st.audio(audio_bytes, format="audio/wav")
|
66 |
except Exception as e:
|
67 |
st.error(f"Error loading audio: {e}")
|
@@ -74,14 +79,14 @@ def main():
|
|
74 |
for prov, voice_files in providers.items():
|
75 |
with st.expander(prov, expanded=False):
|
76 |
for vf in voice_files:
|
77 |
-
path =
|
78 |
btn_key = f"play_{prov}_{vf}"
|
79 |
cols = st.columns([3,1])
|
80 |
cols[0].write(vf)
|
81 |
if cols[1].button("Play", key=btn_key):
|
82 |
-
if
|
83 |
try:
|
84 |
-
data =
|
85 |
st.audio(data, format="audio/wav")
|
86 |
st.write(f"**Playing**: `{prov}/{vf}`")
|
87 |
except Exception as ex:
|
|
|
2 |
# Fix Streamlit permission issues by redirecting HOME and XDG_CONFIG_HOME to writable path
|
3 |
cwd = os.getcwd()
|
4 |
os.environ.setdefault('XDG_CONFIG_HOME', cwd)
|
|
|
5 |
os.environ.setdefault('HOME', cwd)
|
6 |
|
7 |
import streamlit as st
|
8 |
+
from pathlib import Path
|
9 |
|
10 |
+
# Determine BASE_DIR relative to this script's location
|
11 |
+
SCRIPT_DIR = Path(__file__).resolve().parent
|
12 |
+
# Assuming project structure: project_root/src/streamlit_app.py and project_root/tts_smaples
|
13 |
+
BASE_DIR = SCRIPT_DIR.parent / "tts_smaples"
|
14 |
+
|
15 |
+
@st.cache_data
|
16 |
+
def get_providers_and_voices(base_dir: Path):
|
17 |
+
"""
|
18 |
+
Scan the base_dir for subfolders (providers) and .wav files under each.
|
19 |
+
Returns a dict: { provider_name: [voice_filename1.wav, ...], ... }
|
20 |
+
"""
|
21 |
+
providers = {}
|
22 |
+
if not base_dir.is_dir():
|
23 |
+
return providers
|
24 |
+
for provider in sorted([p for p in base_dir.iterdir() if p.is_dir()]):
|
25 |
+
voices = sorted([f.name for f in provider.iterdir() if f.is_file() and f.suffix.lower() == ".wav"] )
|
26 |
+
if voices:
|
27 |
+
providers[provider.name] = voices
|
28 |
+
return providers
|
29 |
+
|
30 |
+
# Main app
|
31 |
def main():
|
32 |
# Configure page for better mobile experience; sidebar collapses nicely on small screens
|
33 |
st.set_page_config(page_title="TTS Samples Player", layout="wide")
|
|
|
36 |
"""
|
37 |
Select a TTS engine provider and a voice to play the corresponding WAV sample.
|
38 |
Use the sidebar to select provider and voice for better mobile experience.
|
|
|
39 |
"""
|
40 |
)
|
41 |
+
# Debugging: show BASE_DIR path
|
42 |
+
st.sidebar.write(f"BASE_DIR: `{BASE_DIR}`")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
providers = get_providers_and_voices(BASE_DIR)
|
44 |
if not providers:
|
45 |
st.warning(f"No providers found in `{BASE_DIR}`. Please ensure the folder exists with subfolders containing .wav files.")
|
46 |
+
# Optionally show what exists at parent: for debugging
|
47 |
+
parent = BASE_DIR.parent
|
48 |
+
if parent.exists():
|
49 |
+
st.info(f"Contents of parent directory `{parent}`: {sorted([p.name for p in parent.iterdir()])}")
|
50 |
return
|
51 |
|
52 |
# Sidebar for selection (better for mobile)
|
53 |
st.sidebar.header("Select TTS Sample")
|
54 |
+
provider_list = sorted(providers.keys())
|
55 |
provider = st.sidebar.selectbox("Provider", options=provider_list)
|
56 |
|
57 |
voices = providers.get(provider, [])
|
|
|
63 |
|
64 |
# Main area shows playback
|
65 |
st.markdown(f"### Playing: **{provider} / {voice}**")
|
66 |
+
file_path = BASE_DIR / provider / voice
|
67 |
+
if file_path.is_file():
|
68 |
try:
|
69 |
+
audio_bytes = file_path.read_bytes()
|
70 |
st.audio(audio_bytes, format="audio/wav")
|
71 |
except Exception as e:
|
72 |
st.error(f"Error loading audio: {e}")
|
|
|
79 |
for prov, voice_files in providers.items():
|
80 |
with st.expander(prov, expanded=False):
|
81 |
for vf in voice_files:
|
82 |
+
path = BASE_DIR / prov / vf
|
83 |
btn_key = f"play_{prov}_{vf}"
|
84 |
cols = st.columns([3,1])
|
85 |
cols[0].write(vf)
|
86 |
if cols[1].button("Play", key=btn_key):
|
87 |
+
if path.is_file():
|
88 |
try:
|
89 |
+
data = path.read_bytes()
|
90 |
st.audio(data, format="audio/wav")
|
91 |
st.write(f"**Playing**: `{prov}/{vf}`")
|
92 |
except Exception as ex:
|