File size: 2,942 Bytes
d248698
 
 
 
 
 
2319c67
 
 
 
 
 
d248698
 
 
 
2319c67
d248698
 
 
 
 
2319c67
d248698
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2319c67
d248698
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import numpy as np
import pandas as pd
import os
import torch

# Change the directory
os.chdir('./audiocraft')
from audiocraft.data.audio import audio_write
import audiocraft.models


# download models
huggingface_hub.hf_hub_download(
    repo_id='Cyan0731/MusiConGen',
    filename='compression_state_dict.bin',
    local_dir='./ckpt/musicongen'
)

huggingface_hub.hf_hub_download(
    repo_id='Cyan0731/MusiConGen',
    filename='state_dict.bin',
    local_dir='./ckpt/musicongen'
)

def print_directory_contents(path):
    for root, dirs, files in os.walk(path):
        level = root.replace(path, '').count(os.sep)
        indent = ' ' * 4 * (level)
        print(f"{indent}{os.path.basename(root)}/")
        subindent = ' ' * 4 * (level + 1)
        for f in files:
            print(f"{subindent}{f}")


def infer(text):
    
    # set hparams
    output_dir = 'example_1' ### change this output directory
    
    
    duration = 30
    num_samples = 5
    bs = 1
    
    
    # load your model
    musicgen = audiocraft.models.MusicGen.get_pretrained('./ckpt/musicongen') ### change this path
    musicgen.set_generation_params(duration=duration, extend_stride=duration//2, top_k = 250)
    
    
    chords = ['C G A:min F',
              'A:min F C G',
              'C F G F',
              'C A:min F G',
              'D:min G C A:min',
              ]
    
    descriptions = ["A laid-back blues shuffle with a relaxed tempo, warm guitar tones, and a comfortable groove, perfect for a slow dance or a night in. Instruments: electric guitar, bass, drums."] * num_samples
    
    bpms = [120] * num_samples
    
    meters = [4] * num_samples
    
    wav = []
    for i in range(num_samples//bs):
      print(f"starting {i} batch...")
      temp = musicgen.generate_with_chords_and_beats(descriptions[i*bs:(i+1)*bs], 
                                                      chords[i*bs:(i+1)*bs],
                                                      bpms[i*bs:(i+1)*bs], 
                                                      meters[i*bs:(i+1)*bs]
                                                      )
      wav.extend(temp.cpu())
    
    # save and display generated audio
    for idx, one_wav in enumerate(wav):
      
      sav_path = os.path.join('./output_samples', output_dir, chords[idx] + "|" + descriptions[idx]).replace(" ", "_")
      audio_write(sav_path, one_wav.cpu(), musicgen.sample_rate, strategy='loudness', loudness_compressor=True)

    # Print the outputs directory contents
    print_directory_contents('./output_samples')
    return "done"

with gr.Blocks() as demo:
    with gr.Column():
        gr.Markdown("#MusiConGen")
        with gr.Row():
            text_in = gr.Textbox()
            submit_btn = gr.Button("Submit")
            text_out = gr.Textbox()
    submit_btn.click(
        fn = infer,
        inputs = [text_in],
        outputs = [text_out]
    )
demo.launch()