Spaces:
Running
on
Zero
Running
on
Zero
File size: 9,064 Bytes
e877de6 3e13e03 e877de6 393fe50 3e13e03 e877de6 3e13e03 e877de6 b8606b0 e877de6 6096e62 393fe50 3e13e03 393fe50 6096e62 3e13e03 393fe50 3e13e03 a5c555e 3e13e03 6096e62 b8606b0 a5c555e 3e13e03 393fe50 3d15b33 393fe50 3d15b33 393fe50 e877de6 393fe50 3e13e03 e877de6 393fe50 3d15b33 393fe50 e877de6 393fe50 3d15b33 393fe50 3d15b33 393fe50 3d15b33 393fe50 e877de6 393fe50 e877de6 393fe50 e877de6 393fe50 e877de6 3e13e03 e877de6 393fe50 e877de6 393fe50 e877de6 393fe50 e877de6 393fe50 e877de6 |
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
"""
Reference audio player component using native Gradio components.
Provides the reference reciter audio player with multi-verse navigation.
Supports:
- Single verse: Just audio player
- 2+ verses: Audio player with navigation arrows
- 3+ verses: Audio player with arrows and slider
Uses native Gradio components (gr.Audio, gr.Slider, gr.Button) instead of
custom HTML/JavaScript for HF Spaces compatibility.
"""
from typing import Optional
import gradio as gr
from i8n import t
def create_reference_audio_components(initial_audio_path: str | None = None) -> dict:
"""
Create native Gradio components for reference audio playback.
Args:
initial_audio_path: Optional path to initial audio file. Must be set during
component creation (not after) to properly render the waveform.
Returns a dict with all components needed for reference audio:
- nav_row: Row containing navigation buttons and slider
- prev_btn: Previous verse button
- slider: Slider for verse navigation (embedded in nav row)
- next_btn: Next verse button
- audio_player: Native Gradio audio player with "Reference Reciter" label
- verse_index: State tracking current index
- verse_keys: State tracking list of verse keys
Returns:
Dict with component references
"""
with gr.Column(elem_classes=["reference-audio-section"]):
# Navigation row with slider (hidden for single verse)
# Layout: [◀ 15%] [slider 70%] [▶ 15%]
with gr.Row(visible=False, elem_classes=["ref-audio-nav-row"]) as nav_row:
prev_btn = gr.Button(
"◀",
scale=1,
min_width=40,
elem_classes=["ref-audio-nav-btn"]
)
verse_slider = gr.Slider(
minimum=0,
maximum=0,
step=1,
value=0,
label="",
show_label=False,
scale=5,
elem_id="ref-audio-verse-slider",
elem_classes=["ref-audio-slider"]
)
next_btn = gr.Button(
"▶",
scale=1,
min_width=40,
elem_classes=["ref-audio-nav-btn"]
)
# Audio player (always visible)
audio_player = gr.Audio(
value=initial_audio_path,
label=t("audio.reference_reciter"),
show_label=False,
type="filepath",
interactive=False,
elem_classes=["ref-audio-player"]
)
# State components
verse_index = gr.State(0)
verse_keys = gr.State([])
return {
"nav_row": nav_row,
"prev_btn": prev_btn,
"slider": verse_slider,
"next_btn": next_btn,
"audio_player": audio_player,
"verse_index": verse_index,
"verse_keys": verse_keys,
}
# ============================================================================
# Legacy functions (kept for backward compatibility during transition)
# ============================================================================
def get_placeholder_audio_html() -> str:
"""
Return placeholder HTML to prevent empty-state Gradio bug.
DEPRECATED: Use create_reference_audio_components() instead.
Kept for backward compatibility.
"""
return '<div id="ref-audio-container" style="display:none;"></div>'
def build_reference_audio_html(
data_uris: list[str] | str | None = None,
urls: list[str] | str | None = None,
verse_keys: list[str] | None = None,
from_verse: int | None = None,
to_verse: int | None = None,
chapter: int | None = None,
label: str = "Reference Reciter",
lazy_load: bool = False
) -> str:
"""
Build HTML for audio player with multi-verse navigation.
DEPRECATED: Use create_reference_audio_components() instead.
Kept for backward compatibility during transition.
"""
# Determine which source to use (prefer urls for lazy loading)
sources = urls if urls is not None else data_uris
# Handle backward compatibility: convert single string to list
if isinstance(sources, str):
if not sources:
return ""
sources = [sources]
if from_verse is None:
from_verse = 1
if to_verse is None:
to_verse = 1
# In lazy_load mode, use verse_keys to determine verse count
if lazy_load and verse_keys:
verse_count = len(verse_keys)
elif sources:
verse_count = len(sources)
else:
return ""
# Ensure we have at least one source for the first verse
if not sources or len(sources) == 0:
return ""
# Determine verse range
if from_verse is None or to_verse is None:
from_verse = 1
to_verse = verse_count
# Determine what UI elements to show
show_navigation = verse_count >= 2
show_slider = verse_count >= 3
show_indicator = verse_count >= 2
# Serialize sources (URLs or data URIs) to JSON for JavaScript
import json
sources_json = json.dumps(sources)
verse_keys_json = json.dumps(verse_keys) if verse_keys else "[]"
# CSS styles
container_style = '''
position: relative;
padding: 12px 14px;
border: 1px solid var(--border-color-primary, #e5e7eb);
border-radius: 10px;
background: var(--background-fill-secondary, #f3f4f6);
'''
header_style = '''
display: flex;
justify-content: flex-start;
align-items: center;
margin-bottom: 8px;
'''
title_style = '''
font-weight: 600;
font-size: 14px;
color: var(--body-text-color, inherit);
'''
verse_indicator_style = '''
position: absolute;
top: 8px;
right: 14px;
background: #fb8c00;
color: white;
padding: 4px 10px;
border-radius: 6px;
font-size: 12px;
font-weight: 600;
'''
slider_row_style = '''
margin-bottom: 10px;
padding: 0 30px;
'''
slider_style = '''
width: 100%;
height: 6px;
cursor: pointer;
accent-color: #fb8c00;
'''
player_row_style = '''
display: flex;
align-items: center;
gap: 12px;
'''
nav_button_style = '''
background: var(--button-secondary-background-fill, #f3f4f6);
border: 1px solid var(--border-color-primary, #e5e7eb);
border-radius: 6px;
padding: 8px 12px;
cursor: pointer;
font-size: 16px;
font-weight: 600;
transition: background 0.2s;
color: var(--body-text-color, inherit);
'''
audio_style = '''
flex: 1;
height: 36px;
min-width: 0;
'''
# Build final HTML
html = f'''
<div id="ref-audio-container"
style="{container_style}"
data-verse-count="{verse_count}"
data-current-verse="0"
data-uris='{sources_json}'
data-verse-keys='{verse_keys_json}'
data-from-verse="{from_verse}"
data-chapter="{chapter or 1}"
data-lazy-load="{str(lazy_load).lower()}">
<div style="{header_style}">
<span style="{title_style}">{label}</span>
</div>
{'<div style="' + verse_indicator_style + '" id="verse-indicator">Verse ' + str(from_verse) + '</div>' if show_indicator else ''}
{'<div style="' + slider_row_style + '"><input type="range" id="verse-slider" min="0" max="' + str(verse_count - 1) + '" value="0" style="' + slider_style + '"></div>' if show_slider else ''}
<div style="{player_row_style}">
{'<button class="ref-audio-nav-btn" data-direction="-1" style="' + nav_button_style + '">◀</button>' if show_navigation else ''}
<audio id="ref-audio-player" controls style="{audio_style}">
<source src="{sources[0]}" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
{'<button class="ref-audio-nav-btn" data-direction="1" style="' + nav_button_style + '">▶</button>' if show_navigation else ''}
</div>
</div>
'''
return html
def create_reference_audio_section(init_audio_html: str) -> dict:
"""
Create reference audio section with just the audio player.
DEPRECATED: Use create_reference_audio_components() instead.
Kept for backward compatibility during transition.
"""
# Main audio player row
with gr.Row(equal_height=True) as audio_row:
with gr.Column(scale=1, min_width=80, visible=False) as dropdown_col:
dropdown = gr.Dropdown(visible=False)
with gr.Column(scale=4) as audio_col:
audio_html = gr.HTML(
value=init_audio_html,
elem_id="reference-audio-container"
)
return {
"audio_row": audio_row,
"audio_html": audio_html,
"dropdown_col": dropdown_col,
"dropdown": dropdown,
}
|