File size: 24,705 Bytes
06f9b4f
e2bca25
 
 
 
 
 
 
 
 
 
 
06f9b4f
e2bca25
 
 
 
 
06f9b4f
e2bca25
 
 
 
 
 
 
 
 
06f9b4f
e2bca25
dd2db22
06f9b4f
dd2db22
 
 
 
3a71288
dd2db22
 
 
 
3a71288
dd2db22
 
 
 
 
e2bca25
06f9b4f
048983c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2bca25
 
048983c
 
 
 
 
 
e2bca25
 
 
 
 
ca6cc37
e2bca25
 
 
 
 
048983c
 
e2bca25
 
048983c
e2bca25
 
06f9b4f
e2bca25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c1dff6
 
 
e2bca25
 
 
 
 
 
 
 
2c1dff6
dd2db22
e2bca25
 
 
2c1dff6
dd2db22
e2bca25
 
 
 
 
2c1dff6
dd2db22
9c07de8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2bca25
 
 
 
 
2c1dff6
dd2db22
e2bca25
 
2c1dff6
dd2db22
e2bca25
 
 
 
 
 
 
2c1dff6
 
e2bca25
 
2c1dff6
 
e2bca25
2c1dff6
dd2db22
e2bca25
 
2c1dff6
dd2db22
e2bca25
 
 
 
 
 
 
 
 
 
 
 
2c1dff6
 
e2bca25
2c1dff6
dd2db22
2c1dff6
e2bca25
 
dd2db22
e2bca25
2c1dff6
e2bca25
 
 
 
3a71288
e2bca25
 
 
 
 
 
 
 
 
 
dd2db22
e2bca25
 
ebc0a66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2bca25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a71288
 
 
 
 
e2bca25
 
 
 
 
ebc0a66
 
 
 
 
 
e2bca25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebc0a66
e2bca25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4263af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd2db22
e2bca25
 
 
 
 
 
 
 
 
dd2db22
e2bca25
 
 
 
 
 
 
 
 
 
06f9b4f
2c1dff6
 
dd2db22
2c1dff6
 
 
 
 
 
 
dd2db22
2c1dff6
 
 
 
 
dd2db22
2c1dff6
 
dd2db22
06f9b4f
3a71288
e2bca25
2c1dff6
 
 
 
dd2db22
2c1dff6
e2bca25
 
 
 
 
3a71288
e2bca25
 
06f9b4f
2c1dff6
 
 
 
 
e2bca25
 
 
3a71288
e2bca25
dd2db22
 
ebc0a66
dd2db22
 
 
 
 
e2bca25
dd2db22
e2bca25
 
2c1dff6
e2bca25
dd2db22
2c1dff6
e2bca25
 
 
2c1dff6
dd2db22
2c1dff6
 
 
 
 
e2bca25
3a71288
dd2db22
 
e2bca25
dd2db22
e10d911
e2bca25
 
dd2db22
 
ebc0a66
dd2db22
ebc0a66
dd2db22
 
ebc0a66
 
e2bca25
dd2db22
 
e10d911
e2bca25
 
dd2db22
 
3a71288
 
 
 
 
 
dd2db22
3a71288
 
 
 
 
 
 
dd2db22
3a71288
e2bca25
3a71288
dd2db22
e10d911
e2bca25
 
3a71288
dd2db22
 
3a71288
dd2db22
3a71288
dd2db22
 
3a71288
dd2db22
 
 
3a71288
dd2db22
 
3a71288
dd2db22
 
 
3a71288
 
dd2db22
 
e2bca25
3a71288
e2bca25
 
 
 
 
 
 
dd2db22
 
e2bca25
dd2db22
 
e2bca25
dd2db22
 
 
 
e2bca25
 
3a71288
dd2db22
3a71288
 
 
e2bca25
3a71288
e2bca25
 
 
3a71288
 
e2bca25
 
 
 
dd2db22
e2bca25
 
 
dd2db22
3a71288
dd2db22
 
 
3a71288
dd2db22
 
 
3a71288
dd2db22
 
 
e2bca25
dd2db22
 
 
 
e2bca25
 
2c1dff6
e2bca25
2c1dff6
e2bca25
 
06f9b4f
2c1dff6
 
 
 
 
 
