adefossez commited on
Commit
06dc009
1 Parent(s): 56d7528

rm old app

Browse files
Files changed (1) hide show
  1. app_batched.py +0 -222
app_batched.py DELETED
@@ -1,222 +0,0 @@
1
- """
2
- Copyright (c) Meta Platforms, Inc. and affiliates.
3
- All rights reserved.
4
-
5
- This source code is licensed under the license found in the
6
- LICENSE file in the root directory of this source tree.
7
- """
8
-
9
- import argparse
10
- from concurrent.futures import ProcessPoolExecutor
11
- import subprocess as sp
12
- from tempfile import NamedTemporaryFile
13
- import time
14
- import warnings
15
- import torch
16
- import gradio as gr
17
- from audiocraft.data.audio_utils import convert_audio
18
- from audiocraft.data.audio import audio_write
19
- from audiocraft.models import MusicGen
20
-
21
-
22
- MODEL = None
23
-
24
- _old_call = sp.call
25
-
26
-
27
- def _call_nostderr(*args, **kwargs):
28
- # Avoid ffmpeg vomitting on the logs.
29
- kwargs['stderr'] = sp.DEVNULL
30
- kwargs['stdout'] = sp.DEVNULL
31
- _old_call(*args, **kwargs)
32
-
33
-
34
- sp.call = _call_nostderr
35
- pool = ProcessPoolExecutor(3)
36
- pool.__enter__()
37
-
38
-
39
- def make_waveform(*args, **kwargs):
40
- be = time.time()
41
- with warnings.catch_warnings():
42
- warnings.simplefilter('ignore')
43
- out = gr.make_waveform(*args, **kwargs)
44
- print("Make a video took", time.time() - be)
45
- return out
46
-
47
-
48
- def load_model():
49
- print("Loading model")
50
- return MusicGen.get_pretrained("melody")
51
-
52
-
53
- def predict(texts, melodies):
54
- global MODEL
55
- if MODEL is None:
56
- MODEL = load_model()
57
-
58
- duration = 12
59
- max_text_length = 512
60
- texts = [text[:max_text_length] for text in texts]
61
- MODEL.set_generation_params(duration=duration)
62
-
63
- print("new batch", len(texts), texts, [None if m is None else (m[0], m[1].shape) for m in melodies])
64
- be = time.time()
65
- processed_melodies = []
66
- target_sr = 32000
67
- target_ac = 1
68
- for melody in melodies:
69
- if melody is None:
70
- processed_melodies.append(None)
71
- else:
72
- sr, melody = melody[0], torch.from_numpy(melody[1]).to(MODEL.device).float().t()
73
- if melody.dim() == 1:
74
- melody = melody[None]
75
- melody = melody[..., :int(sr * duration)]
76
- melody = convert_audio(melody, sr, target_sr, target_ac)
77
- processed_melodies.append(melody)
78
-
79
- outputs = MODEL.generate_with_chroma(
80
- descriptions=texts,
81
- melody_wavs=processed_melodies,
82
- melody_sample_rate=target_sr,
83
- progress=False
84
- )
85
-
86
- outputs = outputs.detach().cpu().float()
87
- out_files = []
88
- for output in outputs:
89
- with NamedTemporaryFile("wb", suffix=".wav", delete=False) as file:
90
- audio_write(
91
- file.name, output, MODEL.sample_rate, strategy="loudness",
92
- loudness_headroom_db=16, loudness_compressor=True, add_suffix=False)
93
- out_files.append(pool.submit(make_waveform, file.name))
94
- res = [[out_file.result() for out_file in out_files]]
95
- print("batch finished", len(texts), time.time() - be)
96
- return res
97
-
98
-
99
- def ui(**kwargs):
100
- with gr.Blocks() as demo:
101
- gr.Markdown(
102
- """
103
- # MusicGen
104
-
105
- This is the demo for [MusicGen](https://github.com/facebookresearch/audiocraft), a simple and controllable model for music generation
106
- presented at: ["Simple and Controllable Music Generation"](https://huggingface.co/papers/2306.05284).
107
- <br/>
108
- <a href="https://huggingface.co/spaces/musicgen/MusicGen?duplicate=true" style="display: inline-block;margin-top: .5em;margin-right: .25em;" target="_blank">
109
- <img style="margin-bottom: 0em;display: inline;margin-top: -.25em;" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
110
- for longer sequences, more control and no queue.</p>
111
- """
112
- )
113
- with gr.Row():
114
- with gr.Column():
115
- with gr.Row():
116
- text = gr.Text(label="Describe your music", lines=2, interactive=True)
117
- melody = gr.Audio(source="upload", type="numpy", label="Condition on a melody (optional)", interactive=True)
118
- with gr.Row():
119
- submit = gr.Button("Generate")
120
- with gr.Column():
121
- output = gr.Video(label="Generated Music")
122
- submit.click(predict, inputs=[text, melody], outputs=[output], batch=True, max_batch_size=8)
123
- gr.Examples(
124
- fn=predict,
125
- examples=[
126
- [
127
- "An 80s driving pop song with heavy drums and synth pads in the background",
128
- "./assets/bach.mp3",
129
- ],
130
- [
131
- "A cheerful country song with acoustic guitars",
132
- "./assets/bolero_ravel.mp3",
133
- ],
134
- [
135
- "90s rock song with electric guitar and heavy drums",
136
- None,
137
- ],
138
- [
139
- "a light and cheerly EDM track, with syncopated drums, aery pads, and strong emotions bpm: 130",
140
- "./assets/bach.mp3",
141
- ],
142
- [
143
- "lofi slow bpm electro chill with organic samples",
144
- None,
145
- ],
146
- ],
147
- inputs=[text, melody],
148
- outputs=[output]
149
- )
150
- gr.Markdown("""
151
- ### More details
152
-
153
- The model will generate 12 seconds of audio based on the description you provided.
154
- You can optionaly provide a reference audio from which a broad melody will be extracted.
155
- The model will then try to follow both the description and melody provided.
156
- All samples are generated with the `melody` model.
157
-
158
- You can also use your own GPU or a Google Colab by following the instructions on our repo.
159
-
160
- See [github.com/facebookresearch/audiocraft](https://github.com/facebookresearch/audiocraft)
161
- for more details.
162
- """)
163
-
164
- # Show the interface
165
- launch_kwargs = {}
166
- username = kwargs.get('username')
167
- password = kwargs.get('password')
168
- server_port = kwargs.get('server_port', 0)
169
- inbrowser = kwargs.get('inbrowser', False)
170
- share = kwargs.get('share', False)
171
- server_name = kwargs.get('listen')
172
-
173
- launch_kwargs['server_name'] = server_name
174
-
175
- if username and password:
176
- launch_kwargs['auth'] = (username, password)
177
- if server_port > 0:
178
- launch_kwargs['server_port'] = server_port
179
- if inbrowser:
180
- launch_kwargs['inbrowser'] = inbrowser
181
- if share:
182
- launch_kwargs['share'] = share
183
- demo.queue(max_size=8 * 4).launch(**launch_kwargs)
184
-
185
-
186
- if __name__ == "__main__":
187
- parser = argparse.ArgumentParser()
188
- parser.add_argument(
189
- '--listen',
190
- type=str,
191
- default='0.0.0.0',
192
- help='IP to listen on for connections to Gradio',
193
- )
194
- parser.add_argument(
195
- '--username', type=str, default='', help='Username for authentication'
196
- )
197
- parser.add_argument(
198
- '--password', type=str, default='', help='Password for authentication'
199
- )
200
- parser.add_argument(
201
- '--server_port',
202
- type=int,
203
- default=0,
204
- help='Port to run the server listener on',
205
- )
206
- parser.add_argument(
207
- '--inbrowser', action='store_true', help='Open in browser'
208
- )
209
- parser.add_argument(
210
- '--share', action='store_true', help='Share the gradio UI'
211
- )
212
-
213
- args = parser.parse_args()
214
-
215
- ui(
216
- username=args.username,
217
- password=args.password,
218
- inbrowser=args.inbrowser,
219
- server_port=args.server_port,
220
- share=args.share,
221
- listen=args.listen
222
- )