kochit's picture
Update app.py
b81eac5 verified
Raw
History Blame Contribute Delete
6.51 kB
import os
import torch
import gradio as gr
import edge_tts
import asyncio
import subprocess
from openvoice import se_extractor
from openvoice.api import ToneColorConverter
# ==========================================
# ၁။ Model Load α€œα€―α€•α€Ία€α€Όα€„α€Ία€Έ
# ==========================================
checkpoint_path = "checkpoint.pth"
config_path = "config.json"
tone_color_converter = None
model_error = None
if os.path.exists(checkpoint_path) and os.path.exists(config_path):
try:
device = "cpu"
tone_color_converter = ToneColorConverter(config_path, device=device)
tone_color_converter.load_ckpt(checkpoint_path)
print("βœ… Model Loaded!")
except Exception as e:
model_error = str(e)
else:
model_error = "Files not found!"
os.makedirs("processed", exist_ok=True)
# ==========================================
# ၂။ Professional Mastering Engine (FFmpeg)
# ==========================================
def apply_mastering(input_wav, volume_gain, auto_master, treble_boost):
output_wav = "output_mastered.wav"
# FFmpeg Filters Chain α€α€Šα€Ία€†α€±α€¬α€€α€Ία€α€Όα€„α€Ία€Έ
filters = []
# 1. Volume Gain (α€‘α€žα€Ά α€‘α€α€­α€―α€Έα€‘α€€α€»α€šα€Ί)
# 0dB α€€ α€•α€―α€Άα€™α€Ύα€”α€ΊαŠ +5dB ဆိုရင် α€•α€­α€―α€€α€»α€šα€Ία€™α€šα€Ί
if volume_gain != 0:
filters.append(f"volume={volume_gain}dB")
# 2. Treble Boost (α€‘α€žα€Άα€€α€Όα€Šα€Ία€œα€„α€Ία€…α€±α€›α€”α€Ί High Frequency တင်ခြင်း)
if treble_boost:
filters.append("treble=g=5")
# 3. Auto Mastering (Loudness Normalization)
# ဒါက Studio Quality ရထောင် α€‘α€žα€Άα€€α€­α€― Compression α€œα€―α€•α€Ία€•α€±α€Έα€•α€«α€α€šα€Ί
if auto_master:
filters.append("loudnorm=I=-16:TP=-1.5:LRA=11")
# Filter မရှိရင် မူရင်းထတိုင်းပြန်ပေး
if not filters:
return input_wav
# FFmpeg Command Run ခြင်း
filter_str = ",".join(filters)
cmd = [
"ffmpeg", "-y", "-i", input_wav,
"-af", filter_str,
output_wav
]
print(f"Applying Mastering Filters: {filter_str}")
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if os.path.exists(output_wav):
return output_wav
else:
return input_wav # Error တက်ရင် မူရင်းပဲ α€•α€Όα€”α€Ία€•α€±α€Έα€™α€šα€Ί
# ==========================================
# ၃။ Main Process
# ==========================================
async def generate_base_audio(text, voice, speed, pitch):
output_file = "temp_base.mp3"
rate = f"{int(speed)}%" if speed < 0 else f"+{int(speed)}%"
pitch_str = f"{int(pitch)}Hz" if pitch < 0 else f"+{int(pitch)}Hz"
communicate = edge_tts.Communicate(text, voice, rate=rate, pitch=pitch_str)
await communicate.save(output_file)
return output_file
def voice_clone(text, gender, ref_audio, speed, pitch, volume, auto_master, treble):
if model_error: return None, f"❌ {model_error}"
if tone_color_converter is None: return None, "❌ Model Error"
try:
base_voice = "my-MM-NilarNeural" if gender == "Female (α€™)" else "my-MM-ThihaNeural"
# Step 1: Base Audio
base_audio_path = asyncio.run(generate_base_audio(text, base_voice, speed, pitch))
# Step 2: OpenVoice Conversion (Raw Audio)
source_se, _ = se_extractor.get_se(base_audio_path, tone_color_converter, target_dir='processed', vad=True)
target_se, _ = se_extractor.get_se(ref_audio, tone_color_converter, target_dir='processed', vad=True)
raw_output = "output_raw.wav"
tone_color_converter.convert(
audio_src_path=base_audio_path, src_se=source_se, tgt_se=target_se,
output_path=raw_output
)
# Step 3: Mastering & Boosting
final_output = apply_mastering(raw_output, volume, auto_master, treble)
return final_output, "βœ… α€‘α€±α€¬α€„α€Ία€™α€Όα€„α€Ία€•α€«α€α€šα€Ί (Mastered)"
except Exception as e:
return None, f"❌ Error: {str(e)}"
# ==========================================
# ၄။ UI Design (Pro Version)
# ==========================================
with gr.Blocks(title="Myanmar Voice Master") as demo:
gr.Markdown("# πŸ‡²πŸ‡² Myanmar Voice Cloning (Mastering Edition)")
if model_error: gr.Warning(model_error)
with gr.Row():
with gr.Column():
# Input Section
inp_text = gr.Textbox(label="α€…α€¬α€žα€¬α€Έ (Text)", lines=3, value="α€™α€„α€Ία€Ήα€‚α€œα€¬α€•α€«")
with gr.Row():
inp_gender = gr.Dropdown(["Male (ကျား)", "Female (α€™)"], value="Female (α€™)", label="Gender")
inp_ref = gr.Audio(label="Reference Audio", type="filepath")
# Basic Controls
with gr.Accordion("βš™οΈ Basic Settings (ထခြေခဢ)", open=False):
slider_speed = gr.Slider(-50, 50, value=0, step=5, label="Speed % (ထမြန်နှုန်း)")
slider_pitch = gr.Slider(-20, 20, value=0, step=1, label="Pitch Hz (α€‘α€žα€Άα€”α€­α€™α€·α€Ία€™α€Όα€„α€·α€Ί)")
# Mastering Controls (Highlight)
gr.Markdown("### 🎚️ Studio Mastering (α€‘α€žα€Άα€•α€Όα€―α€•α€Όα€„α€Ία€›α€”α€Ί)")
with gr.Group():
# Volume Booster
slider_vol = gr.Slider(0, 20, value=5, step=1, label="Volume Booster (+dB) - α€‘α€žα€Άα€€α€»α€šα€Ία€›α€”α€Ί")
# Checkboxes
with gr.Row():
chk_master = gr.Checkbox(label="Auto Mastering (α€‘α€žα€Άα€‘α€›α€Šα€Ία€‘α€žα€½α€±α€Έ α€‘α€œα€­α€―α€‘α€œα€»α€±α€¬α€€α€Ία€Šα€Ύα€­α€™α€Šα€Ί)", value=True)
chk_treble = gr.Checkbox(label="Treble Boost (α€‘α€žα€Άα€•α€­α€―α€€α€Όα€Šα€Ία€‘α€±α€¬α€„α€Ία€œα€―α€•α€Ία€™α€Šα€Ί)", value=False)
btn = gr.Button("Generate Mastered Audio", variant="primary")
with gr.Column():
out_audio = gr.Audio(label="Final Output")
out_status = gr.Textbox(label="Status")
btn.click(
fn=voice_clone,
inputs=[inp_text, inp_gender, inp_ref, slider_speed, slider_pitch, slider_vol, chk_master, chk_treble],
outputs=[out_audio, out_status]
)
if __name__ == "__main__":
demo.queue().launch(server_name="0.0.0.0", server_port=7860)