06f9b4f
e2bca25
 
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
import gradio as gr
import torch
import torchaudio
import logging
import tempfile
import os
import sys
from pathlib import Path
import numpy as np
from typing import Optional, Tuple
import time
import traceback

# Add hf_AC to path
current_dir = Path(__file__).parent
hf_ac_path = current_dir / "hf_AC"
if hf_ac_path.exists():
    sys.path.insert(0, str(hf_ac_path))

# Configuration for HF Space
EXAMPLE_PROMPTS = [
    "Crackling fireplace with gentle flames",
    "Ocean waves crashing on rocky shore", 
    "Forest ambience with bird songs",
    "Keyboard typing sounds",
    "Footsteps on wooden floor",
    "Rain on metal roof"
]

USAGE_TIPS = """
### ๐Ÿ’ก Usage Tips

**Basic Settings:**
- **Video Quality**: Use clear, well-lit videos, recommended 1-15 seconds
- **Reference Audio**: Provide clear audio clips as timbre reference
- **CFG Strength**: Between 1-8, higher values follow description more closely

**Advanced Features:**
- **mask_away_clip**: Enable when video content differs significantly from desired audio
- **Fine-grained Control**: Use reference audio for precise timbre and style control
- **Zero-shot Generation**: Generate novel sound combinations without training

**Application Scenarios:**
- Film post-production audio
- Game sound effect creation
- Music composition assistance
- Sound design experimentation
"""

# Check and install missing dependencies
def check_dependencies():
    """Check if all required packages are available"""
    missing_packages = []
    required_packages = [
        'torch', 'torchaudio', 'numpy', 'scipy', 'librosa', 
        'torchdiffeq', 'einops', 'hydra', 'tensordict', 'av'
    ]
    
    for package in required_packages:
        try:
            if package == 'hydra':
                __import__('hydra')
            elif package == 'av':
                __import__('av')
            else:
                __import__(package)
        except ImportError:
            missing_packages.append(package)
    
    return missing_packages

# Import hf_AC modules with error handling
try:
    # First check basic dependencies
    missing_deps = check_dependencies()
    if missing_deps:
        print(f"Warning: Missing dependencies: {missing_deps}")
        print("Some dependencies may be installing in the background...")
    
    from hf_AC.mmaudio.eval_utils import (ModelConfig, all_model_cfg, generate, load_video, 
                                          setup_eval_logging)
    from hf_AC.mmaudio.model.flow_matching import FlowMatching
    from hf_AC.mmaudio.model.networks import MMAudio, get_my_mmaudio
    from hf_AC.mmaudio.model.utils.features_utils import FeaturesUtils
    from hf_AC.inf import Audio
    
    # Setup logging
    setup_eval_logging()
    log = logging.getLogger()
    HF_AC_AVAILABLE = True
    print("โœ… hf_AC modules loaded successfully!")
    
except ImportError as e:
    print(f"Warning: hf_AC modules not available: {e}")
    print("This may be due to missing dependencies. Please wait for installation to complete.")
    log = logging.getLogger()
    HF_AC_AVAILABLE = False

