Delete app.py
Browse files
app.py
DELETED
|
@@ -1,79 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import tempfile
|
| 3 |
-
|
| 4 |
-
import gradio as gr
|
| 5 |
-
from huggingface_hub import snapshot_download
|
| 6 |
-
|
| 7 |
-
from openvoice import se_extractor
|
| 8 |
-
from openvoice.api import ToneColorConverter
|
| 9 |
-
|
| 10 |
-
DEVICE = "cpu"
|
| 11 |
-
|
| 12 |
-
# Downloads OpenVoice V2's tone-color converter checkpoint from the HF Hub
|
| 13 |
-
# the first time the Space boots. Cached afterwards in the Space's storage.
|
| 14 |
-
CKPT_DIR = snapshot_download(repo_id="myshell-ai/OpenVoiceV2")
|
| 15 |
-
CONVERTER_DIR = os.path.join(CKPT_DIR, "converter")
|
| 16 |
-
|
| 17 |
-
tone_color_converter = ToneColorConverter(
|
| 18 |
-
os.path.join(CONVERTER_DIR, "config.json"), device=DEVICE
|
| 19 |
-
)
|
| 20 |
-
tone_color_converter.load_ckpt(os.path.join(CONVERTER_DIR, "checkpoint.pth"))
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
def convert_voice(source_audio, elderly_reference_audio):
|
| 24 |
-
if source_audio is None or elderly_reference_audio is None:
|
| 25 |
-
raise gr.Error("请先上传「原始录音」和「老年人参考音频」两个文件")
|
| 26 |
-
|
| 27 |
-
with tempfile.TemporaryDirectory() as tmp:
|
| 28 |
-
# vad=True uses lightweight Silero VAD to find clean speech segments
|
| 29 |
-
# (no transcription needed), which is enough for tone-color extraction.
|
| 30 |
-
src_se, _ = se_extractor.get_se(
|
| 31 |
-
source_audio, tone_color_converter, target_dir=tmp, vad=True
|
| 32 |
-
)
|
| 33 |
-
tgt_se, _ = se_extractor.get_se(
|
| 34 |
-
elderly_reference_audio, tone_color_converter, target_dir=tmp, vad=True
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
output_path = os.path.join(tmp, "output.wav")
|
| 38 |
-
tone_color_converter.convert(
|
| 39 |
-
audio_src_path=source_audio,
|
| 40 |
-
src_se=src_se,
|
| 41 |
-
tgt_se=tgt_se,
|
| 42 |
-
output_path=output_path,
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
with open(output_path, "rb") as f:
|
| 46 |
-
data = f.read()
|
| 47 |
-
|
| 48 |
-
# Write result outside the tempdir so Gradio can still read it after cleanup
|
| 49 |
-
final_path = tempfile.NamedTemporaryFile(suffix=".wav", delete=False).name
|
| 50 |
-
with open(final_path, "wb") as f:
|
| 51 |
-
f.write(data)
|
| 52 |
-
return final_path
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
with gr.Blocks(title="老年声音转换") as demo:
|
| 56 |
-
gr.Markdown(
|
| 57 |
-
"# 声音转老年音色\n"
|
| 58 |
-
"1. 上传要转换的原始录音\n"
|
| 59 |
-
"2. 上传一段几秒钟的老年人声音样本(参考音色)\n"
|
| 60 |
-
"3. 点击「开始转换」\n\n"
|
| 61 |
-
"基于 [OpenVoice V2](https://github.com/myshell-ai/OpenVoice) 的零样本音色转换,"
|
| 62 |
-
"CPU 上单次转换大约需要 10~30 秒,请耐心等待。"
|
| 63 |
-
)
|
| 64 |
-
with gr.Row():
|
| 65 |
-
source_audio = gr.Audio(label="要转换的原始录音", type="filepath")
|
| 66 |
-
elderly_reference_audio = gr.Audio(
|
| 67 |
-
label="老年人参考音频(几秒钟即可)", type="filepath"
|
| 68 |
-
)
|
| 69 |
-
convert_btn = gr.Button("开始转换", variant="primary")
|
| 70 |
-
output_audio = gr.Audio(label="转换结果", type="filepath")
|
| 71 |
-
|
| 72 |
-
convert_btn.click(
|
| 73 |
-
fn=convert_voice,
|
| 74 |
-
inputs=[source_audio, elderly_reference_audio],
|
| 75 |
-
outputs=output_audio,
|
| 76 |
-
)
|
| 77 |
-
|
| 78 |
-
if __name__ == "__main__":
|
| 79 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|