Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
-
# --- 1. FORCE CPU (
|
| 3 |
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 4 |
|
| 5 |
import sys
|
|
@@ -9,7 +9,7 @@ import edge_tts
|
|
| 9 |
import asyncio
|
| 10 |
import shutil
|
| 11 |
import zipfile
|
| 12 |
-
import
|
| 13 |
|
| 14 |
# PyTorch CPU Mode
|
| 15 |
torch.cuda.is_available = lambda : False
|
|
@@ -22,33 +22,34 @@ if not os.path.exists("OpenVoice"):
|
|
| 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 (
|
| 29 |
def download_v2_models():
|
| 30 |
# Model ရှိမရှိ စစ်ဆေးခြင်း
|
| 31 |
if not os.path.exists("checkpoints_v2/converter/checkpoint.pth"):
|
| 32 |
-
print("Downloading OpenVoice V2
|
| 33 |
try:
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
print("Extracting Model...")
|
| 41 |
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 42 |
zip_ref.extractall(".")
|
| 43 |
|
| 44 |
-
#
|
| 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
|
| 52 |
except Exception as e:
|
| 53 |
print(f"Download Error: {e}")
|
| 54 |
|
|
@@ -65,16 +66,17 @@ except ImportError:
|
|
| 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"
|
| 77 |
-
# Error တက်ရင် Variable ကို None ပေးထားမည် (UI မှာ Crash မဖြစ်အောင်)
|
| 78 |
tone_color_converter = None
|
| 79 |
|
| 80 |
# --- 4. Mastering Engine ---
|
|
@@ -103,18 +105,17 @@ async def run_edge_tts(text, gender):
|
|
| 103 |
|
| 104 |
def predict(text, ref_audio, gender, mastering_style):
|
| 105 |
if tone_color_converter is None:
|
| 106 |
-
return "
|
| 107 |
if not text: return "စာရိုက်ထည့်ပါ", None
|
| 108 |
if not ref_audio: return "Reference Audio ထည့်ပါ", None
|
| 109 |
|
| 110 |
try:
|
| 111 |
-
# Step A: Edge TTS
|
| 112 |
base_audio = asyncio.run(run_edge_tts(text, gender))
|
| 113 |
|
| 114 |
-
# Step B: OpenVoice V2
|
| 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:
|
|
@@ -144,7 +145,7 @@ def predict(text, ref_audio, gender, mastering_style):
|
|
| 144 |
# UI
|
| 145 |
with gr.Blocks(title="Myanmar OpenVoice V2") as demo:
|
| 146 |
gr.Markdown("# 🇲🇲 Myanmar Voice Cloning (OpenVoice V2)")
|
| 147 |
-
gr.Markdown("
|
| 148 |
|
| 149 |
with gr.Row():
|
| 150 |
with gr.Column():
|
|
|
|
| 1 |
import os
|
| 2 |
+
# --- 1. FORCE CPU (GPU Error ရှောင်ရန်) ---
|
| 3 |
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 4 |
|
| 5 |
import sys
|
|
|
|
| 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
|
|
|
|
| 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 |
|
|
|
|
| 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 ---
|
|
|
|
| 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 |
|
| 112 |
try:
|
| 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:
|
|
|
|
| 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():
|