Heinrich Dinkel commited on
Commit
c38c9dd
·
1 Parent(s): 92d5c95

Added notebook.ipynb

Browse files
Files changed (1) hide show
  1. notebook.ipynb +203 -0
notebook.ipynb ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# Dasheng-AudioGen \u2014 Notebook Demo\n",
8
+ "\n",
9
+ "This notebook walks through the audio-generation usage shown in the [README](./README.md). 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 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\", 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."
70
+ ]
71
+ },
72
+ {
73
+ "cell_type": "code",
74
+ "execution_count": null,
75
+ "metadata": {},
76
+ "outputs": [],
77
+ "source": [
78
+ "prompt = model.compose_prompt(\n",
79
+ " caption=\"A gritty detective narrating over the sound of heavy rain and a melancholic solo jazz saxophone.\",\n",
80
+ " speech=\"gritty deep male voice\",\n",
81
+ " music=\"melancholic solo saxophone\",\n",
82
+ " env=\"distant urban ambience\",\n",
83
+ " sfx=\"heavy rain hitting pavement\",\n",
84
+ " asr=\"The city never sleeps, but it sure knows how to cry.\",\n",
85
+ ")\n",
86
+ "audio = model.generate(prompt)\n",
87
+ "torchaudio.save(\"output_detective.wav\", audio.cpu(), 16000)\n",
88
+ "Audio(\"output_detective.wav\")"
89
+ ]
90
+ },
91
+ {
92
+ "cell_type": "markdown",
93
+ "metadata": {},
94
+ "source": [
95
+ "You can also pass a pre-formatted string with tags directly."
96
+ ]
97
+ },
98
+ {
99
+ "cell_type": "code",
100
+ "execution_count": null,
101
+ "metadata": {},
102
+ "outputs": [],
103
+ "source": [
104
+ "audio = model.generate(\n",
105
+ " \"<|caption|> A helicopter passing overhead. <|sfx|> Rhythmic helicopter blade sounds. <|env|> Open sky ambience.\"\n",
106
+ ")\n",
107
+ "torchaudio.save(\"output_helicopter.wav\", audio.cpu(), 16000)\n",
108
+ "Audio(\"output_helicopter.wav\")"
109
+ ]
110
+ },
111
+ {
112
+ "cell_type": "markdown",
113
+ "metadata": {},
114
+ "source": [
115
+ "## Batch Inference\n",
116
+ "\n",
117
+ "Pass a list of prompts to generate multiple audios in a single call."
118
+ ]
119
+ },
120
+ {
121
+ "cell_type": "code",
122
+ "execution_count": null,
123
+ "metadata": {},
124
+ "outputs": [],
125
+ "source": [
126
+ "prompts = [\n",
127
+ " model.compose_prompt(caption=\"A cat meowing softly.\", sfx=\"Soft cat meow.\"),\n",
128
+ " model.compose_prompt(caption=\"Thunder rolling in the distance.\", env=\"Stormy night ambience.\"),\n",
129
+ " model.compose_prompt(caption=\"A piano playing a gentle melody.\", music=\"Soft piano ballad.\"),\n",
130
+ "]\n",
131
+ "audios = model.generate(prompts)\n",
132
+ "\n",
133
+ "for i, audio in enumerate(audios):\n",
134
+ " torchaudio.save(f\"output_{i}.wav\", audio.unsqueeze(0).cpu(), 16000)"
135
+ ]
136
+ },
137
+ {
138
+ "cell_type": "code",
139
+ "execution_count": null,
140
+ "metadata": {},
141
+ "outputs": [],
142
+ "source": [
143
+ "Audio(\"output_0.wav\")"
144
+ ]
145
+ },
146
+ {
147
+ "cell_type": "code",
148
+ "execution_count": null,
149
+ "metadata": {},
150
+ "outputs": [],
151
+ "source": [
152
+ "Audio(\"output_1.wav\")"
153
+ ]
154
+ },
155
+ {
156
+ "cell_type": "code",
157
+ "execution_count": null,
158
+ "metadata": {},
159
+ "outputs": [],
160
+ "source": [
161
+ "Audio(\"output_2.wav\")"
162
+ ]
163
+ },
164
+ {
165
+ "cell_type": "markdown",
166
+ "metadata": {},
167
+ "source": [
168
+ "## Generation Parameters\n",
169
+ "\n",
170
+ "Tune the denoising steps, classifier-free guidance scale, and sway sampling coefficient."
171
+ ]
172
+ },
173
+ {
174
+ "cell_type": "code",
175
+ "execution_count": null,
176
+ "metadata": {},
177
+ "outputs": [],
178
+ "source": [
179
+ "audio = model.generate(\n",
180
+ " prompts=\"A dog barking in a park\",\n",
181
+ " num_steps=25, # number of denoising steps (default: 25)\n",
182
+ " guidance_scale=5.0, # classifier-free guidance scale (default: 5.0)\n",
183
+ " sway_sampling_coef=-1.0, # sway sampling coefficient (default: -1.0, 0 for linear)\n",
184
+ ")\n",
185
+ "torchaudio.save(\"output_tuned.wav\", audio.cpu(), 16000)\n",
186
+ "Audio(\"output_tuned.wav\")"
187
+ ]
188
+ }
189
+ ],
190
+ "metadata": {
191
+ "kernelspec": {
192
+ "display_name": "Python 3",
193
+ "language": "python",
194
+ "name": "python3"
195
+ },
196
+ "language_info": {
197
+ "name": "python",
198
+ "version": "3.10"
199
+ }
200
+ },
201
+ "nbformat": 4,
202
+ "nbformat_minor": 5
203
+ }