kochit commited on
Commit
be732bb
Β·
verified Β·
1 Parent(s): 5102078

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -49
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import os
2
- # --- 1. FORCE CPU (GPU Error ရှောင်ရန်) ---
3
  os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
4
 
5
  import sys
@@ -8,54 +8,38 @@ import gradio as gr
8
  import edge_tts
9
  import asyncio
10
  import shutil
11
- import zipfile
12
  from huggingface_hub import hf_hub_download
13
 
14
  # PyTorch CPU Mode
15
  torch.cuda.is_available = lambda : False
16
- print(f"πŸš€ System Mode: CPU Only (Stable V2)")
17
 
18
- # --- 2. Setup OpenVoice V2 ---
19
  if not os.path.exists("OpenVoice"):
20
- print("Installing OpenVoice V2...")
21
  os.system("git clone https://github.com/myshell-ai/OpenVoice.git")
22
 
23
  sys.path.append(os.path.abspath("OpenVoice"))
24
 
25
- # V2 Checkpoints Folder
26
- os.makedirs("checkpoints_v2", exist_ok=True)
27
 
28
- # V2 Model Download Function (Hugging Face Source)
29
- def download_v2_models():
30
- # Model α€›α€Ύα€­α€™α€›α€Ύα€­ စစ်ဆေးခြင်း
31
- if not os.path.exists("checkpoints_v2/converter/checkpoint.pth"):
32
- print("Downloading OpenVoice V2 Zip from Hugging Face...")
33
  try:
34
- # AWS Link (403 Error) ထစား Hugging Face Link α€€α€­α€― α€žα€―α€Άα€Έα€žα€Šα€Ί
35
- zip_path = hf_hub_download(
36
- repo_id="myshell-ai/OpenVoice",
37
- filename="checkpoints_v2_0417.zip",
38
- local_dir=".",
39
- local_dir_use_symlinks=False
40
- )
41
-
42
- print("Extracting Model Zip...")
43
- with zipfile.ZipFile(zip_path, 'r') as zip_ref:
44
- zip_ref.extractall(".")
45
-
46
- # Folder α€”α€¬α€™α€Šα€Ία€•α€Όα€”α€Ία€Šα€Ύα€­α€α€Όα€„α€Ία€Έ
47
- if os.path.exists("checkpoints_v2_0417"):
48
- if os.path.exists("checkpoints_v2"):
49
- shutil.rmtree("checkpoints_v2")
50
- os.rename("checkpoints_v2_0417", "checkpoints_v2")
51
-
52
- print("βœ… V2 Model Extracted Successfully!")
53
  except Exception as e:
54
  print(f"Download Error: {e}")
55
 
56
- download_v2_models()
57
 
58
- # Import OpenVoice Modules
59
  try:
60
  from openvoice.api import ToneColorConverter
61
  from openvoice import se_extractor
@@ -64,22 +48,23 @@ except ImportError:
64
  from api import ToneColorConverter
65
  import se_extractor
66
 
67
- # --- 3. Load V2 Model ---
68
- ckpt_converter = 'checkpoints_v2/converter'
69
  if not os.path.exists(f"{ckpt_converter}/config.json"):
70
- print("Warning: Config not found, trying fallback path...")
71
- ckpt_converter = 'OpenVoice/checkpoints_v2/converter'
72
 
73
- print(f"Loading V2 Model from {ckpt_converter}...")
74
  try:
 
75
  tone_color_converter = ToneColorConverter(f'{ckpt_converter}/config.json', device='cpu')
76
  tone_color_converter.load_ckpt(f'{ckpt_converter}/checkpoint.pth')
77
- print("βœ… V2 Model Loaded Successfully!")
78
  except Exception as e:
79
- print(f"CRITICAL MODEL LOAD ERROR: {e}")
80
  tone_color_converter = None
81
 
82
- # --- 4. Mastering Engine ---
83
  def apply_mastering(input_wav, style="Radio"):
