Spaces:
Running
on
Zero
Running
on
Zero
chong.zhang
commited on
Commit
·
1343135
1
Parent(s):
e58a420
update
Browse files
app.py
CHANGED
@@ -32,7 +32,7 @@ def download_model(model_name="InspireMusic-Base"):
|
|
32 |
def get_args(
|
33 |
task, text="", audio=None, model_name="InspireMusic-Base",
|
34 |
chorus="intro", fast=False, fade_out=True, trim=False,
|
35 |
-
output_sample_rate=48000, max_generate_audio_seconds=30.0):
|
36 |
# This function constructs the arguments required for InspireMusic
|
37 |
args = {
|
38 |
"task" : task,
|
@@ -44,13 +44,25 @@ def get_args(
|
|
44 |
"fade_out" : fade_out,
|
45 |
"trim" : trim,
|
46 |
"output_sample_rate" : output_sample_rate,
|
|
|
47 |
"max_generate_audio_seconds": max_generate_audio_seconds,
|
48 |
"model_dir" : os.path.join("iic",
|
49 |
model_name),
|
50 |
"result_dir" : "exp/inspiremusic",
|
51 |
"output_fn" : generate_filename(),
|
52 |
"format" : "wav",
|
|
|
|
|
|
|
53 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
print(args)
|
55 |
return args
|
56 |
|
@@ -60,23 +72,51 @@ def music_generation(args):
|
|
60 |
model = InspireMusicUnified(
|
61 |
model_name=args["model_name"],
|
62 |
model_dir=args["model_dir"],
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
output_sample_rate=args["output_sample_rate"],
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
69 |
output_path = model.inference(
|
70 |
task=args["task"],
|
71 |
text=args["text"],
|
72 |
audio_prompt=args["audio_prompt"],
|
73 |
chorus=args["chorus"],
|
|
|
|
|
74 |
output_fn=args["output_fn"],
|
|
|
|
|
75 |
output_format=args["format"],
|
76 |
-
|
|
|
77 |
)
|
78 |
return output_path
|
79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
def run_inspiremusic(task, text, audio, model_name, chorus, fast, fade_out,
|
82 |
trim, output_sample_rate, max_generate_audio_seconds):
|
@@ -90,7 +130,7 @@ def run_inspiremusic(task, text, audio, model_name, chorus, fast, fade_out,
|
|
90 |
|
91 |
with gr.Blocks() as demo:
|
92 |
gr.Markdown("""
|
93 |
-
# InspireMusic
|
94 |
Generate music using InspireMusic with various tasks such as "Text-to-Music" or "Music Continuation".
|
95 |
""")
|
96 |
|
@@ -98,10 +138,19 @@ with gr.Blocks() as demo:
|
|
98 |
task = gr.Radio(["text-to-music", "continuation"], label="Select Task",
|
99 |
value="text-to-music")
|
100 |
model_name = gr.Dropdown(["InspireMusic-1.5B-Long", "InspireMusic-1.5B", "InspireMusic-1.5B-24kHz", "InspireMusic-Base", "InspireMusic-Base-24kHz"], label="Model Name", value="InspireMusic-Base")
|
101 |
-
|
102 |
text_input = gr.Textbox(label="Input Text (For Text-to-Music Task)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
audio_input = gr.Audio(label="Input Audio (For Music Continuation Task)",
|
104 |
type="filepath")
|
|
|
105 |
|
106 |
with gr.Column():
|
107 |
chorus = gr.Dropdown(["verse", "chorus", "intro", "outro"],
|
|
|
32 |
def get_args(
|
33 |
task, text="", audio=None, model_name="InspireMusic-Base",
|
34 |
chorus="intro", fast=False, fade_out=True, trim=False,
|
35 |
+
output_sample_rate=48000, max_generate_audio_seconds=30.0, time_start = 0.0, time_end=30.0):
|
36 |
# This function constructs the arguments required for InspireMusic
|
37 |
args = {
|
38 |
"task" : task,
|
|
|
44 |
"fade_out" : fade_out,
|
45 |
"trim" : trim,
|
46 |
"output_sample_rate" : output_sample_rate,
|
47 |
+
"min_generate_audio_seconds": 10.0,
|
48 |
"max_generate_audio_seconds": max_generate_audio_seconds,
|
49 |
"model_dir" : os.path.join("iic",
|
50 |
model_name),
|
51 |
"result_dir" : "exp/inspiremusic",
|
52 |
"output_fn" : generate_filename(),
|
53 |
"format" : "wav",
|
54 |
+
"time_start" : time_start,
|
55 |
+
"time_end": time_end,
|
56 |
+
"fade_out_duration": 1.0,
|
57 |
}
|
58 |
+
|
59 |
+
if args["time_start"] is None:
|
60 |
+
args["time_start"] = 0.0
|
61 |
+
if args["time_end"] is None:
|
62 |
+
args["time_end"] = args["time_start"] + 30.0
|
63 |
+
if args["time_start"] > args["time_end"]:
|
64 |
+
args["time_end"] = args["time_start"] + 30.0
|
65 |
+
|
66 |
print(args)
|
67 |
return args
|
68 |
|
|
|
72 |
model = InspireMusicUnified(
|
73 |
model_name=args["model_name"],
|
74 |
model_dir=args["model_dir"],
|
75 |
+
min_generate_audio_seconds=args["min_generate_audio_seconds"],
|
76 |
+
max_generate_audio_seconds=args["max_generate_audio_seconds"],
|
77 |
+
sample_rate=24000,
|
78 |
output_sample_rate=args["output_sample_rate"],
|
79 |
+
load_jit=True,
|
80 |
+
load_onnx=False,
|
81 |
+
fast=args["fast"],
|
82 |
+
result_dir=args.result_dir)
|
83 |
+
|
84 |
output_path = model.inference(
|
85 |
task=args["task"],
|
86 |
text=args["text"],
|
87 |
audio_prompt=args["audio_prompt"],
|
88 |
chorus=args["chorus"],
|
89 |
+
time_start=args["time_start"],
|
90 |
+
time_end=args["time_end"],
|
91 |
output_fn=args["output_fn"],
|
92 |
+
max_audio_prompt_length=args["max_audio_prompt_length"],
|
93 |
+
fade_out_duration=args["fade_out_duration"],
|
94 |
output_format=args["format"],
|
95 |
+
fade_out_mode=args["fade_out"],
|
96 |
+
trim=args["trim"])
|
97 |
)
|
98 |
return output_path
|
99 |
|
100 |
+
def update_text():
|
101 |
+
global text_input # Declare as global to modify the outer scope variable
|
102 |
+
text_input = "New value set by button click"
|
103 |
+
return text_input
|
104 |
+
|
105 |
+
default_prompts = [
|
106 |
+
"Energizing morning melody",
|
107 |
+
"Relaxing evening tune",
|
108 |
+
"Upbeat workout track",
|
109 |
+
"Soothing meditation soundscape",
|
110 |
+
"Joyful dance rhythm"
|
111 |
+
]
|
112 |
+
|
113 |
+
def cut_audio(audio_file, cut_seconds=5):
|
114 |
+
audio, sr = torchaudio.load(audio_file)
|
115 |
+
num_samples = cut_seconds * sr
|
116 |
+
cutted_audio = audio[:, :num_samples]
|
117 |
+
output_path = os.path.join(os.getcwd(), "audio_prompt_" + generate_filename() + ".wav")
|
118 |
+
torchaudio.save(output_path, cutted_audio, sr)
|
119 |
+
return output_path
|
120 |
|
121 |
def run_inspiremusic(task, text, audio, model_name, chorus, fast, fade_out,
|
122 |
trim, output_sample_rate, max_generate_audio_seconds):
|
|
|
130 |
|
131 |
with gr.Blocks() as demo:
|
132 |
gr.Markdown("""
|
133 |
+
# InspireMusic
|
134 |
Generate music using InspireMusic with various tasks such as "Text-to-Music" or "Music Continuation".
|
135 |
""")
|
136 |
|
|
|
138 |
task = gr.Radio(["text-to-music", "continuation"], label="Select Task",
|
139 |
value="text-to-music")
|
140 |
model_name = gr.Dropdown(["InspireMusic-1.5B-Long", "InspireMusic-1.5B", "InspireMusic-1.5B-24kHz", "InspireMusic-Base", "InspireMusic-Base-24kHz"], label="Model Name", value="InspireMusic-Base")
|
141 |
+
# Textbox for custom input
|
142 |
text_input = gr.Textbox(label="Input Text (For Text-to-Music Task)")
|
143 |
+
|
144 |
+
with gr.Row():
|
145 |
+
default_prompt_buttons = []
|
146 |
+
for prompt in default_prompts:
|
147 |
+
button = gr.Button(value=prompt)
|
148 |
+
button.click(fn=update_text, inputs=None, outputs=textbox)
|
149 |
+
default_prompt_buttons.append(button)
|
150 |
+
|
151 |
audio_input = gr.Audio(label="Input Audio (For Music Continuation Task)",
|
152 |
type="filepath")
|
153 |
+
audio_input = cut_audio(audio_input, sr, cut_seconds=5)
|
154 |
|
155 |
with gr.Column():
|
156 |
chorus = gr.Dropdown(["verse", "chorus", "intro", "outro"],
|