{ "cells": [ { "cell_type": "markdown", "id": "029da2d4", "metadata": {}, "source": [ "# OmniVoice — Lồng tiếng tiếng Việt trên Colab\n", "\n", "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/YOUR_USER/Omivoice/blob/main/hf/colab/Omivoice_VI_Colab.ipynb)\n", "\n", "Notebook chạy [OmniVoice](https://huggingface.co/k2-fsa/OmniVoice) với **6 giọng Việt** và pipeline **SRT lồng tiếng** (`native` — không cắt ngắt, tràn vào khoảng trống sau cue).\n", "\n", "**Yêu cầu:** Runtime **GPU** (T4+).\n", "\n", "**Trước khi chạy:** sửa `HF_REPO` thành dataset Hugging Face bạn đã upload." ] }, { "cell_type": "markdown", "id": "26e11674", "metadata": {}, "source": [ "## 0. Cấu hình" ] }, { "cell_type": "code", "execution_count": null, "id": "899b610f", "metadata": {}, "outputs": [], "source": [ "# Repo dataset trên HF — xem hf/README.md\n", "HF_REPO = \"STBack23/omnivoice-vi\"\n", "\n", "DEFAULT_VOICE = \"ngoc_huyen\"\n", "MERGE_MODE = \"native\" # native | cascade | fit | strict\n", "NUM_STEP = 32" ] }, { "cell_type": "markdown", "id": "a96c5856", "metadata": {}, "source": [ "## 1. Kiểm tra GPU & cài đặt" ] }, { "cell_type": "code", "execution_count": null, "id": "52d2b7f3", "metadata": {}, "outputs": [], "source": [ "import torch\n", "assert torch.cuda.is_available(), \"Bật GPU: Runtime → Change runtime type → T4 GPU\"\n", "print(f\"GPU: {torch.cuda.get_device_name(0)}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "ef948472", "metadata": {}, "outputs": [], "source": [ "!pip install -q omnivoice audiotsm huggingface_hub soundfile librosa" ] }, { "cell_type": "markdown", "id": "12ea36ff", "metadata": {}, "source": [ "## 2. Tải giọng & công cụ từ Hugging Face" ] }, { "cell_type": "code", "execution_count": null, "id": "ef3f54c5", "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "from huggingface_hub import snapshot_download\n", "\n", "ROOT = Path(snapshot_download(HF_REPO, repo_type=\"dataset\", local_dir=\"/content/omnivoice-vi\"))\n", "print(f\"Đã tải: {ROOT}\")\n", "print(\"Giọng:\", [p.parent.name for p in sorted((ROOT / \"voices\").glob(\"*/profile.json\"))])" ] }, { "cell_type": "code", "execution_count": null, "id": "86a2c0ac", "metadata": {}, "outputs": [], "source": [ "import importlib.util\n", "import sys\n", "\n", "speak_path = ROOT / \"tools\" / \"speak.py\"\n", "spec = importlib.util.spec_from_file_location(\"speak\", speak_path)\n", "speak = importlib.util.module_from_spec(spec)\n", "sys.modules[\"speak\"] = speak\n", "spec.loader.exec_module(speak)\n", "print(\"Đã load speak.py\")" ] }, { "cell_type": "markdown", "id": "2f09d0f9", "metadata": {}, "source": [ "## 3. Nạp model (~2–3 phút lần đầu)" ] }, { "cell_type": "code", "execution_count": null, "id": "ac82e5d7", "metadata": {}, "outputs": [], "source": [ "PROFILE = speak.load_profile(ROOT / \"voices\" / DEFAULT_VOICE / \"profile.json\")\n", "MODEL = speak.load_model(PROFILE)\n", "PROMPT = speak.ensure_voice_prompt(MODEL, PROFILE)\n", "print(f\"Giọng: {PROFILE['name']} | SR: {MODEL.sampling_rate}\")" ] }, { "cell_type": "markdown", "id": "52684ad7", "metadata": {}, "source": [ "## 4. Đọc thử một câu" ] }, { "cell_type": "code", "execution_count": null, "id": "657ecf07", "metadata": {}, "outputs": [], "source": [ "import soundfile as sf\n", "from IPython.display import Audio, display\n", "\n", "TEXT = \"Xin chào, đây là thử nghiệm lồng tiếng tiếng Việt bằng OmniVoice trên Colab.\"\n", "audio = speak.synthesize(MODEL, PROMPT, TEXT, PROFILE[\"language\"], num_step=NUM_STEP)\n", "sf.write(\"/content/demo.wav\", audio, MODEL.sampling_rate)\n", "display(Audio(\"/content/demo.wav\"))" ] }, { "cell_type": "markdown", "id": "e037e417", "metadata": {}, "source": [ "## 5. Đổi giọng" ] }, { "cell_type": "code", "execution_count": null, "id": "67b577c2", "metadata": {}, "outputs": [], "source": [ "def load_voice(slug: str):\n", " global PROFILE, PROMPT\n", " PROFILE = speak.load_profile(ROOT / \"voices\" / slug / \"profile.json\")\n", " PROMPT = speak.ensure_voice_prompt(MODEL, PROFILE)\n", " print(f\"Đã chọn: {PROFILE['name']} ({slug})\")\n", "\n", "load_voice(\"ban_mai\")" ] }, { "cell_type": "markdown", "id": "ed3c3013", "metadata": {}, "source": [ "## 6. Lồng tiếng file SRT\n", "\n", "Upload `.srt`, ghép WAV với mode `native`." ] }, { "cell_type": "code", "execution_count": null, "id": "33f7fbc1", "metadata": {}, "outputs": [], "source": [ "from google.colab import files\n", "\n", "print(\"Upload file .srt:\")\n", "uploaded = files.upload()\n", "SRT_PATH = Path(\"/content\") / list(uploaded.keys())[0]\n", "print(f\"File: {SRT_PATH}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "dcc05960", "metadata": {}, "outputs": [], "source": [ "profile_path = ROOT / \"voices\" / PROFILE[\"slug\"] / \"profile.json\"\n", "\n", "result = speak.run_srt_pipeline(\n", " profile_path,\n", " SRT_PATH,\n", " merge=True,\n", " merge_mode=MERGE_MODE,\n", " num_step=NUM_STEP,\n", " model=MODEL,\n", " prompt=PROMPT,\n", ")\n", "\n", "print(result[\"log\"])\n", "merged = result.get(\"merged_wav\")\n", "if merged:\n", " display(Audio(merged))\n", " files.download(merged)" ] }, { "cell_type": "markdown", "id": "a2e288e3", "metadata": {}, "source": [ "## 7. Clone giọng mới (tùy chọn)\n", "\n", "Upload audio mẫu 3–10 giây. `REF_TEXT = None` để Whisper tự nhận." ] }, { "cell_type": "code", "execution_count": null, "id": "09ec9960", "metadata": {}, "outputs": [], "source": [ "print(\"Upload ref audio (wav/mp3):\")\n", "ref_up = files.upload()\n", "REF_AUDIO = Path(\"/content\") / list(ref_up.keys())[0]\n", "REF_TEXT = None\n", "\n", "clone_prompt = MODEL.create_voice_clone_prompt(\n", " ref_audio=str(REF_AUDIO),\n", " ref_text=REF_TEXT,\n", " preprocess_prompt=True,\n", ")\n", "clone_audio = speak.synthesize(\n", " MODEL, clone_prompt,\n", " \"Đây là giọng clone từ audio mẫu bạn vừa upload.\",\n", " \"Vietnamese\", num_step=NUM_STEP,\n", ")\n", "sf.write(\"/content/clone.wav\", clone_audio, MODEL.sampling_rate)\n", "display(Audio(\"/content/clone.wav\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8. Giải phóng VRAM" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "del MODEL, PROMPT\n", "import gc\n", "gc.collect()\n", "torch.cuda.empty_cache()\n", "print(\"Đã giải phóng VRAM\")" ] } ], "metadata": { "accelerator": "GPU", "colab": { "gpuType": "T4", "provenance": [] }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.10.0" } }, "nbformat": 4, "nbformat_minor": 5 }