84
  if not shutil.which("ffmpeg"): return input_wav
85
  output_wav = "outputs/mastered_output.wav"
@@ -96,7 +81,7 @@ def apply_mastering(input_wav, style="Radio"):
96
  return output_wav
97
  except: return input_wav
98
 
99
- # --- 5. Main Workflow ---
100
  async def run_edge_tts(text, gender):
101
  voice = "my-MM-ThihaNeural" if gender == "Male" else "my-MM-NularNeural"
102
  output_file = "temp_base.mp3"
@@ -105,7 +90,7 @@ async def run_edge_tts(text, gender):
105
 
106
  def predict(text, ref_audio, gender, mastering_style):
107
  if tone_color_converter is None:
108
- return "System Error: Model failed to load. Check logs.", None
109
  if not text: return "α€…α€¬α€›α€­α€―α€€α€Ία€‘α€Šα€·α€Ία€•α€«", None
110
  if not ref_audio: return "Reference Audio α€‘α€Šα€·α€Ία€•α€«", None
111
 
@@ -113,9 +98,10 @@ def predict(text, ref_audio, gender, mastering_style):
113
  # Step A: Edge TTS
114
  base_audio = asyncio.run(run_edge_tts(text, gender))
115
 
116
- # Step B: OpenVoice V2
117
  os.makedirs("outputs", exist_ok=True)
118
 
 
119
  try:
120
  target_se, _ = se_extractor.get_se(ref_audio, tone_color_converter, target_dir='outputs', vad=True)
121
  except:
@@ -123,7 +109,7 @@ def predict(text, ref_audio, gender, mastering_style):
123
 
124
  source_se, _ = se_extractor.get_se(base_audio, tone_color_converter, target_dir='outputs', vad=False)
125
 
126
- raw_output = "outputs/raw_v2.wav"
127
 
