Spaces:
Running
Running
shethjenil
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,45 @@
|
|
1 |
-
import
|
2 |
import torch
|
3 |
from unidecode import unidecode
|
4 |
from musc.model import PretrainedModel
|
5 |
-
from unidecode import unidecode
|
6 |
-
import os
|
7 |
-
import sys
|
8 |
-
import torch
|
9 |
-
import json
|
10 |
-
#from yt_dlp import YoutubeDL
|
11 |
-
sys.path.append('MUSC_violin')
|
12 |
-
from MUSC_violin import musc
|
13 |
|
14 |
# Function to transcribe the WAV file and generate the MIDI file
|
15 |
-
def transcribe_and_generate_midi(
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
midi, _, title = model.transcribe_wav(wav_file_path, batch_size=batch_size, postprocessing=postprocessing)
|
17 |
|
18 |
# Write the MIDI file
|
19 |
midi_file_name = unidecode(title) + '.mid'
|
20 |
midi.write(midi_file_name)
|
21 |
|
22 |
-
return midi_file_name
|
23 |
|
24 |
# Set up the Pretrained Model
|
25 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
26 |
model = PretrainedModel(instrument='violin').to(device)
|
27 |
|
28 |
-
#
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
import torch
|
3 |
from unidecode import unidecode
|
4 |
from musc.model import PretrainedModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# Function to transcribe the WAV file and generate the MIDI file
|
7 |
+
def transcribe_and_generate_midi(wav_file, model, batch_size=32, postprocessing='spotify'):
|
8 |
+
# Save the uploaded WAV file to disk
|
9 |
+
wav_file_path = "temp_audio.wav"
|
10 |
+
with open(wav_file_path, "wb") as f:
|
11 |
+
f.write(wav_file.read())
|
12 |
+
|
13 |
+
# Transcribe the WAV file and generate MIDI
|
14 |
midi, _, title = model.transcribe_wav(wav_file_path, batch_size=batch_size, postprocessing=postprocessing)
|
15 |
|
16 |
# Write the MIDI file
|
17 |
midi_file_name = unidecode(title) + '.mid'
|
18 |
midi.write(midi_file_name)
|
19 |
|
20 |
+
return midi_file_name
|
21 |
|
22 |
# Set up the Pretrained Model
|
23 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
24 |
model = PretrainedModel(instrument='violin').to(device)
|
25 |
|
26 |
+
# Gradio interface function
|
27 |
+
def convert_to_midi(wav_file):
|
28 |
+
try:
|
29 |
+
midi_file_name = transcribe_and_generate_midi(wav_file, model)
|
30 |
+
return midi_file_name
|
31 |
+
except Exception as e:
|
32 |
+
return f"Error: {str(e)}"
|
33 |
+
|
34 |
+
# Create Gradio interface
|
35 |
+
iface = gr.Interface(
|
36 |
+
fn=convert_to_midi,
|
37 |
+
inputs=gr.inputs.File(label="Upload your WAV file"),
|
38 |
+
outputs=gr.outputs.File(label="Download MIDI file"),
|
39 |
+
live=False,
|
40 |
+
title="Violin to MIDI Converter",
|
41 |
+
description="Upload a WAV file of a violin performance, and it will be transcribed into MIDI format."
|
42 |
+
)
|
43 |
+
|
44 |
+
# Launch the Gradio app
|
45 |
+
iface.launch()
|