Spaces:
Sleeping
Sleeping
Create utils/ui_components.py
Browse files- utils/ui_components.py +125 -0
utils/ui_components.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Reusable UI components
|
| 3 |
+
"""
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from typing import List, Dict, Any
|
| 6 |
+
import plotly.graph_objects as go
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
class UIComponents:
|
| 10 |
+
@staticmethod
|
| 11 |
+
def create_audio_player(label: str = "Audio Player") -> gr.Audio:
|
| 12 |
+
"""Create styled audio player"""
|
| 13 |
+
return gr.Audio(
|
| 14 |
+
label=label,
|
| 15 |
+
type="filepath",
|
| 16 |
+
show_download_button=True,
|
| 17 |
+
show_share_button=True
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
@staticmethod
|
| 21 |
+
def create_model_dropdown() -> gr.Dropdown:
|
| 22 |
+
"""Create model selection dropdown"""
|
| 23 |
+
from config import MODELS
|
| 24 |
+
choices = [(config.name, key) for key, config in MODELS.items()]
|
| 25 |
+
return gr.Dropdown(
|
| 26 |
+
choices=choices,
|
| 27 |
+
value="musicgen_small",
|
| 28 |
+
label="Model",
|
| 29 |
+
info="Select AI model for generation"
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
@staticmethod
|
| 33 |
+
def create_duration_slider(min_val: int = 5, max_val: int = 30) -> gr.Slider:
|
| 34 |
+
"""Create duration slider"""
|
| 35 |
+
return gr.Slider(
|
| 36 |
+
minimum=min_val,
|
| 37 |
+
maximum=max_val,
|
| 38 |
+
value=10,
|
| 39 |
+
step=1,
|
| 40 |
+
label="Duration (seconds)",
|
| 41 |
+
info="Length of generated music"
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
@staticmethod
|
| 45 |
+
def create_guidance_slider() -> gr.Slider:
|
| 46 |
+
"""Create guidance scale slider"""
|
| 47 |
+
return gr.Slider(
|
| 48 |
+
minimum=1,
|
| 49 |
+
maximum=10,
|
| 50 |
+
value=3,
|
| 51 |
+
step=0.5,
|
| 52 |
+
label="Guidance Scale",
|
| 53 |
+
info="How closely to follow the prompt"
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
@staticmethod
|
| 57 |
+
def create_preset_dropdown() -> gr.Dropdown:
|
| 58 |
+
"""Create preset selection dropdown"""
|
| 59 |
+
from config import PRESETS
|
| 60 |
+
choices = [(name, name) for name in PRESETS.keys()]
|
| 61 |
+
return gr.Dropdown(
|
| 62 |
+
choices=choices,
|
| 63 |
+
label="Presets",
|
| 64 |
+
info="Quick style selection"
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
@staticmethod
|
| 68 |
+
def create_audio_visualization(audio_array: np.ndarray) -> go.Figure:
|
| 69 |
+
"""Create audio waveform visualization"""
|
| 70 |
+
fig = go.Figure()
|
| 71 |
+
|
| 72 |
+
# Waveform
|
| 73 |
+
fig.add_trace(go.Scatter(
|
| 74 |
+
y=audio_array,
|
| 75 |
+
mode='lines',
|
| 76 |
+
name='Waveform',
|
| 77 |
+
line=dict(color='blue', width=1)
|
| 78 |
+
))
|
| 79 |
+
|
| 80 |
+
fig.update_layout(
|
| 81 |
+
title="Audio Waveform",
|
| 82 |
+
xaxis_title="Sample",
|
| 83 |
+
yaxis_title="Amplitude",
|
| 84 |
+
height=200,
|
| 85 |
+
showlegend=False
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
return fig
|
| 89 |
+
|
| 90 |
+
@staticmethod
|
| 91 |
+
def create_spectrogram_visualization(audio_array: np.ndarray, sample_rate: int = 16000) -> go.Figure:
|
| 92 |
+
"""Create spectrogram visualization"""
|
| 93 |
+
import librosa
|
| 94 |
+
|
| 95 |
+
# Compute spectrogram
|
| 96 |
+
D = librosa.stft(audio_array)
|
| 97 |
+
S_db = librosa.amplitude_to_db(np.abs(D), ref=np.max)
|
| 98 |
+
|
| 99 |
+
fig = go.Figure(data=go.Heatmap(
|
| 100 |
+
z=S_db,
|
| 101 |
+
colorscale='Viridis'
|
| 102 |
+
))
|
| 103 |
+
|
| 104 |
+
fig.update_layout(
|
| 105 |
+
title="Spectrogram",
|
| 106 |
+
xaxis_title="Time",
|
| 107 |
+
yaxis_title="Frequency",
|
| 108 |
+
height=300
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
return fig
|
| 112 |
+
|
| 113 |
+
@staticmethod
|
| 114 |
+
def create_progress_info(current: int, total: int, status: str = "Processing") -> str:
|
| 115 |
+
"""Create progress information text"""
|
| 116 |
+
percentage = (current / total) * 100
|
| 117 |
+
return f"""
|
| 118 |
+
<div style='text-align: center; padding: 10px; background-color: #f0f0f0; border-radius: 5px;'>
|
| 119 |
+
<h4>{status}</h4>
|
| 120 |
+
<p>Progress: {current}/{total} ({percentage:.1f}%)</p>
|
| 121 |
+
<div style='width: 100%; background-color: #ddd; border-radius: 10px;'>
|
| 122 |
+
<div style='width: {percentage}%; height: 20px; background-color: #4CAF50; border-radius: 10px;'></div>
|
| 123 |
+
</div>
|
| 124 |
+
</div>
|
| 125 |
+
"""
|