shethjenil commited on
Commit
c160daf
·
verified ·
1 Parent(s): 02423dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -29
app.py CHANGED
@@ -1,45 +1,45 @@
1
- import streamlit as st
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(wav_file_path, model, batch_size=32, postprocessing='spotify'):
 
 
 
 
 
 
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, title
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
- # Streamlit UI
29
- st.title("Violin to MIDI Converter")
30
-
31
- uploaded_file = st.file_uploader("Upload your WAV file", type=["wav"])
32
-
33
- if uploaded_file is not None:
34
- st.write("File Uploaded Successfully!")
35
- st.audio(uploaded_file, format='audio/wav')
36
-
37
- if st.button("Convert to MIDI"):
38
- try:
39
- midi_file_name, title = transcribe_and_generate_midi(uploaded_file, model)
40
- st.success(f"MIDI file generated successfully: {midi_file_name}")
41
- st.audio(midi_file_name, format='audio/midi', label='Download MIDI')
42
- except Exception as e:
43
- st.error(f"Error: {str(e)}")
44
- else:
45
- st.info("Please upload a WAV file to convert to MIDI.")
 
 
 
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()