class AudioFoleyModel:
    def __init__(self):
        self.device = 'cpu'
        if torch.cuda.is_available():
            self.device = 'cuda'
        elif torch.backends.mps.is_available():
            self.device = 'mps'
        
        self.dtype = torch.bfloat16
        self.model = None
        self.net = None
        self.fm = None
        self.feature_utils = None
        
    def load_model(self, variant='large_44k', model_path=None):
        """Load the hf_AC model with progress updates"""
        global model_loading_status
        
        try:
            if not HF_AC_AVAILABLE:
                return "โŒ hf_AC modules not available. Please install the hf_AC package."
            
            if variant not in all_model_cfg:
                available_variants = list(all_model_cfg.keys()) if all_model_cfg else []
                return f"โŒ Unknown model variant: {variant}. Available: {available_variants}"
            
            # Step 1: Initialize model config
            model_loading_status = "๐Ÿ”ง Initializing model configuration..."
            log.info(f"Loading model variant: {variant}")
            self.model: ModelConfig = all_model_cfg[variant]
            
            # Step 2: Download model components
            model_loading_status = "๐Ÿ“ฅ Downloading model components..."
            try:
                self.model.download_if_needed()
            except Exception as e:
                log.warning(f"Could not download model components: {e}")
            
            # Step 3: Download main model weights
            model_loading_status = "๐Ÿ“ฅ Downloading main model weights..."
            if not hasattr(self.model, 'model_path') or not self.model.model_path or not Path(self.model.model_path).exists():
                try:
                    from huggingface_hub import hf_hub_download
                    log.info("Downloading main model weights from HuggingFace...")
                    
                    # Create weights directory
                    weights_dir = Path("weights")
                    weights_dir.mkdir(exist_ok=True)
                    
                    # Download model.pth from HuggingFace
                    model_file = hf_hub_download(
                        repo_id="FF2416/AC-Foley",
                        filename="model.pth",
                        local_dir=str(weights_dir),
                        local_dir_use_symlinks=False
                    )
                    self.model.model_path = Path(model_file)
                    log.info(f"โœ… Downloaded model weights to {model_file}")
                    
                except Exception as e:
                    log.warning(f"Could not download main model weights: {e}")
                    log.info("Will proceed with available components only")
            
            # Set custom model path if provided
            if model_path and os.path.exists(model_path):
                self.model.model_path = Path(model_path)
                log.info(f"Using custom model path: {model_path}")
            
            # Step 4: Load neural network
            model_loading_status = "๐Ÿง  Loading neural network..."
            self.net: MMAudio = get_my_mmaudio(self.model.model_name).to(self.device, self.dtype).eval()
            
            # Step 5: Load weights
            model_loading_status = "โš–๏ธ Loading model weights..."
            if hasattr(self.model, 'model_path') and self.model.model_path and Path(self.model.model_path).exists():
                try:
                    weights = torch.load(self.model.model_path, map_location=self.device, weights_only=True)
                    self.net.load_weights(weights['weights'])
                    log.info(f'โœ… Loaded weights from {self.model.model_path}')
                except Exception as e:
                    log.error(f"Failed to load weights: {e}")
                    model_loading_status = f"โŒ Failed to load model weights: {e}"
                    return model_loading_status
            else:
                log.warning('โš ๏ธ No model weights found, using default initialization')
                model_loading_status = "โš ๏ธ ๆจกๅž‹็ป„ไปถๅทฒๅŠ ่ฝฝ๏ผŒไฝ†ไธปๆƒ้‡ไธๅฏ็”จใ€‚ๆŸไบ›ๅŠŸ่ƒฝๅฏ่ƒฝๅ—้™ใ€‚"
                return model_loading_status
            
            # Step 6: Initialize flow matching
            model_loading_status = "๐ŸŒŠ Initializing flow matching..."
            self.fm = FlowMatching(min_sigma=0, inference_mode='euler', num_steps=25)
            
            # Step 7: Initialize feature utils
            model_loading_status = "๐Ÿ”ง Initializing feature utilities..."
            try:
                self.feature_utils = FeaturesUtils(
                    tod_vae_ckpt=self.model.vae_path,
                    synchformer_ckpt=self.model.synchformer_ckpt,
                    enable_conditions=True,
                    mode=self.model.mode,
                    bigvgan_vocoder_ckpt=self.model.bigvgan_16k_path,
                    need_vae_encoder=True
                )
                self.feature_utils = self.feature_utils.to(self.device, self.dtype).eval()
            except Exception as e:
                log.error(f"Failed to initialize feature utils: {e}")
                model_loading_status = f"โŒ Failed to initialize feature utilities: {e}"
                return model_loading_status
            
            # Step 8: Complete
            model_loading_status = "โœ… Model loaded successfully! Ready to generate audio."
            return model_loading_status
            
        except Exception as e:
            error_msg = f"โŒ Model loading error: {str(e)}"
            log.error(error_msg)
            model_loading_status = error_msg
            return error_msg
    
    def generate_audio(self, video_file, prompt: str, negative_prompt: str = "", 
                      duration: float = 8.0, cfg_strength: float = 4.5, 
                      seed: int = 42, reference_audio: str = None, mask_away_clip: bool = False) -> Tuple[Optional[str], str]:
        """Generate audio from video and text prompt"""
        try:
            # Validation checks
            if not HF_AC_AVAILABLE:
                return None, "โŒ hf_AC modules not available."
            
            if self.net is None or self.feature_utils is None:
                return None, "โŒ Model not loaded. Please load the model first."
            
            if video_file is None:
                return None, "โŒ Please upload a video file."
            
            log.info(f'๐ŸŽฌ Processing video: {video_file}')
            if prompt.strip():
                log.info(f'๐Ÿ“ Prompt: "{prompt}"')
            else:
                log.info('๐Ÿ“ No prompt provided - will generate based on video content')
            if reference_audio:
                log.info(f'๐ŸŽต Reference audio: {reference_audio}')
            
            # Load and process reference audio if provided
            reference_audio_tensor = None
            if reference_audio and os.path.exists(reference_audio):
                try:
                    # Use the same Audio class from hf_AC
                    SAMPLE_RATE = 44100
                    audio_processor = Audio([reference_audio], SAMPLE_RATE)
                    audio_list = audio_processor.load_audio()
                    if audio_list:
                        reference_audio_tensor = audio_list[0]
                        log.info(f'๐ŸŽต Reference audio loaded: {reference_audio_tensor.shape}')
                except Exception as e:
                    log.warning(f"Failed to load reference audio: {e}")
                    reference_audio_tensor = None
            
            # Load and process video
            try:
                video_path = Path(video_file)
                if not video_path.exists():
                    return None, f"โŒ Video file not found: {video_file}"
                
                video_info = load_video(video_path, duration)
                clip_frames = video_info.clip_frames
                sync_frames = video_info.sync_frames
                duration_sec = video_info.duration_sec
                
                log.info(f'๐Ÿ“น Video loaded: {duration_sec:.2f}s duration')
                
            except Exception as e:
                return None, f"โŒ Failed to load video: {str(e)}"
            
            # Prepare frames
            if mask_away_clip:
                clip_frames = None  # Mask away clip frames when video and audio don't match well
                log.info("๐ŸŽญ Using mask_away_clip: ignoring visual features")
            else:
                clip_frames = clip_frames.unsqueeze(0) if clip_frames is not None else None
            sync_frames = sync_frames.unsqueeze(0)
            
            # Update model sequence configuration
            try:
                self.model.seq_cfg.duration = duration_sec
                # Set audio sample count based on reference audio or default
                if reference_audio_tensor is not None:
                    self.model.seq_cfg.audio_num_sample = reference_audio_tensor.shape[0]
                else:
                    self.model.seq_cfg.audio_num_sample = 89088  # Default for 44kHz
                    
                self.net.update_seq_lengths(
                    self.model.seq_cfg.latent_seq_len, 
                    self.model.seq_cfg.clip_seq_len, 
                    self.model.seq_cfg.sync_seq_len, 
                    self.model.seq_cfg.audio_seq_len
                )
            except Exception as e:
                return None, f"โŒ Failed to configure model: {str(e)}"
            
            # Generate audio
            try:
                log.info('๐ŸŽต Generating audio...')
                start_time = time.time()
                
                with torch.inference_mode():
                    audios = generate(
                        clip_frames,
                        sync_frames, 
                        [prompt], 
                        reference_audio_tensor,  # Use reference audio if provided
                        negative_text=[negative_prompt] if negative_prompt.strip() else None,
                        feature_utils=self.feature_utils,
                        net=self.net,
                        fm=self.fm,
                        rng=torch.Generator(device=self.device).manual_seed(seed),
                        cfg_strength=cfg_strength
                    )
                
                generation_time = time.time() - start_time
                log.info(f'โฑ๏ธ Generation completed in {generation_time:.2f}s')
                
            except Exception as e:
                return None, f"โŒ Audio generation failed: {str(e)}"
            
            # Save generated audio
            try:
                audio = audios.float().cpu()[0]
                
                # Create output filename with timestamp
                timestamp = int(time.time())
                output_filename = f"generated_audio_{timestamp}.wav"
                permanent_path = f"/tmp/{output_filename}"
                
                # Save audio file with fallback methods
                try:
                    # Try with torchaudio first
                    torchaudio.save(permanent_path, audio, self.model.seq_cfg.sampling_rate)
                except Exception as e:
                    log.warning(f"torchaudio.save failed: {e}, trying alternative method...")
                    try:
                        # Fallback: use soundfile if available
                        import soundfile as sf
                        sf.write(permanent_path, audio.numpy().T, self.model.seq_cfg.sampling_rate)
                    except ImportError:
                        try:
                            # Fallback: use scipy.io.wavfile
                            from scipy.io.wavfile import write
                            # Convert to int16 for wav format
                            audio_int16 = (audio * 32767).clamp(-32768, 32767).to(torch.int16)
                            write(permanent_path, self.model.seq_cfg.sampling_rate, audio_int16.numpy().T)
                        except Exception as e2:
                            return None, f"โŒ Audio saving failed: {str(e2)}"
                
                # Verify file was created
                if not os.path.exists(permanent_path):
                    return None, "โŒ Failed to save audio file"
                
                file_size = os.path.getsize(permanent_path) / 1024  # KB
                success_msg = f"โœ… Audio generated successfully!\n"
                success_msg += f"๐Ÿ“Š Duration: {duration_sec:.2f}s | "
                success_msg += f"Size: {file_size:.1f}KB | "
                success_msg += f"Generation time: {generation_time:.2f}s"
                
                return permanent_path, success_msg
                
            except Exception as e:
                return None, f"โŒ Failed to save audio: {str(e)}"
                
        except Exception as e:
            error_msg = f"โŒ Unexpected error: {str(e)}\n{traceback.format_exc()}"
            log.error(error_msg)
            return None, error_msg

