Heinrich Dinkel commited on
Commit
76bb476
·
1 Parent(s): ab1359a

Added notebook

Browse files
Files changed (3) hide show
  1. README.md +1 -0
  2. README_zh.md +1 -0
  3. notebook.ipynb +234 -0
README.md CHANGED
@@ -28,6 +28,7 @@ pipeline_tag: text-to-audio
28
  [![Hugging Face Model](https://img.shields.io/badge/HuggingFace-Model-orange?logo=huggingface)](https://huggingface.co/mispeech/Dasheng-AudioGen-Multilingual)
29
  [![Hugging Face Demo](https://img.shields.io/badge/HuggingFace-Demo-orange?logo=huggingface)](https://huggingface.co/spaces/mispeech/Dasheng-AudioGen)
30
  [![Web Demo](https://img.shields.io/badge/Website-Demo-181717?logo=google-chrome)](https://nieeim.github.io/Dasheng-AudioGen-Web/)
 
31
 
32
  [**English**](./README.md) | [**中文**](./README_zh.md)
33
 
 
28
  [![Hugging Face Model](https://img.shields.io/badge/HuggingFace-Model-orange?logo=huggingface)](https://huggingface.co/mispeech/Dasheng-AudioGen-Multilingual)
29
  [![Hugging Face Demo](https://img.shields.io/badge/HuggingFace-Demo-orange?logo=huggingface)](https://huggingface.co/spaces/mispeech/Dasheng-AudioGen)
30
  [![Web Demo](https://img.shields.io/badge/Website-Demo-181717?logo=google-chrome)](https://nieeim.github.io/Dasheng-AudioGen-Web/)
31
+ [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/#fileId=https://huggingface.co/mispeech/Dasheng-AudioGen-Multilingual/resolve/main/notebook.ipynb)
32
 
33
  [**English**](./README.md) | [**中文**](./README_zh.md)
34
 
README_zh.md CHANGED
@@ -4,6 +4,7 @@
4
  [![Hugging Face Model](https://img.shields.io/badge/HuggingFace-Model-orange?logo=huggingface)](https://huggingface.co/mispeech/Dasheng-AudioGen-Multilingual)
5
  [![Hugging Face Demo](https://img.shields.io/badge/HuggingFace-Demo-orange?logo=huggingface)](https://huggingface.co/spaces/mispeech/Dasheng-AudioGen)
6
  [![Web Demo](https://img.shields.io/badge/Website-Demo-181717?logo=google-chrome)](https://nieeim.github.io/Dasheng-AudioGen-Web/)
 
7
 
8
  [**English**](./README.md) | [**中文**](./README_zh.md)
9
 
 
4
  [![Hugging Face Model](https://img.shields.io/badge/HuggingFace-Model-orange?logo=huggingface)](https://huggingface.co/mispeech/Dasheng-AudioGen-Multilingual)
5
  [![Hugging Face Demo](https://img.shields.io/badge/HuggingFace-Demo-orange?logo=huggingface)](https://huggingface.co/spaces/mispeech/Dasheng-AudioGen)
6
  [![Web Demo](https://img.shields.io/badge/Website-Demo-181717?logo=google-chrome)](https://nieeim.github.io/Dasheng-AudioGen-Web/)
7
+ [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/#fileId=https://huggingface.co/mispeech/Dasheng-AudioGen-Multilingual/resolve/main/notebook.ipynb)
8
 
9
  [**English**](./README.md) | [**中文**](./README_zh.md)
10
 
notebook.ipynb ADDED
@@ -0,0 +1,234 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# Dasheng-AudioGen-Multilingual \u2014 Notebook Demo\n",
8
+ "\n",
9
+ "This notebook walks through the audio-generation usage shown in the [README](./README.md) for the **multilingual** variant of Dasheng-AudioGen. A CUDA-capable GPU is required.\n",
10
+ "\n",
11
+ "Each example takes a text description and produces an audio waveform that is saved to disk and played back inline."
12
+ ]
13
+ },
14
+ {
15
+ "cell_type": "markdown",
16
+ "metadata": {},
17
+ "source": [
18
+ "## Installation"
19
+ ]
20
+ },
21
+ {
22
+ "cell_type": "code",
23
+ "execution_count": null,
24
+ "metadata": {},
25
+ "outputs": [],
26
+ "source": [
27
+ "%pip install torch torchaudio \"transformers<5\" einops"
28
+ ]
29
+ },
30
+ {
31
+ "cell_type": "markdown",
32
+ "metadata": {},
33
+ "source": [
34
+ "## Basic Usage\n",
35
+ "\n",
36
+ "Load the multilingual model and generate audio from a single text prompt."
37
+ ]
38
+ },
39
+ {
40
+ "cell_type": "code",
41
+ "execution_count": null,
42
+ "metadata": {},
43
+ "outputs": [],
44
+ "source": [
45
+ "import torchaudio\n",
46
+ "from transformers import AutoModel\n",
47
+ "from IPython.display import Audio\n",
48
+ "\n",
49
+ "model = AutoModel.from_pretrained(\"mispeech/Dasheng-AudioGen-Multilingual\", trust_remote_code=True).cuda()"
50
+ ]
51
+ },
52
+ {
53
+ "cell_type": "code",
54
+ "execution_count": null,
55
+ "metadata": {},
56
+ "outputs": [],
57
+ "source": [
58
+ "audio = model.generate(\"A dog barking in a park\")\n",
59
+ "torchaudio.save(\"output.wav\", audio.cpu(), 16000)\n",
60
+ "Audio(\"output.wav\")"
61
+ ]
62
+ },
63
+ {
64
+ "cell_type": "markdown",
65
+ "metadata": {},
66
+ "source": [
67
+ "## Aspect-wise Prompt\n",
68
+ "\n",
69
+ "Use `compose_prompt` to describe different audio aspects separately.\n",
70
+ "\n",
71
+ "> **Multilingual prompt convention:** All descriptive tags (`caption`, `speech`, `sfx`, `music`, `env`) should be written in **English**. Only the `<|asr|>` field (the actual spoken content to be synthesized) should use the target language."
72
+ ]
73
+ },
74
+ {
75
+ "cell_type": "markdown",
76
+ "metadata": {},
77
+ "source": [
78
+ "### Spanish example"
79
+ ]
80
+ },
81
+ {
82
+ "cell_type": "code",
83
+ "execution_count": null,
84
+ "metadata": {},
85
+ "outputs": [],
86
+ "source": [
87
+ "prompt = model.compose_prompt(\n",
88
+ " caption=\"A conversation scene on a busy city street.\",\n",
89
+ " speech=\"A young woman speaking softly in Spanish.\",\n",
90
+ " env=\"Rain and distant traffic noise.\",\n",
91
+ " asr=\"Creo que deber\u00edamos irnos ya.\",\n",
92
+ ")\n",
93
+ "audio = model.generate(prompt)\n",
94
+ "torchaudio.save(\"output_spanish.wav\", audio.cpu(), 16000)\n",
95
+ "Audio(\"output_spanish.wav\")"
96
+ ]
97
+ },
98
+ {
99
+ "cell_type": "markdown",
100
+ "metadata": {},
101
+ "source": [
102
+ "### German example"
103
+ ]
104
+ },
105
+ {
106
+ "cell_type": "code",
107
+ "execution_count": null,
108
+ "metadata": {},
109
+ "outputs": [],
110
+ "source": [
111
+ "prompt = model.compose_prompt(\n",
112
+ " caption=\"A calm voice giving directions in a quiet office.\",\n",
113
+ " speech=\"A middle-aged man speaking calmly in German.\",\n",
114
+ " env=\"Quiet office ambience with faint keyboard typing.\",\n",
115
+ " asr=\"Bitte biegen Sie an der n\u00e4chsten Kreuzung links ab.\",\n",
116
+ ")\n",
117
+ "audio = model.generate(prompt)\n",
118
+ "torchaudio.save(\"output_german.wav\", audio.cpu(), 16000)\n",
119
+ "Audio(\"output_german.wav\")"
120
+ ]
121
+ },
122
+ {
123
+ "cell_type": "markdown",
124
+ "metadata": {},
125
+ "source": [
126
+ "You can also pass a pre-formatted string with tags directly."
127
+ ]
128
+ },
129
+ {
130
+ "cell_type": "code",
131
+ "execution_count": null,
132
+ "metadata": {},
133
+ "outputs": [],
134
+ "source": [
135
+ "audio = model.generate(\n",
136
+ " \"<|caption|> A helicopter passing overhead. <|sfx|> Rhythmic helicopter blade sounds. <|env|> Open sky ambience.\"\n",
137
+ ")\n",
138
+ "torchaudio.save(\"output_helicopter.wav\", audio.cpu(), 16000)\n",
139
+ "Audio(\"output_helicopter.wav\")"
140
+ ]
141
+ },
142
+ {
143
+ "cell_type": "markdown",
144
+ "metadata": {},
145
+ "source": [
146
+ "## Batch Inference\n",
147
+ "\n",
148
+ "Pass a list of prompts to generate multiple audios in a single call."
149
+ ]
150
+ },
151
+ {
152
+ "cell_type": "code",
153
+ "execution_count": null,
154
+ "metadata": {},
155
+ "outputs": [],
156
+ "source": [
157
+ "prompts = [\n",
158
+ " model.compose_prompt(caption=\"A cat meowing softly.\", sfx=\"Soft cat meow.\"),\n",
159
+ " model.compose_prompt(caption=\"Thunder rolling in the distance.\", env=\"Stormy night ambience.\"),\n",
160
+ " model.compose_prompt(caption=\"A piano playing a gentle melody.\", music=\"Soft piano ballad.\"),\n",
161
+ "]\n",
162
+ "audios = model.generate(prompts)\n",
163
+ "\n",
164
+ "for i, audio in enumerate(audios):\n",
165
+ " torchaudio.save(f\"output_{i}.wav\", audio.unsqueeze(0).cpu(), 16000)"
166
+ ]
167
+ },
168
+ {
169
+ "cell_type": "code",
170
+ "execution_count": null,
171
+ "metadata": {},
172
+ "outputs": [],
173
+ "source": [
174
+ "Audio(\"output_0.wav\")"
175
+ ]
176
+ },
177
+ {
178
+ "cell_type": "code",
179
+ "execution_count": null,
180
+ "metadata": {},
181
+ "outputs": [],
182
+ "source": [
183
+ "Audio(\"output_1.wav\")"
184
+ ]
185
+ },
186
+ {
187
+ "cell_type": "code",
188
+ "execution_count": null,
189
+ "metadata": {},
190
+ "outputs": [],
191
+ "source": [
192
+ "Audio(\"output_2.wav\")"
193
+ ]
194
+ },
195
+ {
196
+ "cell_type": "markdown",
197
+ "metadata": {},
198
+ "source": [
199
+ "## Generation Parameters\n",
200
+ "\n",
201
+ "Tune the denoising steps, classifier-free guidance scale, and sway sampling coefficient."
202
+ ]
203
+ },
204
+ {
205
+ "cell_type": "code",
206
+ "execution_count": null,
207
+ "metadata": {},
208
+ "outputs": [],
209
+ "source": [
210
+ "audio = model.generate(\n",
211
+ " prompts=\"A dog barking in a park\",\n",
212
+ " num_steps=25, # number of denoising steps (default: 25)\n",
213
+ " guidance_scale=5.0, # classifier-free guidance scale (default: 5.0)\n",
214
+ " sway_sampling_coef=-1.0, # sway sampling coefficient (default: -1.0, 0 for linear)\n",
215
+ ")\n",
216
+ "torchaudio.save(\"output_tuned.wav\", audio.cpu(), 16000)\n",
217
+ "Audio(\"output_tuned.wav\")"
218
+ ]
219
+ }
220
+ ],
221
+ "metadata": {
222
+ "kernelspec": {
223
+ "display_name": "Python 3",
224
+ "language": "python",
225
+ "name": "python3"
226
+ },
227
+ "language_info": {
228
+ "name": "python",
229
+ "version": "3.10"
230
+ }
231
+ },
232
+ "nbformat": 4,
233
+ "nbformat_minor": 5
234
+ }