File size: 11,642 Bytes
64db5cc
8e0957b
64db5cc
8e0957b
 
a2a351d
 
8e0957b
a2a351d
8e0957b
16826f3
a2a351d
64db5cc
8e0957b
 
 
 
 
a414844
 
 
 
 
8e0957b
 
 
9eb519e
 
 
8e0957b
 
9eb519e
 
 
 
 
 
8e0957b
 
9eb519e
8e0957b
9eb519e
 
8e0957b
 
 
 
9eb519e
 
8e0957b
 
9eb519e
 
8e0957b
 
 
 
 
 
 
9eb519e
8e0957b
 
9eb519e
8e0957b
 
 
 
f8c9e01
 
 
 
 
 
 
 
 
 
 
8e0957b
 
 
a2a351d
8e0957b
 
 
a2a351d
8e0957b
 
 
 
5d4d4d4
8e0957b
 
 
 
 
 
a414844
64db5cc
 
 
 
8e0957b
a2a351d
 
 
 
 
 
 
 
8e0957b
 
 
 
 
 
 
 
 
64db5cc
 
 
 
 
 
8e0957b
 
 
 
 
a2a351d
 
 
 
 
 
 
8e0957b
f8c9e01
5d4d4d4
64db5cc
 
 
 
 
 
 
 
 
 
 
 
8e0957b
64db5cc
8e0957b
 
 
 
 
 
 
 
 
a2a351d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e0957b
 
 
 
 
 
 
 
 
a2a351d
 
 
 
 
 
 
 
 
 
 
 
 
8e0957b
64db5cc
8e0957b
 
 
 
 
 
 
 
 
9eb519e
8e0957b
 
 
 
 
 
 
 
 
 
 
 
 
34d0553
8e0957b
 
9eb519e
8e0957b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34d0553
8e0957b
 
 
 
 
 
 
 
 
 
 
 
 
34d0553
 
 
 
 
64db5cc
 
 
 
 
 
 
 
 
 
 
 
 
34d0553
 
64db5cc
34d0553
8e0957b
 
 
a2a351d
8e0957b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d4d4d4
a2a351d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e0957b
 
 
 
 
 
a2a351d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import { useEffect, useRef, useState } from 'react';
import { AudioPlayer } from './AudioPlayer';
import { Podcast } from '../utils/types';
import { parse } from 'yaml';
import {
  audioBufferToMp3,
  isBlogMode,
  pickRand,
  uploadFileToHub,
} from '../utils/utils';

import { getBlogComment } from '../utils/prompts';
import { pipelineGeneratePodcast } from '../utils/pipeline';

const SPEEDS = [
  { name: 'slow AF', value: 0.8 },
  { name: 'slow', value: 0.9 },
  { name: 'a bit slow', value: 1.0 },
  { name: 'natural', value: 1.1 },
  { name: 'most natural', value: 1.2 },
  { name: 'a bit fast', value: 1.3 },
  { name: 'fast!', value: 1.4 },
  { name: 'fast AF', value: 1.5 },
];

const SPEAKERS = [
  { name: 'πŸ‡ΊπŸ‡Έ 🚺 Heart', value: 'af_heart' },
  { name: 'πŸ‡ΊπŸ‡Έ 🚺 Bella πŸ‘', value: 'af_bella' },
  { name: 'πŸ‡ΊπŸ‡Έ 🚺 Nicole 🎧', value: 'af_nicole' },
  { name: 'πŸ‡ΊπŸ‡Έ 🚺 Aoede', value: 'af_aoede' },
  { name: 'πŸ‡ΊπŸ‡Έ 🚺 Kore', value: 'af_kore' },
  { name: 'πŸ‡ΊπŸ‡Έ 🚺 Sarah πŸ‘', value: 'af_sarah' },
  { name: 'πŸ‡ΊπŸ‡Έ 🚺 Nova πŸ‘', value: 'af_nova' },
  { name: 'πŸ‡ΊπŸ‡Έ 🚺 Sky πŸ‘', value: 'af_sky' },
  { name: 'πŸ‡ΊπŸ‡Έ 🚺 Alloy πŸ‘', value: 'af_alloy' },
  { name: 'πŸ‡ΊπŸ‡Έ 🚺 Jessica πŸ‘', value: 'af_jessica' },
  { name: 'πŸ‡ΊπŸ‡Έ 🚺 River πŸ‘', value: 'af_river' },
  { name: 'πŸ‡ΊπŸ‡Έ 🚹 Michael', value: 'am_michael' },
  { name: 'πŸ‡ΊπŸ‡Έ 🚹 Fenrir', value: 'am_fenrir' },
  { name: 'πŸ‡ΊπŸ‡Έ 🚹 Puck πŸ‘', value: 'am_puck' },
  { name: 'πŸ‡ΊπŸ‡Έ 🚹 Echo', value: 'am_echo' },
  { name: 'πŸ‡ΊπŸ‡Έ 🚹 Eric πŸ‘', value: 'am_eric' },
  { name: 'πŸ‡ΊπŸ‡Έ 🚹 Liam πŸ‘', value: 'am_liam' },
  { name: 'πŸ‡ΊπŸ‡Έ 🚹 Onyx', value: 'am_onyx' },
  { name: 'πŸ‡ΊπŸ‡Έ 🚹 Santa', value: 'am_santa' },
  { name: 'πŸ‡ΊπŸ‡Έ 🚹 Adam', value: 'am_adam' },
  { name: 'πŸ‡¬πŸ‡§ 🚺 Emma', value: 'bf_emma' },
  { name: 'πŸ‡¬πŸ‡§ 🚺 Isabella πŸ‘', value: 'bf_isabella' },
  { name: 'πŸ‡¬πŸ‡§ 🚺 Alice πŸ‘', value: 'bf_alice' },
  { name: 'πŸ‡¬πŸ‡§ 🚺 Lily', value: 'bf_lily' },
  { name: 'πŸ‡¬πŸ‡§ 🚹 George', value: 'bm_george' },
  { name: 'πŸ‡¬πŸ‡§ 🚹 Fable πŸ‘', value: 'bm_fable' },
  { name: 'πŸ‡¬πŸ‡§ 🚹 Lewis πŸ‘', value: 'bm_lewis' },
  { name: 'πŸ‡¬πŸ‡§ 🚹 Daniel', value: 'bm_daniel' },
];