# Global model instance - initialized once
audio_model = None
model_loading_status = "Not initialized"

def initialize_model():
    """Initialize model once at startup"""
    global audio_model, model_loading_status
    
    if audio_model is None:
        try:
            model_loading_status = "Initializing model..."
            audio_model = AudioFoleyModel()
            load_result = audio_model.load_model()
            model_loading_status = load_result
            return load_result
        except Exception as e:
            model_loading_status = f"โŒ Model initialization failed: {str(e)}"
            return model_loading_status
    else:
        return "โœ… Model already loaded"

def generate_audio_interface(video_file, audio_file, prompt, duration, cfg_strength, mask_away_clip):
    """Interface function for generating audio"""
    global audio_model, model_loading_status
    
    # Check if model is loaded
    if audio_model is None or audio_model.net is None:
        return None, "โŒ Model not loaded. Please wait for initialization to complete or refresh the page."
    
    # Use fixed seed for consistency in HF Space
    seed = 42
    negative_prompt = ""  # Simplified interface
    
    audio_path, message = audio_model.generate_audio(
        video_file, prompt, negative_prompt, duration, cfg_strength, seed, audio_file, mask_away_clip
    )
    return audio_path, message

def get_model_status():
    """Get current model loading status"""
    global model_loading_status
    return model_loading_status

