Files
audiocraft-ui/src/ui/state.py

295 lines
8.5 KiB
Python
Raw Normal View History

"""State management for Gradio UI."""
from dataclasses import dataclass, field
from typing import Any, Optional
@dataclass
class UIState:
"""Global UI state container."""
# Current view
current_tab: str = "dashboard"
# Generation state
is_generating: bool = False
current_job_id: Optional[str] = None
# Selected items
selected_project_id: Optional[str] = None
selected_generation_id: Optional[str] = None
selected_preset_id: Optional[str] = None
# Model state
selected_model: str = "musicgen"
selected_variant: str = "medium"
# Generation parameters (current values)
prompt: str = ""
duration: float = 10.0
temperature: float = 1.0
top_k: int = 250
top_p: float = 0.0
cfg_coef: float = 3.0
seed: Optional[int] = None
# Conditioning
melody_audio: Optional[str] = None
style_audio: Optional[str] = None
chords: list[dict[str, Any]] = field(default_factory=list)
drums_pattern: str = ""
bpm: float = 120.0
# UI preferences
show_advanced: bool = False
auto_play: bool = True
def reset_generation_params(self) -> None:
"""Reset generation parameters to defaults."""
self.prompt = ""
self.duration = 10.0
self.temperature = 1.0
self.top_k = 250
self.top_p = 0.0
self.cfg_coef = 3.0
self.seed = None
self.melody_audio = None
self.style_audio = None
self.chords = []
self.drums_pattern = ""
def apply_preset(self, preset: dict[str, Any]) -> None:
"""Apply preset parameters."""
params = preset.get("parameters", {})
self.duration = params.get("duration", self.duration)
self.temperature = params.get("temperature", self.temperature)
self.top_k = params.get("top_k", self.top_k)
self.top_p = params.get("top_p", self.top_p)
self.cfg_coef = params.get("cfg_coef", self.cfg_coef)
def to_generation_params(self) -> dict[str, Any]:
"""Convert current state to generation parameters."""
return {
"duration": self.duration,
"temperature": self.temperature,
"top_k": self.top_k,
"top_p": self.top_p,
"cfg_coef": self.cfg_coef,
"seed": self.seed,
}
# Default presets for each model
DEFAULT_PRESETS = {
"musicgen": [
{
"id": "cinematic",
"name": "Cinematic",
"description": "Epic orchestral soundscapes",
"parameters": {
"duration": 30,
"temperature": 1.0,
"top_k": 250,
"cfg_coef": 3.0,
},
},
{
"id": "electronic",
"name": "Electronic",
"description": "Synthesizers and beats",
"parameters": {
"duration": 15,
"temperature": 1.1,
"top_k": 200,
"cfg_coef": 3.5,
},
},
{
"id": "ambient",
"name": "Ambient",
"description": "Atmospheric and calm",
"parameters": {
"duration": 30,
"temperature": 0.9,
"top_k": 300,
"cfg_coef": 2.5,
},
},
{
"id": "rock",
"name": "Rock",
"description": "Guitar-driven energy",
"parameters": {
"duration": 20,
"temperature": 1.0,
"top_k": 250,
"cfg_coef": 3.0,
},
},
{
"id": "jazz",
"name": "Jazz",
"description": "Smooth and improvisational",
"parameters": {
"duration": 20,
"temperature": 1.2,
"top_k": 200,
"cfg_coef": 2.5,
},
},
],
"audiogen": [
{
"id": "nature",
"name": "Nature",
"description": "Natural environments",
"parameters": {
"duration": 10,
"temperature": 1.0,
"top_k": 250,
"cfg_coef": 3.0,
},
},
{
"id": "urban",
"name": "Urban",
"description": "City sounds",
"parameters": {
"duration": 10,
"temperature": 1.0,
"top_k": 250,
"cfg_coef": 3.0,
},
},
{
"id": "mechanical",
"name": "Mechanical",
"description": "Machines and tools",
"parameters": {
"duration": 5,
"temperature": 0.9,
"top_k": 200,
"cfg_coef": 3.5,
},
},
{
"id": "weather",
"name": "Weather",
"description": "Rain, thunder, wind",
"parameters": {
"duration": 10,
"temperature": 1.0,
"top_k": 250,
"cfg_coef": 3.0,
},
},
],
"magnet": [
{
"id": "fast",
"name": "Fast",
"description": "Quick generation",
"parameters": {
"duration": 10,
"temperature": 3.0,
"top_p": 0.9,
"cfg_coef": 3.0,
},
},
{
"id": "quality",
"name": "Quality",
"description": "Higher quality output",
"parameters": {
"duration": 10,
"temperature": 2.5,
"top_p": 0.85,
"cfg_coef": 4.0,
},
},
],
"musicgen-style": [
{
"id": "style_transfer",
"name": "Style Transfer",
"description": "Copy style from reference",
"parameters": {
"duration": 15,
"temperature": 1.0,
"top_k": 250,
"cfg_coef": 3.0,
"eval_q": 3,
"excerpt_length": 3.0,
},
},
],
"jasco": [
{
"id": "pop",
"name": "Pop",
"description": "Pop chord progressions",
"parameters": {
"duration": 10,
"temperature": 1.0,
"top_k": 250,
"cfg_coef": 3.0,
"bpm": 120,
},
},
{
"id": "blues",
"name": "Blues",
"description": "12-bar blues",
"parameters": {
"duration": 10,
"temperature": 1.0,
"top_k": 250,
"cfg_coef": 3.0,
"bpm": 100,
},
},
],
}
# Prompt suggestions for each model
PROMPT_SUGGESTIONS = {
"musicgen": [
"Epic orchestral music with dramatic strings and powerful brass",
"Upbeat electronic dance music with synthesizers and heavy bass",
"Calm acoustic guitar melody with soft piano accompaniment",
"Energetic rock song with electric guitars and driving drums",
"Smooth jazz with saxophone solo and walking bass",
"Ambient soundscape with ethereal pads and gentle textures",
"Cinematic trailer music building to an epic climax",
"Lo-fi hip hop beats with vinyl crackle and mellow keys",
],
"audiogen": [
"Thunder and heavy rain with occasional lightning strikes",
"Busy city street with traffic, horns, and distant sirens",
"Forest ambience with birds singing and wind in trees",
"Ocean waves crashing on a rocky shore",
"Crackling fireplace with wood popping",
"Coffee shop atmosphere with murmuring voices and clinking cups",
"Construction site with hammering and machinery",
"Spaceship engine humming with occasional beeps",
],
"magnet": [
"Energetic pop music with catchy melody",
"Dark electronic music with deep bass",
"Cheerful ukulele tune with whistling",
"Dramatic piano piece with building intensity",
],
"musicgen-style": [
"Generate music in the style of the uploaded reference",
"Create a variation with similar instrumentation",
"Compose a piece matching the mood of the reference",
],
"jasco": [
"Upbeat pop song with the specified chord progression",
"Mellow jazz piece following the chord changes",
"Rock anthem with powerful drum pattern",
"Electronic track with syncopated rhythms",
],
}