const getRandomSpeakerPair = (): { s1: string; s2: string } => {
  const s1Gender = Math.random() > 0.5 ? '🚺' : '🚹';
  const s2Gender = s1Gender === '🚺' ? '🚹' : '🚺';
  const s1 = pickRand(
    SPEAKERS.filter((s) => s.name.includes(s1Gender) && s.name.includes('πŸ‘'))
  ).value;
  const s2 = pickRand(
    SPEAKERS.filter((s) => s.name.includes(s2Gender) && s.name.includes('πŸ‘'))
  ).value;
  return { s1, s2 };
};

const parseYAML = (yaml: string): Podcast => {
  try {
    return parse(yaml);
  } catch (e) {
    console.error(e);
    throw new Error(
      'invalid YAML, please re-generate the script: ' + (e as any).message
    );
  }
};

export const PodcastGenerator = ({
  genratedScript,
  setBusy,
  blogURL,
  busy,
}: {
  genratedScript: string;
  blogURL: string;
  setBusy: (busy: boolean) => void;
  busy: boolean;
}) => {
  const [wav, setWav] = useState<AudioBuffer | null>(null);
  const [outTitle, setOutTitle] = useState<string>('');
  const [numSteps, setNumSteps] = useState<number>(0);
  const [numStepsDone, setNumStepsDone] = useState<number>(0);

  const [script, setScript] = useState<string>('');
  const [speaker1, setSpeaker1] = useState<string>('');
  const [speaker2, setSpeaker2] = useState<string>('');
  const [speed, setSpeed] = useState<string>('1.2');
  const [isAddIntroMusic, setIsAddIntroMusic] = useState<boolean>(false);
  const [isAddNoise, setIsAddNoise] = useState<boolean>(true);

  const refInput = useRef<HTMLTextAreaElement | null>(null);

  const [blogFilePushToken, setBlogFilePushToken] = useState<string>(
    localStorage.getItem('blogFilePushToken') || ''
  );
  const [blogCmtOutput, setBlogCmtOutput] = useState<string>('');
  useEffect(() => {
    localStorage.setItem('blogFilePushToken', blogFilePushToken);
  }, [blogFilePushToken]);

  const setRandSpeaker = () => {
    const { s1, s2 } = getRandomSpeakerPair();
    setSpeaker1(s1);
    setSpeaker2(s2);
  };
  useEffect(setRandSpeaker, []);

  useEffect(() => {
    setScript(genratedScript);
    setTimeout(() => {
      // auto scroll
      if (refInput.current) {
        refInput.current.scrollTop = refInput.current.scrollHeight;
      }
    }, 10);
  }, [genratedScript]);

  const generatePodcast = async () => {
    setWav(null);
    setBusy(true);
    setBlogCmtOutput('');
    if (isBlogMode && !blogURL) {
      alert('Please enter a blog slug');
      setBusy(false);
      return;
    }
    let outputWav: AudioBuffer;
    try {
      const podcast = parseYAML(script);
      setOutTitle(podcast.title ?? 'Untitled podcast');
      outputWav = await pipelineGeneratePodcast(
        {
          podcast,
          speaker1,
          speaker2,
          speed: parseFloat(speed),
          isAddIntroMusic,
          isAddNoise,
        },
        (done: number, total: number) => {
          setNumStepsDone(done);
          setNumSteps(total);
        }
      );
      setWav(outputWav! ?? null);
    } catch (e) {
      console.error(e);
      alert(`Error: ${(e as any).message}`);
      setWav(null);
    }
    setBusy(false);
    setNumStepsDone(0);
    setNumSteps(0);

    // maybe upload
    if (isBlogMode && outputWav!) {
      const repoId = 'ngxson/hf-blog-podcast';
      const blogSlug = blogURL.split('/blog/').pop() ?? '_noname';
      const filename = `${blogSlug}.mp3`;
      setBlogCmtOutput(`Uploading '${filename}' ...`);
      await uploadFileToHub(
        audioBufferToMp3(outputWav),
        filename,
        repoId,
        blogFilePushToken
      );
      setBlogCmtOutput(getBlogComment(filename));
    }
  };

  const isGenerating = numSteps > 0;

  return (
    <>
      <div className="card bg-base-100 w-full shadow-xl">
        <div className="card-body">
          <h2 className="card-title">Step 2: Script (YAML format)</h2>

          {isBlogMode && (
            <>
              <input
                type="password"
                placeholder="Repo push HF_TOKEN"
                className="input input-bordered w-full"
                value={blogFilePushToken}
                onChange={(e) => setBlogFilePushToken(e.target.value)}
              />
            </>
          )}

          <textarea
            ref={refInput}
            className="textarea textarea-bordered w-full h-72 p-2"
            placeholder="Type your script here..."
            value={script}
            onChange={(e) => setScript(e.target.value)}
          ></textarea>

          <div className="grid grid-cols-2 gap-4">
            <label className="form-control w-full">
              <div className="label">
                <span className="label-text">Speaker 1 (πŸ‘ is better)</span>
              </div>
              <select
                className="select select-bordered"
                value={speaker1}
                onChange={(e) => setSpeaker1(e.target.value)}
              >
                {SPEAKERS.map((s) => (
                  <option key={s.value} value={s.value}>
                    {s.name}
                  </option>
                ))}
              </select>
            </label>

            <label className="form-control w-full">
              <div className="label">
                <span className="label-text">Speaker 2 (πŸ‘ is better)</span>
              </div>
              <select
                className="select select-bordered"
                value={speaker2}
                onChange={(e) => setSpeaker2(e.target.value)}
              >
                {SPEAKERS.map((s) => (
                  <option key={s.value} value={s.value}>
                    {s.name}
                  </option>
                ))}
              </select>
            </label>

            <button className="btn" onClick={setRandSpeaker}>
              Randomize speakers
            </button>

            <label className="form-control w-full">
              <select
                className="select select-bordered"
                value={speed.toString()}
                onChange={(e) => setSpeed(e.target.value)}
              >
                {SPEEDS.map((s) => (
                  <option key={s.value} value={s.value.toString()}>
                    Speed: {s.name} ({s.value})
                  </option>
                ))}
              </select>
            </label>

            <div className="flex items-center gap-2">
              <input
                type="checkbox"
                className="checkbox"
                checked={isAddIntroMusic}
                onChange={(e) => setIsAddIntroMusic(e.target.checked)}
                disabled={isGenerating || busy}
              />
              Add intro music (to make it feels like radio)
            </div>

            <div className="flex items-center gap-2">
              <input
                type="checkbox"
                className="checkbox"
                checked={isAddNoise}
                onChange={(e) => setIsAddNoise(e.target.checked)}
                disabled={isGenerating || busy}
              />
              Add small background noise (to make it more realistic)
            </div>
          </div>

          <button
            id="btn-generate-podcast"
            className="btn btn-primary mt-2"
            onClick={generatePodcast}
            disabled={busy || !script || isGenerating}
          >
            {isGenerating ? (
              <>
                <span className="loading loading-spinner loading-sm"></span>
                Generating ({numStepsDone}/{numSteps})...
              </>
            ) : (
              'Generate podcast'
            )}
          </button>

          {isGenerating && (
            <progress
              className="progress progress-primary mt-2"
              value={numStepsDone}
              max={numSteps}
            ></progress>
          )}
        </div>
      </div>

      {wav && (
        <div className="card bg-base-100 w-full shadow-xl">
          <div className="card-body">
            <h2 className="card-title">Step 3: Listen to your podcast</h2>
            <AudioPlayer audioBuffer={wav} title={outTitle} />

            {isBlogMode && (
              <div>
                -------------------
                <br />
                <h2>Comment to be posted:</h2>
                <pre className="p-2 bg-base-200 rounded-md my-2 whitespace-pre-wrap break-words">
                  {blogCmtOutput}
                </pre>
                <button
                  className="btn btn-sm btn-secondary"
                  onClick={() => copyStr(blogCmtOutput)}
                >
                  Copy comment
                </button>
              </div>
            )}
          </div>
        </div>
      )}
    </>
  );
};

// copy text to clipboard
export const copyStr = (textToCopy: string) => {
  // Navigator clipboard api needs a secure context (https)
  if (navigator.clipboard && window.isSecureContext) {
    navigator.clipboard.writeText(textToCopy);
  } else {
    // Use the 'out of viewport hidden text area' trick
    const textArea = document.createElement('textarea');
    textArea.value = textToCopy;
    // Move textarea out of the viewport so it's not visible
    textArea.style.position = 'absolute';
    textArea.style.left = '-999999px';
    document.body.prepend(textArea);
    textArea.select();
    document.execCommand('copy');
  }
};