linting and adding file cleaner
Browse files9 unmodified lines10111213141516171831 unmodified lines50515253545547 unmodified lines10310410510610710810911029 unmodified lines1401411421431441451461471481491501511521531541551561572 unmodified lines1601611621631641651661671681691701711721733 unmodified lines17717817918018118218337 unmodified lines22122222322422522622722822923023123223323423523623710 unmodified lines2482492502512522532542552562572582 unmodified lines2612622632642652662672682692702712722739 unmodified linesimport argparsefrom concurrent.futures import ProcessPoolExecutorimport osimport subprocess as spfrom tempfile import NamedTemporaryFileimport timeimport warningsimport torch31 unmodified linesINTERRUPTING = Truedef make_waveform(*args, **kwargs):# Further remove some warnings.be = time.time()47 unmodified linesfile.name, output, MODEL.sample_rate, strategy="loudness",loudness_headroom_db=16, loudness_compressor=True, add_suffix=False)out_files.append(pool.submit(make_waveform, file.name))res = [out_file.result() for out_file in out_files]print("batch finished", len(texts), time.time() - be)return res29 unmodified linestop_k=topk, top_p=topp, temperature=temperature, cfg_coef=cfg_coef)return outs[0]def toggle_audio_src(choice):if choice == "mic":return gr.update(source="microphone", value=None, label="Microphone")else:return gr.update(source="upload", value=None, label="File")def ui_full(launch_kwargs):with gr.Blocks() as interface:gr.Markdown("""# MusicGenThis is your private demo for [MusicGen](https://github.com/facebookresearch/audiocraft), a simple and controllable model for music generationpresented at: ["Simple and Controllable Music Generation"](https://huggingface.co/papers/2306.05284)""")2 unmodified lineswith gr.Row():text = gr.Text(label="Input Text", interactive=True)with gr.Column():radio = gr.Radio(["file", "mic"], value="file", label="Condition on a melody (optional) File or Mic")melody = gr.Audio(source="upload", type="numpy", label="File", interactive=True, elem_id="melody-input")with gr.Row():submit = gr.Button("Submit")# Adapted from https://github.com/rkfg/audiocraft/blob/long/app.py, MIT license._ = gr.Button("Interrupt").click(fn=interrupt, queue=False)with gr.Row():model = gr.Radio(["melody", "medium", "small", "large"], label="Model", value="melody", interactive=True)with gr.Row():duration = gr.Slider(minimum=1, maximum=120, value=10, label="Duration", interactive=True)with gr.Row():3 unmodified linescfg_coef = gr.Number(label="Classifier Free Guidance", value=3.0, interactive=True)with gr.Column():output = gr.Video(label="Generated Music")submit.click(predict_full, inputs=[model, text, melody, duration, topk, topp, temperature, cfg_coef], outputs=[output])radio.change(toggle_audio_src, radio, [melody], queue=False, show_progress=False)gr.Examples(fn=predict_full,37 unmodified linesThis can take a long time, and the model might lose consistency. The model might alsodecide at arbitrary positions that the song ends.**WARNING:** Choosing long durations will take a long time to generate (2min might take ~10min). An overlap of 12 secondsis kept with the previously generated chunk, and 18 "new" seconds are generated each time.We present 4 model variations:1. Melody -- a music generation model capable of generating music condition on text and melody inputs. **Note**, you can also use text only.2. Small -- a 300M transformer decoder conditioned on text only.3. Medium -- a 1.5B transformer decoder conditioned on text only.4. Large -- a 3.3B transformer decoder conditioned on text only (might OOM for the longest sequences.)When using `melody`, ou can optionaly provide a reference audio fromwhich a broad melody will be extracted. The model will then try to follow both the description and melody provided.You can also use your own GPU or a Google Colab by following the instructions on our repo.See [github.com/facebookresearch/audiocraft](https://github.com/facebookresearch/audiocraft)10 unmodified lines"""# MusicGenThis is the demo for [MusicGen](https://github.com/facebookresearch/audiocraft), a simple and controllable model for music generationpresented at: ["Simple and Controllable Music Generation"](https://huggingface.co/papers/2306.05284).<br/><a href="https://huggingface.co/spaces/facebook/MusicGen?duplicate=true" style="display: inline-block;margin-top: .5em;margin-right: .25em;" target="_blank"><img style="margin-bottom: 0em;display: inline;margin-top: -.25em;" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>for longer sequences, more control and no queue.</p>""")2 unmodified lineswith gr.Row():text = gr.Text(label="Describe your music", lines=2, interactive=True)with gr.Column():radio = gr.Radio(["file", "mic"], value="file", label="Condition on a melody (optional) File or Mic")melody = gr.Audio(source="upload", type="numpy", label="File", interactive=True, elem_id="melody-input")with gr.Row():submit = gr.Button("Generate")with gr.Column():output = gr.Video(label="Generated Music")submit.click(predict_batched, inputs=[text, melody], outputs=[output], batch=True, max_batch_size=MAX_BATCH_SIZE)radio.change(toggle_audio_src, radio, [melody], queue=False, show_progress=False)gr.Examples(fn=predict_batched,9 unmodified lines101112131415161718192031 unmodified lines525354555657585960616263646566676869707172737475767778798047 unmodified lines12812913013113213313413513613713813929 unmodified lines1691701711721731741751761771781791801811821831841851861871881892 unmodified lines1921931941951961971981992002012022032042052062072083 unmodified lines21221321421521621721821922037 unmodified lines25825926026126226326426526626726826927027127227327427527627710 unmodified lines2882892902912922932942952962972982993003012 unmodified lines3043053063073083093103113123133143153163173183199 unmodified linesimport argparsefrom concurrent.futures import ProcessPoolExecutorimport osfrom pathlib import Pathimport subprocess as spfrom tempfile import NamedTemporaryFileimport timeimport typing as tpimport warningsimport torch31 unmodified linesINTERRUPTING = Trueclass FileCleaner:def __init__(self, file_lifetime: float = 3600):self.file_lifetime = file_lifetimeself.files = []def add(self, path: tp.Union[str, Path]):self._cleanup()self.files.append((time.time(), Path(path)))def _cleanup(self):now = time.time()for time_added, path in list(self.files):if now - time_added > self.file_lifetime:if path.exists():path.unlink()self.files.pop(0)else:breakfile_cleaner = FileCleaner()def make_waveform(*args, **kwargs):# Further remove some warnings.be = time.time()47 unmodified linesfile.name, output, MODEL.sample_rate, strategy="loudness",loudness_headroom_db=16, loudness_compressor=True, add_suffix=False)out_files.append(pool.submit(make_waveform, file.name))file_cleaner.add(file.name)res = [out_file.result() for out_file in out_files]for file in res:file_cleaner.add(file)print("batch finished", len(texts), time.time() - be)print("Tempfiles currently stored: ", len(file_cleaner.files))return res29 unmodified linestop_k=topk, top_p=topp, temperature=temperature, cfg_coef=cfg_coef)return outs[0]def toggle_audio_src(choice):if choice == "mic":return gr.update(source="microphone", value=None, label="Microphone")else:return gr.update(source="upload", value=None, label="File")def ui_full(launch_kwargs):with gr.Blocks() as interface:gr.Markdown("""# MusicGenThis is your private demo for [MusicGen](https://github.com/facebookresearch/audiocraft),a simple and controllable model for music generationpresented at: ["Simple and Controllable Music Generation"](https://huggingface.co/papers/2306.05284)""")2 unmodified lineswith gr.Row():text = gr.Text(label="Input Text", interactive=True)with gr.Column():radio = gr.Radio(["file", "mic"], value="file",label="Condition on a melody (optional) File or Mic")melody = gr.Audio(source="upload", type="numpy", label="File",interactive=True, elem_id="melody-input")with gr.Row():submit = gr.Button("Submit")# Adapted from https://github.com/rkfg/audiocraft/blob/long/app.py, MIT license._ = gr.Button("Interrupt").click(fn=interrupt, queue=False)with gr.Row():model = gr.Radio(["melody", "medium", "small", "large"],label="Model", value="melody", interactive=True)with gr.Row():duration = gr.Slider(minimum=1, maximum=120, value=10, label="Duration", interactive=True)with gr.Row():3 unmodified linescfg_coef = gr.Number(label="Classifier Free Guidance", value=3.0, interactive=True)with gr.Column():output = gr.Video(label="Generated Music")submit.click(predict_full,inputs=[model, text, melody, duration, topk, topp, temperature, cfg_coef],outputs=[output])radio.change(toggle_audio_src, radio, [melody], queue=False, show_progress=False)gr.Examples(fn=predict_full,37 unmodified linesThis can take a long time, and the model might lose consistency. The model might alsodecide at arbitrary positions that the song ends.**WARNING:** Choosing long durations will take a long time to generate (2min might take ~10min).An overlap of 12 seconds is kept with the previously generated chunk, and 18 "new" secondsare generated each time.We present 4 model variations:1. Melody -- a music generation model capable of generating music conditionon text and melody inputs. **Note**, you can also use text only.2. Small -- a 300M transformer decoder conditioned on text only.3. Medium -- a 1.5B transformer decoder conditioned on text only.4. Large -- a 3.3B transformer decoder conditioned on text only (might OOM for the longest sequences.)When using `melody`, ou can optionaly provide a reference audio fromwhich a broad melody will be extracted. The model will then try to follow boththe description and melody provided.You can also use your own GPU or a Google Colab by following the instructions on our repo.See [github.com/facebookresearch/audiocraft](https://github.com/facebookresearch/audiocraft)10 unmodified lines"""# MusicGenThis is the demo for [MusicGen](https://github.com/facebookresearch/audiocraft),a simple and controllable model for music generationpresented at: ["Simple and Controllable Music Generation"](https://huggingface.co/papers/2306.05284).<br/><a href="https://huggingface.co/spaces/facebook/MusicGen?duplicate=true"style="display: inline-block;margin-top: .5em;margin-right: .25em;" target="_blank"><img style="margin-bottom: 0em;display: inline;margin-top: -.25em;"src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>for longer sequences, more control and no queue.</p>""")2 unmodified lineswith gr.Row():text = gr.Text(label="Describe your music", lines=2, interactive=True)with gr.Column():radio = gr.Radio(["file", "mic"], value="file",label="Condition on a melody (optional) File or Mic")melody = gr.Audio(source="upload", type="numpy", label="File",interactive=True, elem_id="melody-input")with gr.Row():submit = gr.Button("Generate")with gr.Column():output = gr.Video(label="Generated Music")submit.click(predict_batched, inputs=[text, melody],outputs=[output], batch=True, max_batch_size=MAX_BATCH_SIZE)radio.change(toggle_audio_src, radio, [melody], queue=False, show_progress=False)gr.Examples(fn=predict_batched,
app.py+62-16