File size: 3,791 Bytes
cb7f5d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import importlib.util
import sys
from huggingface_hub import hf_hub_download
from typing import List, Optional

def load_module_from_file(module_name: str, file_path: str):
    """Load a Python module from file path"""
    spec = importlib.util.spec_from_file_location(module_name, file_path)
    if spec is None or spec.loader is None:
        raise ImportError(f"Cannot load module {module_name} from {file_path}")
    module = importlib.util.module_from_spec(spec)
    sys.modules[module_name] = module
    spec.loader.exec_module(module)
    return module

def download_model_files(repo_id: str, filenames: List[str], local_dir: Optional[str] = None) -> List[str]:
    """Download multiple files from Hugging Face Hub"""
    paths = []
    for filename in filenames:
        try:
            path = hf_hub_download(
                repo_id=repo_id,
                filename=filename,
                local_dir=local_dir,
                local_dir_use_symlinks=False
            )
            paths.append(path)
        except Exception as e:
            print(f"Error downloading {filename}: {str(e)}")
            raise
    return paths

def ensure_dir(path: str) -> None:
    """Ensure directory exists, create if it doesn't"""
    os.makedirs(path, exist_ok=True)

def list_voice_files(voices_dir: str) -> List[str]:
    """List available voice files in directory"""
    voices = []
    try:
        if not os.path.exists(voices_dir):
            print(f"Voices directory does not exist: {voices_dir}")
            return voices
            
        files = os.listdir(voices_dir)
        print(f"Found {len(files)} files in voices directory")
        
        for file in files:
            if file.endswith(".pt"):
                voice_name = file[:-3]  # Remove .pt extension
                print(f"Found voice: {voice_name}")
                voices.append(voice_name)
                
        if not voices:
            print("No voice files found in voices directory")
            
    except Exception as e:
        print(f"Error listing voices: {str(e)}")
        import traceback
        traceback.print_exc()
        
    return sorted(voices)

def download_voice_files(repo_id: str, voices: List[str], voices_dir: str) -> None:
    """Download voice files from Hugging Face Hub"""
    ensure_dir(voices_dir)
    
    for voice in voices:
        try:
            voice_path = os.path.join(voices_dir, voice)
            print(f"Attempting to download voice {voice} to {voice_path}")
            
            try:
                downloaded_path = hf_hub_download(
                    repo_id=repo_id,
                    filename=f"voices/{voice}",
                    local_dir=voices_dir,
                    local_dir_use_symlinks=False,
                    force_filename=voice
                )
                print(f"Download completed to: {downloaded_path}")
                
                if not os.path.exists(voice_path):
                    print(f"Warning: File not found at expected path {voice_path}")
                    print(f"Checking download location: {downloaded_path}")
                    if os.path.exists(downloaded_path):
                        print(f"Moving file from {downloaded_path} to {voice_path}")
                        os.rename(downloaded_path, voice_path)
                else:
                    print(f"Verified voice file exists: {voice_path}")
                    
            except Exception as e:
                print(f"Error downloading voice {voice}: {str(e)}")
                import traceback
                traceback.print_exc()
                
        except Exception as e:
            print(f"Error downloading voice {voice}: {str(e)}")
            import traceback
            traceback.print_exc()