128
  tone_color_converter.convert(
129
  audio_src_path=base_audio,
@@ -135,7 +121,7 @@ def predict(text, ref_audio, gender, mastering_style):
135
 
136
  # Step C: Mastering
137
  final_output = apply_mastering(raw_output, mastering_style)
138
- return "Success (V2 Ready)!", final_output
139
 
140
  except Exception as e:
141
  import traceback
@@ -143,9 +129,9 @@ def predict(text, ref_audio, gender, mastering_style):
143
  return f"Error: {str(e)}", None
144
 
145
  # UI
146
- with gr.Blocks(title="Myanmar OpenVoice V2") as demo:
147
- gr.Markdown("# πŸ‡²πŸ‡² Myanmar Voice Cloning (OpenVoice V2)")
148
- gr.Markdown("Corrected Download Source (No 403 Error)")
149
 
150
  with gr.Row():
151
  with gr.Column():
 
1
  import os
2
+ # --- FORCE CPU (GPU Error ရှောင်ရန်) ---
3
  os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
4
 
5
  import sys
 
8
  import edge_tts
9
  import asyncio
10
  import shutil
 
11
  from huggingface_hub import hf_hub_download
12
 
13
  # PyTorch CPU Mode
14
  torch.cuda.is_available = lambda : False
15
+ print(f"πŸš€ System Mode: CPU Only (OpenVoice V1)")
16
 
17
+ # --- 1. Setup OpenVoice V1 ---
18
  if not os.path.exists("OpenVoice"):
19
+ print("Cloning OpenVoice V1...")
20
  os.system("git clone https://github.com/myshell-ai/OpenVoice.git")
21
 
22
  sys.path.append(os.path.abspath("OpenVoice"))
23
 
24
+ # V1 Checkpoints Folder
25
+ os.makedirs("checkpoints/converter", exist_ok=True)
26
 
27
+ # V1 Model Download (Hugging Face Source)
28
+ def download_v1_models():
29
+ if not os.path.exists("checkpoints/converter/checkpoint.pth"):
30
+ print("Downloading V1 Model from Hugging Face...")
 
31
  try:
32
+ # Config File
33
+ hf_hub_download(repo_id="myshell-ai/OpenVoice", filename="checkpoints/converter/config.json", local_dir=".", local_dir_use_symlinks=False)
34
+ # Model File (V1)
35
+ hf_hub_download(repo_id="myshell-ai/OpenVoice", filename="checkpoints/converter/checkpoint.pth", local_dir=".", local_dir_use_symlinks=False)
36
+ print("βœ… V1 Model Downloaded!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  except Exception as e:
38
  print(f"Download Error: {e}")
39
 
40
+ download_v1_models()
41
 
42
+ # Import Modules
43
  try:
44
  from openvoice.api import ToneColorConverter
45
  from openvoice import se_extractor
 
48
  from api import ToneColorConverter
49
  import se_extractor
50
 
51
+ # --- 2. Load V1 Model ---
52
+ ckpt_converter = 'checkpoints/converter'
53
  if not os.path.exists(f"{ckpt_converter}/config.json"):
54
+ # Fallback logic
55
+ ckpt_converter = 'OpenVoice/checkpoints/converter'
56
 
57
+ print(f"Loading V1 Model...")
58
  try:
59
+ # V1 Model Load (CPU)
60
  tone_color_converter = ToneColorConverter(f'{ckpt_converter}/config.json', device='cpu')
61
  tone_color_converter.load_ckpt(f'{ckpt_converter}/checkpoint.pth')
62
+ print("βœ… V1 Model Loaded Successfully!")
63
  except Exception as e:
64
+ print(f"Model Load Error: {e}")
65
  tone_color_converter = None
66
 
67
+ # --- 3. Mastering Engine (Audio Fix) ---
68
  def apply_mastering(input_wav, style="Radio"):
69
  if not shutil.which("ffmpeg"): return input_wav
70
  output_wav = "outputs/mastered_output.wav"
 
81
  return output_wav
82
  except: return input_wav
83
 
84
+ # --- 4. Main Workflow ---
85
  async def run_edge_tts(text, gender):
86
  voice = "my-MM-ThihaNeural" if gender == "Male" else "my-MM-NularNeural"
87
  output_file = "temp_base.mp3"
 
90
 
91
  def predict(text, ref_audio, gender, mastering_style):
92
  if tone_color_converter is None:
93
+ return "System Error: Model failed to load.", None
94
  if not text: return "α€…α€¬α€›α€­α€―α€€α€Ία€‘α€Šα€·α€Ία€•α€«", None
95
  if not ref_audio: return "Reference Audio α€‘α€Šα€·α€Ία€•α€«", None
96
 
 
98
  # Step A: Edge TTS
99
  base_audio = asyncio.run(run_edge_tts(text, gender))
100
 
101
+ # Step B: OpenVoice V1
102
  os.makedirs("outputs", exist_ok=True)
103
 
104
+ # VAD Handling
105
  try:
106
  target_se, _ = se_extractor.get_se(ref_audio, tone_color_converter, target_dir='outputs', vad=True)
107
  except:
 
109
 
110
  source_se, _ = se_extractor.get_se(base_audio, tone_color_converter, target_dir='outputs', vad=False)
111
 
112
+ raw_output = "outputs/raw_v1.wav"
113
 
114
  tone_color_converter.convert(
115
  audio_src_path=base_audio,
 
121
 
122
  # Step C: Mastering
123
  final_output = apply_mastering(raw_output, mastering_style)
124
+ return "Success (V1)!", final_output
125
 
126
  except Exception as e:
127
  import traceback
 
129
  return f"Error: {str(e)}", None
130
 
131
  # UI
132
+ with gr.Blocks(title="Myanmar OpenVoice V1") as demo:
133
+ gr.Markdown("# πŸ‡²πŸ‡² Myanmar Voice Cloning (OpenVoice V1)")
134
+ gr.Markdown("V1 Model + CPU Stable Mode")
135
 
136
  with gr.Row():
137
  with gr.Column():