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 9 |
+
"Run the **v3** model on a free Colab GPU.\n",
|
| 10 |
+
"**First: Runtime → Change runtime type → T4 GPU.**"
|
| 11 |
+
]
|
| 12 |
+
},
|
| 13 |
+
{
|
| 14 |
+
"cell_type": "code",
|
| 15 |
+
"metadata": {},
|
| 16 |
+
"execution_count": null,
|
| 17 |
+
"outputs": [],
|
| 18 |
+
"source": [
|
| 19 |
+
"!pip install -q omnivoice catt-tashkeel num2words soundfile"
|
| 20 |
+
]
|
| 21 |
+
},
|
| 22 |
+
{
|
| 23 |
+
"cell_type": "markdown",
|
| 24 |
+
"metadata": {},
|
| 25 |
+
"source": [
|
| 26 |
+
"### Log in to Hugging Face (private repo — use a READ token)"
|
| 27 |
+
]
|
| 28 |
+
},
|
| 29 |
+
{
|
| 30 |
+
"cell_type": "code",
|
| 31 |
+
"metadata": {},
|
| 32 |
+
"execution_count": null,
|
| 33 |
+
"outputs": [],
|
| 34 |
+
"source": [
|
| 35 |
+
"from huggingface_hub import login\n",
|
| 36 |
+
"login()"
|
| 37 |
+
]
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"cell_type": "markdown",
|
| 41 |
+
"metadata": {},
|
| 42 |
+
"source": [
|
| 43 |
+
"### Load model + reference voice"
|
| 44 |
+
]
|
| 45 |
+
},
|
| 46 |
+
{
|
| 47 |
+
"cell_type": "code",
|
| 48 |
+
"metadata": {},
|
| 49 |
+
"execution_count": null,
|
| 50 |
+
"outputs": [],
|
| 51 |
+
"source": [
|
| 52 |
+
"import torch\n",
|
| 53 |
+
"from omnivoice.models.omnivoice import OmniVoice\n",
|
| 54 |
+
"from huggingface_hub import hf_hub_download\n",
|
| 55 |
+
"\n",
|
| 56 |
+
"REPO = \"ehabnegm/lahgtna-omnivoice-egyptian-v3\"\n",
|
| 57 |
+
"model = OmniVoice.from_pretrained(REPO, device_map=\"cuda\", dtype=torch.float16)\n",
|
| 58 |
+
"ref_audio = hf_hub_download(REPO, \"reference.wav\")\n",
|
| 59 |
+
"ref_text = \"كان العمل التطوعي واللي لما تفتح الباب بس ليه الناس\""
|
| 60 |
+
]
|
| 61 |
+
},
|
| 62 |
+
{
|
| 63 |
+
"cell_type": "markdown",
|
| 64 |
+
"metadata": {},
|
| 65 |
+
"source": [
|
| 66 |
+
"### Text front-end + long-form helper (chunk → no drift)"
|
| 67 |
+
]
|
| 68 |
+
},
|
| 69 |
+
{
|
| 70 |
+
"cell_type": "code",
|
| 71 |
+
"metadata": {},
|
| 72 |
+
"execution_count": null,
|
| 73 |
+
"outputs": [],
|
| 74 |
+
"source": [
|
| 75 |
+
"import re, numpy as np, soundfile as sf\n",
|
| 76 |
+
"from num2words import num2words\n",
|
| 77 |
+
"_DIGITS = str.maketrans(\"٠١٢٣٤٥٦٧٨٩\", \"0123456789\")\n",
|
| 78 |
+
"HARAKAT = re.compile(r\"[ً-ْٰـ]\")\n",
|
| 79 |
+
"def prep(t):\n",
|
| 80 |
+
" t = t.translate(_DIGITS)\n",
|
| 81 |
+
" t = re.sub(r\"\\d+\", lambda m: \" \" + num2words(int(m.group()), lang=\"ar\") + \" \", t)\n",
|
| 82 |
+
" t = re.sub(r\"[A-Za-z]+\", \" \", t)\n",
|
| 83 |
+
" t = HARAKAT.sub(\"\", t).replace(\"ى\", \"ي\")\n",
|
| 84 |
+
" return re.sub(r\"\\s+\", \" \", t).strip()\n",
|
| 85 |
+
"def chunks(t):\n",
|
| 86 |
+
" out, cur = [], \"\"\n",
|
| 87 |
+
" for p in re.split(r\"([،؛\\.؟!\\n]+)\", t):\n",
|
| 88 |
+
" cur += p\n",
|
| 89 |
+
" if re.search(r\"[،؛\\.؟!\\n]\", p):\n",
|
| 90 |
+
" if cur.strip(): out.append(cur.strip())\n",
|
| 91 |
+
" cur = \"\"\n",
|
| 92 |
+
" if cur.strip(): out.append(cur.strip())\n",
|
| 93 |
+
" return out or [t]\n",
|
| 94 |
+
"def synth(text, num_step=16):\n",
|
| 95 |
+
" parts = [model.generate(text=prep(c), language=\"arz\", ref_audio=ref_audio, ref_text=ref_text, num_step=num_step)[0] for c in chunks(text) if prep(c)]\n",
|
| 96 |
+
" return np.concatenate(parts)"
|
| 97 |
+
]
|
| 98 |
+
},
|
| 99 |
+
{
|
| 100 |
+
"cell_type": "markdown",
|
| 101 |
+
"metadata": {},
|
| 102 |
+
"source": [
|
| 103 |
+
"### Generate & play"
|
| 104 |
+
]
|
| 105 |
+
},
|
| 106 |
+
{
|
| 107 |
+
"cell_type": "code",
|
| 108 |
+
"metadata": {},
|
| 109 |
+
"execution_count": null,
|
| 110 |
+
"outputs": [],
|
| 111 |
+
"source": [
|
| 112 |
+
"from IPython.display import Audio\n",
|
| 113 |
+
"text = \"أهلاً بحضرتك، معاك المساعد الذكي. اتفضل قوللي محتاج إيه؟\"\n",
|
| 114 |
+
"sf.write(\"out.wav\", synth(text), 24000)\n",
|
| 115 |
+
"Audio(\"out.wav\")"
|
| 116 |
+
]
|
| 117 |
+
}
|
| 118 |
+
],
|
| 119 |
+
"metadata": {
|
| 120 |
+
"accelerator": "GPU",
|
| 121 |
+
"colab": {
|
| 122 |
+
"provenance": []
|
| 123 |
+
},
|
| 124 |
+
"kernelspec": {
|
| 125 |
+
"name": "python3",
|
| 126 |
+
"display_name": "Python 3"
|
| 127 |
+
}
|
| 128 |
+
},
|
| 129 |
+
"nbformat": 4,
|
| 130 |
+
"nbformat_minor": 0
|
| 131 |
+
}
|