# Create Gradio interface
with gr.Blocks(title="hf_AC Audio Foley Generator", theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    # ๐ŸŽต AC-Foley: Reference-Audio-Guided Video-to-Audio Synthesis
    
    ## ๐Ÿ“– About
    AC-Foley is a reference-audio-guided video-to-audio synthesis model that enables precise fine-grained sound synthesis. Unlike traditional text-dependent methods, AC-Foley directly leverages reference audio to achieve precise control over generated sounds, addressing the ambiguity of textual descriptions in micro-acoustic features.
    
    ## โœจ Key Features
    - **Fine-grained Sound Synthesis**: Generate footsteps with distinct timbres (wood, marble, gravel, etc.)
    - **Timbre Transfer**: Transform violin melodies into bright, piercing suona tones
    - **Zero-shot Generation**: Create unique sound effects without specialized training
    - **Visual-Audio Alignment**: Automatically generate matching audio from video content
    
    *Based on paper: [AC-Foley: Reference-Audio-Guided Video-to-Audio Synthesis with Acoustic Transfer](https://openreview.net/forum?id=URPXhnWdBF)*
    """)
    
    # Model status display - will be updated automatically
    model_status = gr.Textbox(
        label="Model Status", 
        value=model_loading_status,
        interactive=False
    )
    
    # Add a refresh button for status
    refresh_status_btn = gr.Button("๐Ÿ”„ Refresh Status", size="sm")
    refresh_status_btn.click(
        fn=get_model_status,
        outputs=model_status
    )
    
    with gr.Row():
        with gr.Column(scale=2):
            # Required inputs
            gr.Markdown("### ๐Ÿ“น Required Input")
            video_input = gr.Video(
                label="Video File - Upload video for audio generation",
                format="mp4"
            )
            
            # Optional inputs
            gr.Markdown("### ๐ŸŽ›๏ธ Optional Inputs")
            audio_input = gr.Audio(
                label="Reference Audio - Provide timbre, style, rhythm reference (fine-grained control)",
                type="filepath",
                sources=["upload"],
                format="wav"
            )
            
            prompt_input = gr.Textbox(
                label="Text Prompt - Describe desired audio type (leave empty for auto-generation from video)",
                placeholder="e.g., 'footsteps', 'metal clang', 'bird chirping'",
                lines=2
            )
            
            # Advanced options
            with gr.Accordion("๐Ÿ”ง Advanced Options", open=False):
                with gr.Row():
                    duration_slider = gr.Slider(
                        minimum=1.0,
                        maximum=15.0,
                        value=8.0,
                        step=0.5,
                        label="Duration (seconds)"
                    )
                    
                    cfg_strength_slider = gr.Slider(
                        minimum=1.0,
                        maximum=8.0,
                        value=4.5,
                        step=0.1,
                        label="CFG Strength"
                    )
                
                mask_away_clip = gr.Checkbox(
                    label="Ignore Visual Features (mask_away_clip) - Enable when video and reference audio differ significantly",
                    value=False
                )
        
        with gr.Column(scale=1):
            # Usage guide
            gr.Markdown("### ๐Ÿ“‹ Usage Guide")
            gr.Markdown("""
            **Four Generation Modes:**
            
            1๏ธโƒฃ **Video Only**: Upload video only
            - Auto-generate audio from visual content
            
            2๏ธโƒฃ **Video + Reference Audio**: Upload video + audio
            - Use reference audio's timbre and style
            - Achieve fine-grained timbre control
            
            3๏ธโƒฃ **Video + Text**: Upload video + text
            - Generate specified audio type from text description
            
            4๏ธโƒฃ **Complete Mode**: Video + Audio + Text
            - Most precise control method
            - Combine visual, timbral, and semantic guidance
            """)
            
            # Example prompts
            gr.Markdown("### ๐ŸŽฏ Example Prompts")
            example_buttons = []
            for prompt in EXAMPLE_PROMPTS[:4]:
                btn = gr.Button(prompt, size="sm")
                example_buttons.append(btn)
                btn.click(
                    fn=lambda p=prompt: p,
                    outputs=prompt_input
                )
    
    # Generate button
    generate_btn = gr.Button("๐ŸŽต Generate Audio", variant="primary", size="lg")
    
    # Output area
    gr.Markdown("### ๐ŸŽง Generated Results")
    audio_output = gr.Audio(
        label="Generated Audio",
        type="filepath",
        format="wav",
        autoplay=False
    )
    
    generation_status = gr.Textbox(
        label="Generation Status", 
        interactive=False,
        lines=2
    )
    
    # ็ป‘ๅฎš็”Ÿๆˆไบ‹ไปถ
    generate_btn.click(
        fn=generate_audio_interface,
        inputs=[
            video_input, audio_input, prompt_input, 
            duration_slider, cfg_strength_slider, mask_away_clip
        ],
        outputs=[audio_output, generation_status]
    )
    
    with gr.Accordion("๐Ÿ’ก Detailed Information", open=False):
        gr.Markdown(USAGE_TIPS)
        
        gr.Markdown("""
        ### ๐ŸŽฌ Application Examples
        
        **Fine-grained Sound Synthesis:**
        - "Footsteps on wooden floor" + reference audio โ†’ Specific timbre footsteps
        - "Metal collision" + different reference audio โ†’ Iron vs. copper distinction
        
        **Timbre Transfer:**
        - Piano melody video + violin reference audio โ†’ Violin playing same melody
        - Human humming + instrument reference โ†’ Instrumental version
        
        **Creative Sound Effects:**
        - Sci-fi scene video + real sound reference โ†’ Unique sci-fi effects
        - Animation video + real sound effects โ†’ Cartoon-reality hybrid effects
        
        ### ๐Ÿ“š Technical Details
        - Model based on diffusion models and audio conditioning mechanisms
        - Supports 44.1kHz high-quality audio generation
        - Achieves visual-audio-text multimodal alignment
        """)
    
    # Auto-initialize model on startup
    demo.load(
        fn=initialize_model,
        outputs=[model_status]
    )

# Initialize model when module is imported (for HF Space)
if HF_AC_AVAILABLE:
    print("๐Ÿš€ Starting model initialization...")
    initialize_model()
    print(f"๐Ÿ“Š Model status: {model_loading_status}")

if __name__ == "__main__":
    # HF Space will handle the server configuration
    demo.launch()