Spaces:
Sleeping
Sleeping
import os # ํ์ผ ๋ฐ ๋๋ ํ ๋ฆฌ ์์ ์ ์ํ ๋ชจ๋ | |
import torch # ๋ฅ๋ฌ๋ ํ๋ ์์ํฌ PyTorch | |
import librosa # ์ค๋์ค ์ฒ๋ฆฌ๋ฅผ ์ํ ๋ชจ๋ | |
import binascii # ์ด์ง ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃจ๋ ๋ชจ๋ | |
import warnings # ๊ฒฝ๊ณ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋ ๋ชจ๋ | |
import midi2audio # MIDI ํ์ผ์ WAV ํ์ผ๋ก ๋ณํํ๋ ๋ชจ๋ | |
import numpy as np # ๋ค์ฐจ์ ๋ฐฐ์ด์ ๋ค๋ฃจ๋ ๋ชจ๋ | |
import pytube as pt # YouTube ๋์์์ ๋ค์ด๋ก๋ํ๋ ๋ชจ๋ | |
import gradio as gr # ์ธํฐ๋ํฐ๋ธํ UI๋ฅผ ๋ง๋ค๊ธฐ ์ํ ๋ชจ๋ | |
import soundfile as sf # ์ฌ์ด๋ ํ์ผ์ ๋ค๋ฃจ๋ ๋ชจ๋ | |
from transformers import Pop2PianoForConditionalGeneration, Pop2PianoProcessor # Pop2Piano ๋ชจ๋ธ๊ณผ ์ ์ฒ๋ฆฌ๊ธฐ | |
yt_video_dir = "./yt_dir" # YouTube ๋์์ ๋ค์ด๋ก๋ ๋๋ ํ ๋ฆฌ ๊ฒฝ๋ก | |
outputs_dir = "./midi_wav_outputs" # MIDI ๋ฐ WAV ํ์ผ ์ถ๋ ฅ ๋๋ ํ ๋ฆฌ ๊ฒฝ๋ก | |
os.makedirs(outputs_dir, exist_ok=True) # ์ถ๋ ฅ ๋๋ ํ ๋ฆฌ ์์ฑ (์ด๋ฏธ ์กด์ฌํ๋ ๊ฒฝ์ฐ ๋ฌด์) | |
os.makedirs(yt_video_dir, exist_ok=True) # YouTube ๋์์ ๋ค์ด๋ก๋ ๋๋ ํ ๋ฆฌ ์์ฑ (์ด๋ฏธ ์กด์ฌํ๋ ๊ฒฝ์ฐ ๋ฌด์) | |
device = "cuda" if torch.cuda.is_available() else "cpu" # CUDA๊ฐ ์ฌ์ฉ ๊ฐ๋ฅํ ๊ฒฝ์ฐ GPU๋ฅผ ์ฌ์ฉํ๊ณ , ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ CPU๋ฅผ ์ฌ์ฉ | |
model = Pop2PianoForConditionalGeneration.from_pretrained("sweetcocoa/pop2piano").to(device) # ์ฌ์ ํ์ต๋ Pop2Piano ๋ชจ๋ธ ๋ก๋ | |
processor = Pop2PianoProcessor.from_pretrained("sweetcocoa/pop2piano") # ์ฌ์ ํ์ต๋ Pop2Piano ์ ์ฒ๋ฆฌ๊ธฐ ๋ก๋ | |
composers = model.generation_config.composer_to_feature_token.keys() # ์๊ณก๊ฐ ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ | |
def get_audio_from_yt_video(yt_link): | |
try: | |
yt = pt.YouTube(yt_link) # YouTube ๋์์ ๊ฐ์ฒด ์์ฑ | |
t = yt.streams.filter(only_audio=True) # ์ค๋์ค ์คํธ๋ฆผ ํํฐ๋ง | |
filename = os.path.join(yt_video_dir, binascii.hexlify(os.urandom(8)).decode() + ".mp4") # ๋๋ค ํ์ผ ์ด๋ฆ ์์ฑ | |
t[0].download(filename=filename) # ๋์์ ๋ค์ด๋ก๋ | |
except: | |
warnings.warn(f"Video Not Found at {yt_link}") # ๊ฒฝ๊ณ ๋ฉ์์ง ์ถ๋ ฅ | |
filename = None | |
return filename, filename | |
def inference(file_uploaded, composer): | |
waveform, sr = librosa.load(file_uploaded, sr=None) # ํ์ผ์์ ์ค๋์ค ๋ฐ์ดํฐ์ ์ํ๋ง ์ฃผํ์ ๋ก๋ | |
inputs = processor(audio=waveform, sampling_rate=sr, return_tensors="pt").to(device) # ์ ๋ ฅ ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ | |
model_output = model.generate(input_features=inputs["input_features"], composer=composer) # ๋ชจ๋ธ์ ์ ๋ ฅํ์ฌ ์ถ๋ ฅ ์์ฑ | |
tokenizer_output = processor.batch_decode(token_ids=model_output.to("cpu"), feature_extractor_output=inputs.to("cpu"))["pretty_midi_objects"] # ํ ํฐ ๋์ฝ๋ฉ | |
return prepare_output_file(tokenizer_output, sr) # ์ถ๋ ฅ ํ์ผ ์ค๋น ํจ์ ํธ์ถ | |
def prepare_output_file(tokenizer_output, sr): | |
output_file_name = "output_" + binascii.hexlify(os.urandom(8)).decode() # ๋๋ค ์ถ๋ ฅ ํ์ผ ์ด๋ฆ ์์ฑ | |
midi_output = os.path.join(outputs_dir, output_file_name + ".mid") # MIDI ์ถ๋ ฅ ํ์ผ ๊ฒฝ๋ก | |
tokenizer_output[0].write(midi_output) # MIDI ํ์ผ ์์ฑ | |
wav_output = midi_output.replace(".mid", ".wav") # WAV ์ถ๋ ฅ ํ์ผ ๊ฒฝ๋ก | |
midi2audio.FluidSynth().midi_to_audio(midi_output, wav_output) # MIDI๋ฅผ WAV๋ก ๋ณํ | |
return wav_output, wav_output, midi_output # WAV ๋ฐ MIDI ํ์ผ ๊ฒฝ๋ก ๋ฐํ | |
def get_stereo(pop_path, midi, pop_scale=0.5): | |
pop_y, sr = librosa.load(pop_path, sr=None) # ํ ์์ ํ์ผ ๋ก๋ | |
midi_y, _ = librosa.load(midi.name, sr=None) # MIDI ํ์ผ ๋ก๋ | |
if len(pop_y) > len(midi_y): | |
midi_y = np.pad(midi_y, (0, len(pop_y) - len(midi_y))) # MIDI ๊ธธ์ด๋ฅผ ํ ์์ ๊ธธ์ด์ ๋ง์ถค | |
elif len(pop_y) < len(midi_y): | |
pop_y = np.pad(pop_y, (0, -len(pop_y) + len(midi_y))) # ํ ์์ ๊ธธ์ด๋ฅผ MIDI ๊ธธ์ด์ ๋ง์ถค | |
stereo = np.stack((midi_y, pop_y * pop_scale)) # ์คํ ๋ ์ค ๋ฏน์ค ์์ฑ | |
stereo_mix_path = pop_path.replace("output", "output_stereo_mix") # ์คํ ๋ ์ค ๋ฏน์ค ํ์ผ ๊ฒฝ๋ก | |
sf.write(file=stereo_mix_path, data=stereo.T, samplerate=sr, format="wav") # ์คํ ๋ ์ค ๋ฏน์ค ํ์ผ ์์ฑ | |
return stereo_mix_path, stereo_mix_path # ์คํ ๋ ์ค ๋ฏน์ค ํ์ผ ๊ฒฝ๋ก ๋ฐํ | |
block = gr.Blocks(theme="Taithrah/Deep") # Gradio ๋ธ๋ก ์์ฑ | |
with block: | |
gr.HTML( | |
""" | |
<div style="text-align: center; max-width: 800px; margin: 0 auto;"> | |
<div | |
style=" | |
display: inline-flex; | |
align-items: center; | |
gap: 0.8rem; | |
font-size: 1.75rem; | |
" | |
> | |
<h1 style="font-weight: 900; margin-bottom: 12px;"> | |
๐น Pop2Piano : ํผ์๋ ธ ์ปค๋ฒ๊ณก ์์ฑ๊ธฐ ๐น | |
</h1> | |
</div> | |
<p style="margin-bottom: 10px; font-size: 90%"> | |
A demo for Pop2Piano: Pop Audio-based Piano Cover Generation. <br> | |
Please select the composer (Arranger) and upload the pop audio or enter the YouTube link and then click Generate. | |
</p> | |
</div> | |
""" | |
) | |
with gr.Group(): | |
with gr.Row(equal_height=True): | |
with gr.Column(): | |
file_uploaded = gr.Audio(label="์ค๋์ค ์ ๋ก๋", type="filepath") | |
with gr.Column(): | |
with gr.Row(): | |
yt_link = gr.Textbox(label="์ ํ๋ธ ๋งํฌ๋ฅผ ์ ๋ ฅํ์ธ์.", autofocus=True, lines=3) | |
yt_btn = gr.Button("์ ํ๋ธ ๋งํฌ์์ ์ค๋์ค๋ฅผ ๋ค์ด ๋ฐ์ต๋๋ค.", size="lg") | |
yt_audio_path = gr.Audio(label="์ ํ๋ธ ๋์์์์ ์ถ์ถํ ์ค๋์ค", interactive=False) | |
yt_btn.click(get_audio_from_yt_video, inputs=[yt_link], outputs=[yt_audio_path, file_uploaded]) | |
with gr.Group(): | |
with gr.Column(): | |
composer = gr.Dropdown(label="ํธ๊ณก์ ์ ํ", choices=composers, value="composer1") | |
generate_btn = gr.Button("๋๋ง์ ํผ์๋ ธ ์ปค๋ฒ๊ณก ๋ง๋ค๊ธฐ ๐น ๐ต") | |
with gr.Row().style(mobile_collapse=False, equal_height=True): | |
wav_output2 = gr.File(label="๋๋ง์ ํผ์๋ ธ ์ปค๋ฒ๊ณก์ ๋ค์ด๋ก๋ (.wav)") | |
wav_output1 = gr.Audio(label="๋๋ง์ ํผ์๋ ธ ์ปค๋ฒ๊ณก ๋ฃ๊ธฐ") | |
midi_output = gr.File(label="์์ฑํ midi ํ์ผ ๋ค์ด๋ก๋ (.mid)") | |
generate_btn.click( | |
inference, inputs=[file_uploaded, composer], outputs=[wav_output1, wav_output2, midi_output] | |
) | |
with gr.Group(): | |
gr.HTML( | |
""" | |
<div> <h3> <center> ์๋ณธ ์ค๋์ค์ MIDI๋ฅผ ํผํฉํ์ฌ ์คํ ๋ ์ค ๋ฏน์ค ํ๊ธฐ. </h3> </div> | |
""" | |
) | |
pop_scale = gr.Slider( | |
0, 1, value=0.5, label="์๊ณก๊ณผ MIDI ์ฌ์ด์์ ๋น์จ์ ์กฐ์ ํด ๋ณด์ธ์.", info="1.0 = ์๊ณก, 0.0=.mid", interactive=True | |
), | |
stereo_btn = gr.Button("Get Stereo Mix") | |
with gr.Row(): | |
stereo_mix1 = gr.Audio(label="์คํ ๋ ์ค ๋ฏน์ค ๋ฃ๊ธฐ") | |
stereo_mix2 = gr.File(label="์คํ ๋ ์ค ๋ฏน์ค ๋ค์ด๋ก๋") | |
stereo_btn.click(get_stereo, inputs=[file_uploaded, wav_output2, pop_scale[0]], outputs=[stereo_mix1, stereo_mix2]) | |
gr.HTML( | |
""" | |
<div class="footer"> | |
<center>The design for this Space is taken from DataBassist </a> | |
</div> | |
""" | |
) | |
gr.HTML( | |
""" | |
<div class="footer"> | |
<center><p><a href="http://sweetcocoa.github.io/pop2piano_samples" style="text-decoration: underline;" target="_blank">Project Page</a> | |
<center><a href="https://huggingface.co/docs/transformers/main/model_doc/pop2piano" style="text-decoration: underline;" target="_blank">HuggingFace Model Docs</a> | |
<center><a href="https://github.com/sweetcocoa/pop2piano" style="text-decoration: underline;" target="_blank">Github</a> | |
</p> | |
</div> | |
""" | |
) | |
block.launch(debug=False) |