ray-006 commited on
Commit
799ea32
·
verified ·
1 Parent(s): 85e1d13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -21
app.py CHANGED
@@ -1,57 +1,75 @@
1
  import gradio as gr
2
  import os
 
3
  from basic_pitch.inference import predict
4
 
5
- def convert_audio_to_midi(audio_filepath):
6
- """
7
- Takes an audio file path, runs Basic Pitch, and saves/returns a MIDI file.
8
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  if audio_filepath is None:
10
  return None
 
 
11
 
12
  try:
13
- # Run the basic pitch model on the audio file
14
- # predict() returns model_output, midi_data, and note_events
15
  _, midi_data, _ = predict(audio_filepath)
16
 
17
- # Define a temporary output path for the generated MIDI file
18
- output_midi_path = "output.mid"
 
19
 
20
- # Save the MIDI data to the file
21
  midi_data.write(output_midi_path)
22
 
23
- return output_midi_path
24
-
25
  except Exception as e:
26
- print(f"Error during conversion: {e}")
27
  return None
28
 
29
  # Build the Gradio Interface
30
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
31
  gr.Markdown(
32
  """
33
- # 🎵 Basic Pitch: Audio to MIDI Converter
34
- Upload an audio file (vocals, guitar, piano, etc.) and convert it into a MIDI file using Spotify's open-source **Basic Pitch** machine learning model.
 
35
  """
36
  )
37
 
38
  with gr.Row():
39
  with gr.Column():
40
- # User uploads audio; Gradio passes the file path to our function
41
- audio_input = gr.Audio(type="filepath", label="Upload Audio File (WAV, MP3, etc.)")
42
  convert_btn = gr.Button("Convert to MIDI", variant="primary")
43
 
44
  with gr.Column():
45
- # The output will be a downloadable file
46
- midi_output = gr.File(label="Download Generated MIDI")
47
 
48
- # Link the button click to the conversion function
49
  convert_btn.click(
50
  fn=convert_audio_to_midi,
51
  inputs=audio_input,
52
  outputs=midi_output
53
  )
54
 
55
- # Launch the app
56
  if __name__ == "__main__":
57
- demo.launch()
 
1
  import gradio as gr
2
  import os
3
+ import time
4
  from basic_pitch.inference import predict
5
 
6
+ # Configuration
7
+ OUTPUT_FOLDER = "midi_outputs"
8
+ CLEANUP_THRESHOLD_SECONDS = 60 # 1 minute
9
+
10
+ # Ensure the output directory exists
11
+ if not os.path.exists(OUTPUT_FOLDER):
12
+ os.makedirs(OUTPUT_FOLDER)
13
+
14
+ def cleanup_old_files():
15
+ """Removes MIDI files older than 1 minute from the output folder."""
16
+ now = time.time()
17
+ for filename in os.listdir(OUTPUT_FOLDER):
18
+ file_path = os.path.join(OUTPUT_FOLDER, filename)
19
+ # Check if it's a file and if it's older than our threshold
20
+ if os.path.isfile(file_path) and filename.endswith(".mid"):
21
+ file_age = now - os.path.getmtime(file_path)
22
+ if file_age > CLEANUP_THRESHOLD_SECONDS:
23
+ try:
24
+ os.remove(file_path)
25
+ print(f"Cleaned up old file: {filename}")
26
+ except Exception as e:
27
+ print(f"Failed to delete {filename}: {e}")
28
+
29
+ def convert_audio_to_midi(audio_filepath):
30
  if audio_filepath is None:
31
  return None
32
+
33
+ cleanup_old_files()
34
 
35
  try:
36
+ # predict returns (model_output, midi_data, note_events)
 
37
  _, midi_data, _ = predict(audio_filepath)
38
 
39
+ timestamp = int(time.time())
40
+ filename = f"output_{timestamp}.mid"
41
+ output_midi_path = os.path.join(OUTPUT_FOLDER, filename)
42
 
 
43
  midi_data.write(output_midi_path)
44
 
45
+ return output_midi_path # Gradio handles making this a downloadable URL
 
46
  except Exception as e:
47
+ print(f"Error: {e}")
48
  return None
49
 
50
  # Build the Gradio Interface
51
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
52
  gr.Markdown(
53
  """
54
+ # 🎵 MIDI Converter
55
+ Upload audio to convert to MIDI. Files are saved to the `/midi_outputs` folder.
56
+ **Note:** Outputs are automatically deleted after 1 minute to save space.
57
  """
58
  )
59
 
60
  with gr.Row():
61
  with gr.Column():
62
+ audio_input = gr.Audio(type="filepath", label="Upload Audio File")
 
63
  convert_btn = gr.Button("Convert to MIDI", variant="primary")
64
 
65
  with gr.Column():
66
+ midi_output = gr.File(label="Download MIDI (Expires in 60s)")
 
67
 
 
68
  convert_btn.click(
69
  fn=convert_audio_to_midi,
70
  inputs=audio_input,
71
  outputs=midi_output
72
  )
73
 
 
74
  if __name__ == "__main__":
75
+ demo.launch()