File size: 4,914 Bytes
650ef1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "name": "Training_Example.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "2zx09MSGFHjT"
      },
      "source": [
        "!pip install deep-phonemizer"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "oZ1L1BlhOUMR"
      },
      "source": [
        "# Dowload and prepare a dataset\n",
        "!wget https://raw.githubusercontent.com/CUNY-CL/wikipron/master/data/scrape/tsv/eng_latn_us_broad.tsv\n",
        "\n",
        "with open('eng_latn_us_broad.tsv', 'r', encoding='utf-8') as f:\n",
        "  lines = f.readlines()\n",
        "\n",
        "# Prepare data as tuples (lang, word, phoneme)\n",
        "lines = [l.replace(' ', '').replace('\\n', '') for l in lines]\n",
        "splits = [l.split('\\t') for l in lines]\n",
        "train_data = [('en_us', s[0], s[1]) for s in splits if len(s)==2]\n",
        "\n",
        "for d in train_data[:10000:1000]:\n",
        "  print(d)\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "MiV-OUi0FQ-O"
      },
      "source": [
        "# Read standard config and adjust some params for speedup\n",
        "from dp.utils.io import read_config, save_config\n",
        "import dp\n",
        "import os\n",
        "\n",
        "config_file = os.path.dirname(dp.__file__) + '/configs/forward_config.yaml'\n",
        "config = read_config(config_file)\n",
        "config['training']['epochs'] = 10\n",
        "config['training']['warmup_steps'] = 100\n",
        "config['training']['generate_steps'] = 500\n",
        "config['training']['validate_steps'] = 500\n",
        "save_config(config, 'config.yaml')\n",
        "\n",
        "for k, v in config.items():\n",
        "  print(f'{k} {v}')\n",
        "\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "m_SNVv_HN4fR"
      },
      "source": [
        "%load_ext tensorboard\n",
        "%tensorboard --logdir /content/checkpoints"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "K09wG-ymXECZ"
      },
      "source": [
        "from dp.preprocess import preprocess\n",
        "from dp.train import train\n",
        "\n",
        "preprocess(config_file='config.yaml', train_data=train_data)\n",
        "train(config_file='config.yaml')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2KcIwL6QdvEJ",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "ed499929-1103-4a22-dce8-4a54d85f9b9e"
      },
      "source": [
        "# Load phonemizer (including the training data dictionary)\n",
        "from dp.phonemizer import Phonemizer\n",
        "\n",
        "phonemizer = Phonemizer.from_checkpoint('/content/checkpoints/best_model.pt')\n",
        "result = phonemizer('Phonemizing an English text is imposimpable!', lang='en_us')\n",
        "\n",
        "print(result)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "2021-05-12 10:15:19,916.916 DEBUG phonemizer:  Initializing phonemizer with model step 18000\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "fənəmaɪzɪŋ ən ɪŋɡlɪʃ tɛkst ɪz ɪmpɑsɪmpəbəl!\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Xt85fzFneDno",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "bc1cb892-9cd2-4540-b930-e4443ddf69c2"
      },
      "source": [
        "# Phonemize a list of texts and pull out model predictions with confidence scores\n",
        "result = phonemizer.phonemise_list(['Phonemizing an US-English text is imposimpable!'], lang='en_us')\n",
        "\n",
        "for word, pred in result.predictions.items():\n",
        "  print(f'{word} {pred.phonemes} {pred.confidence}')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "imposimpable ɪmpɑsɪmpəbəl 0.2185952042855603\n",
            "Phonemizing fənəmaɪzɪŋ 0.22222847233670942\n"
          ],
          "name": "stdout"
        }
      ]
    }
  ]
}