kochit commited on
Commit
f0b4364
·
verified ·
1 Parent(s): 18a5a62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -19
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
@@ -9,9 +9,9 @@ import edge_tts
9
  import asyncio
10
  import shutil
11
  import zipfile
12
- from huggingface_hub import hf_hub_download
13
 
14
- # PyTorch ကို CPU ပဲသုံးဖို့ အမိန့်ပေးခြင်း
15
  torch.cuda.is_available = lambda : False
16
  print(f"🚀 System Mode: CPU Only (Stable V2)")
17
 
@@ -22,19 +22,33 @@ if not os.path.exists("OpenVoice"):
22
 
23
  sys.path.append(os.path.abspath("OpenVoice"))
24
 
25
- # Checkpoints V2 folder ဆောက်ခြင်း
26
  os.makedirs("checkpoints_v2", exist_ok=True)
27
 
28
- # V2 Model Download Function
29
  def download_v2_models():
30
- # V2 Converter မရှိေးင် Download ဆွဲမယ်
31
  if not os.path.exists("checkpoints_v2/converter/checkpoint.pth"):
32
- print("Downloading OpenVoice V2 Model...")
33
  try:
34
- # Converter Model (Tone Color)
35
- hf_hub_download(repo_id="myshell-ai/OpenVoice", filename="checkpoints_v2/converter/config.json", local_dir=".", local_dir_use_symlinks=False)
36
- hf_hub_download(repo_id="myshell-ai/OpenVoice", filename="checkpoints_v2/converter/checkpoint.pth", local_dir=".", local_dir_use_symlinks=False)
37
- print("✅ V2 Model Downloaded!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  except Exception as e:
39
  print(f"Download Error: {e}")
40
 
@@ -51,18 +65,17 @@ except ImportError:
51
 
52
  # --- 3. Load V2 Model ---
53
  ckpt_converter = 'checkpoints_v2/converter'
54
- if not os.path.exists(f"{ckpt_converter}/config.json"):
55
- # Fallback path logic
56
- ckpt_converter = 'OpenVoice/checkpoints_v2/converter'
57
 
58
- print("Initializing V2 Model on CPU...")
59
  try:
60
- # V2 မှာ device='cpu' လို့ အတိအကျပေးရပါတယ်
61
  tone_color_converter = ToneColorConverter(f'{ckpt_converter}/config.json', device='cpu')
62
  tone_color_converter.load_ckpt(f'{ckpt_converter}/checkpoint.pth')
63
  print("✅ V2 Model Loaded Successfully!")
64
  except Exception as e:
65
  print(f"Model Load Error: {e}")
 
 
66
 
67
  # --- 4. Mastering Engine ---
68
  def apply_mastering(input_wav, style="Radio"):
@@ -89,6 +102,8 @@ async def run_edge_tts(text, gender):
89
  return output_file
90
 
91
  def predict(text, ref_audio, gender, mastering_style):
 
 
92
  if not text: return "စာရိုက်ထည့်ပါ", None
93
  if not ref_audio: return "Reference Audio ထည့်ပါ", None
94
 
@@ -99,7 +114,7 @@ def predict(text, ref_audio, gender, mastering_style):
99
  # Step B: OpenVoice V2 Conversion
100
  os.makedirs("outputs", exist_ok=True)
101
 
102
- # VAD Handling (Try/Except blocks for safety)
103
  try:
104
  target_se, _ = se_extractor.get_se(ref_audio, tone_color_converter, target_dir='outputs', vad=True)
105
  except:
@@ -109,7 +124,6 @@ def predict(text, ref_audio, gender, mastering_style):
109
 
110
  raw_output = "outputs/raw_v2.wav"
111
 
112
- # Actual Conversion
113
  tone_color_converter.convert(
114
  audio_src_path=base_audio,
115
  src_se=source_se,
@@ -123,12 +137,14 @@ def predict(text, ref_audio, gender, mastering_style):
123
  return "Success (V2 Ready)!", final_output
124
 
125
  except Exception as e:
 
 
126
  return f"Error: {str(e)}", None
127
 
128
  # UI
129
  with gr.Blocks(title="Myanmar OpenVoice V2") as demo:
130
  gr.Markdown("# 🇲🇲 Myanmar Voice Cloning (OpenVoice V2)")
131
- gr.Markdown("V2 Model + CPU Stable Version")
132
 
133
  with gr.Row():
134
  with gr.Column():
 
1
  import os
2
+ # --- 1. FORCE CPU (Driver Error ကာကွယ်ရ်) ---
3
  os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
4
 
5
  import sys
 
9
  import asyncio
10
  import shutil
11
  import zipfile
12
+ import urllib.request
13
 
14
+ # PyTorch CPU Mode
15
  torch.cuda.is_available = lambda : False
16
  print(f"🚀 System Mode: CPU Only (Stable V2)")
17
 
 
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 (Official S3 Link)
29
  def download_v2_models():
30
+ # Model ရှိမရှိ စစ်ဆေးခြင်
31
  if not os.path.exists("checkpoints_v2/converter/checkpoint.pth"):
32
+ print("Downloading OpenVoice V2 Model from AWS...")
33
  try:
34
+ # 1. Zip ဖိုင်ကို ဒေါင်းလုပ်ဆွဲခြင်း
35
+ url = "https://myshell-public-repo-hosting.s3.amazonaws.com/openvoice/checkpoints_v2_0417.zip"
36
+ zip_path = "checkpoints_v2.zip"
37
+ urllib.request.urlretrieve(url, zip_path)
38
+
39
+ # 2. ဖြည်ချခြင်း (Unzip)
40
+ print("Extracting Model...")
41
+ with zipfile.ZipFile(zip_path, 'r') as zip_ref:
42
+ zip_ref.extractall(".")
43
+
44
+ # 3. Folder နာမည်ပြန်ညှိခြင်း (လိုအပ်လျှင်)
45
+ if os.path.exists("checkpoints_v2_0417"):
46
+ # အဟောင်းဖျက်ပြီး အသစ်နေရာချ
47
+ if os.path.exists("checkpoints_v2"):
48
+ shutil.rmtree("checkpoints_v2")
49
+ os.rename("checkpoints_v2_0417", "checkpoints_v2")
50
+
51
+ print("✅ V2 Model Ready!")
52
  except Exception as e:
53
  print(f"Download Error: {e}")
54
 
 
65
 
66
  # --- 3. Load V2 Model ---
67
  ckpt_converter = 'checkpoints_v2/converter'
 
 
 
68
 
69
+ print(f"Loading V2 Model from {ckpt_converter}...")
70
  try:
71
+ # V2 requires 'device' argument
72
  tone_color_converter = ToneColorConverter(f'{ckpt_converter}/config.json', device='cpu')
73
  tone_color_converter.load_ckpt(f'{ckpt_converter}/checkpoint.pth')
74
  print("✅ V2 Model Loaded Successfully!")
75
  except Exception as e:
76
  print(f"Model Load Error: {e}")
77
+ # Error တက်ရင် Variable ကို None ပေးထားမည် (UI မှာ Crash မဖြစ်အောင်)
78
+ tone_color_converter = None
79
 
80
  # --- 4. Mastering Engine ---
81
  def apply_mastering(input_wav, style="Radio"):
 
102
  return output_file
103
 
104
  def predict(text, ref_audio, gender, mastering_style):
105
+ if tone_color_converter is None:
106
+ return "Critical Error: Model not loaded. Check logs.", None
107
  if not text: return "စာရိုက်ထည့်ပါ", None
108
  if not ref_audio: return "Reference Audio ထည့်ပါ", None
109
 
 
114
  # Step B: OpenVoice V2 Conversion
115
  os.makedirs("outputs", exist_ok=True)
116
 
117
+ # VAD Handling
118
  try:
119
  target_se, _ = se_extractor.get_se(ref_audio, tone_color_converter, target_dir='outputs', vad=True)
120
  except:
 
124
 
125
  raw_output = "outputs/raw_v2.wav"
126
 
 
127
  tone_color_converter.convert(
128
  audio_src_path=base_audio,
129
  src_se=source_se,
 
137
  return "Success (V2 Ready)!", final_output
138
 
139
  except Exception as e:
140
+ import traceback
141
+ traceback.print_exc()
142
  return f"Error: {str(e)}", None
143
 
144
  # UI
145
  with gr.Blocks(title="Myanmar OpenVoice V2") as demo:
146
  gr.Markdown("# 🇲🇲 Myanmar Voice Cloning (OpenVoice V2)")
147
+ gr.Markdown("V2 Model (AWS Source) + CPU Stable")
148
 
149
  with gr.Row():
150
  with gr.Column():