File size: 5,769 Bytes
d9edac2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
{
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# HebTTS Quickstart\n",
        "We created a jupyter notebook in order to easily generate your desired samples!\n",
        "\n",
        "\n",
        "  <a href='https://arxiv.org/abs/2407.12206'><img src='https://img.shields.io/badge/ArXiv-PDF-red'></a> &nbsp;\n",
        "   <a href='https://pages.cs.huji.ac.il/adiyoss-lab/HebTTS/'><img src='https://img.shields.io/badge/Project-Page-Green'></a> &nbsp;\n",
        "  [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1f3-6Dqbna9_hI5C9V4qTIG05dixW-r72?usp=sharing) &nbsp;\n",
        "  [![Open In Colab](https://badges.aleen42.com/src/github.svg)](https://github.com/slp-rl/HebTTS) &nbsp;\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "---\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "3wmzNy2fDSsr"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "56kkbeJahIe1"
      },
      "outputs": [],
      "source": [
        "#@title Install โ€๐Ÿ’ป\n",
        "\n",
        "%%capture\n",
        "! git clone https://github.com/slp-rl/HebTTS.git\n",
        "\n",
        "! pip install torch torchaudio\n",
        "! pip install torchmetrics\n",
        "! pip install omegaconf\n",
        "! pip install git+https://github.com/lhotse-speech/lhotse\n",
        "! pip install librosa\n",
        "! pip install encodec\n",
        "! pip install phonemizer\n",
        "! pip install audiocraft  # optional\n",
        "! gdown https://drive.google.com/uc?id=11NoOJzMLRX9q1C_Q4sX0w2b9miiDjGrv\n",
        "\n",
        "from pathlib import Path\n",
        "import glob\n",
        "import os\n",
        "from IPython.display import Audio, display\n",
        "from pathlib import Path\n",
        "\n",
        "\n",
        "def display_audio(prompt_file):\n",
        "  l=100\n",
        "  speaker = os.path.basename(os.path.dirname((os.path.dirname(prompt_file))))\n",
        "  audio_files = list(Path(os.path.join(os.path.dirname(prompt_file))).rglob(\"*.wav\"))\n",
        "  print(f\"Prompt: {open(prompt_file).read()}\")\n",
        "  print(f\"Speaker: {speaker}\")\n",
        "  print(\"-\"*100)\n",
        "\n",
        "  for audio_file in audio_files:\n",
        "    display(Audio(audio_file, autoplay=False))\n",
        "    print(\"-\"*100)\n",
        "    print()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "uIBF-IFwxLjL",
        "cellView": "form"
      },
      "outputs": [],
      "source": [
        "# @title Enter Hebrew text for generation\n",
        "# @markdown You can concatenate prompts with '|' to generate few samples at once\n",
        "hebrew_text_to_generate = \"ื”ื™ื™ ืžื” ืงื•ืจื” | ื•ื‘ืฉื‘ื™ืœ ืœื”ื‘ื™ืŸ ืœืžื” ืžื—ื™ืจ ื”ื“ืœืง ื›ืœ ื›ืš ืขืœื” ืฆืจื™ืš ืœื—ื–ื•ืจ ืฉื ืชื™ื™ื ืื—ืจื•ื ื™ืช\" # @param {type:\"string\"}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "RFrYpBJsxQW3",
        "cellView": "form"
      },
      "outputs": [],
      "source": [
        "\n",
        "# @title Choose speaker\n",
        "speaker = \"shaul\" # @param [\"\\\"osim\\\"\", \"\\\"geek\\\"\", \"\\\"shaul\\\"\"] {type:\"raw\", allow-input: true}"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Note:** inference may be slower based on the allocated resources by google colab. It is recomended to choose a gpu in `Edit` -> `Notebook settings` -> `Hardware accelerator`."
      ],
      "metadata": {
        "id": "NMvd3VytEJEZ"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "U8Hszx6axgns"
      },
      "outputs": [],
      "source": [
        "#@title Generate!\n",
        "from pathlib import Path\n",
        "\n",
        "speaker_dir = Path(f\"./out/{speaker}\")\n",
        "speaker_dir.mkdir(exist_ok=True, parents=True)\n",
        "i=0\n",
        "if any(speaker_dir.iterdir()):\n",
        "    max(map(lambda x:int(x.name),speaker_dir.glob(\"*\")))+1\n",
        "output_dir = speaker_dir / f\"{i}\"\n",
        "output_dir.mkdir(exist_ok=True, parents=True)\n",
        "prompt_file = output_dir / \"prompt.txt\"\n",
        "with open(prompt_file, \"w\") as f:\n",
        "    f.write(hebrew_text_to_generate)\n",
        "\n",
        "! python HebTTS/infer.py \\\n",
        "    --checkpoint checkpoint.pt \\\n",
        "    --output-dir $output_dir                                             \\\n",
        "    --text \"$hebrew_text_to_generate\" \\\n",
        "    --speaker \"$speaker\" \\\n",
        "    --speaker-yaml HebTTS/speakers/speakers.yaml \\\n",
        "    --tokens-file HebTTS/tokenizer/unique_words_tokens_all.k2symbols \\\n",
        "    --vocab-file HebTTS/tokenizer/vocab.txt \\\n",
        "    --mbd True\n",
        "\n",
        "display_audio(prompt_file)"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "#@title Listen\n",
        "for prompt_file in glob.glob(\"./out/*/*/prompt.txt\"):\n",
        "  display_audio(prompt_file)"
      ],
      "metadata": {
        "cellView": "form",
        "id": "CXiMWmNxJzNZ"
      },
      "execution_count": null,
      "outputs": []
    }
  ],
  "metadata": {
    "colab": {
      "provenance": [],
      "gpuType": "T4"
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU"
  },
  "nbformat": 4,
  "nbformat_minor": 0
}