RayPac006 commited on
Commit
26c7cfe
·
verified ·
1 Parent(s): b7659e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -15
app.py CHANGED
@@ -2,31 +2,54 @@ import gradio as gr
2
  from chord_extractor.extractors import Chordino
3
  import pandas as pd
4
 
5
- # Initialize the extractor
6
- # We do this outside the function so it only loads once when the app starts
7
  chordino = Chordino()
8
 
 
 
 
 
 
 
 
 
 
9
  def process_audio(audio_path):
10
  if audio_path is None:
11
  return None, "Please upload an audio file."
12
 
13
  try:
14
- # Extract chords from the file
15
- # chord-extractor handles the conversion internally
16
  extracted_chords = chordino.extract(audio_path)
17
 
18
- # Format the data for the Gradio Dataframe
19
- data = []
 
 
 
20
  for c in extracted_chords:
21
- # Rounding timestamp for better readability
22
- data.append([round(c.timestamp, 2), c.chord])
23
-
24
- return data, f"Successfully extracted {len(data)} chord changes."
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  except Exception as e:
27
  return None, f"Error: {str(e)}"
28
 
29
- # Define the Gradio Interface
30
  with gr.Blocks(title="Guitar Chord Extractor") as demo:
31
  gr.Markdown("# 🎸 Guitar Chord Extractor")
32
  gr.Markdown("Upload an audio file (mp3, wav, flac) to extract the chord progression using Chordino.")
@@ -39,8 +62,8 @@ with gr.Blocks(title="Guitar Chord Extractor") as demo:
39
  with gr.Column():
40
  status_output = gr.Textbox(label="Status")
41
  chord_output = gr.Dataframe(
42
- headers=["Timestamp (s)", "Chord"],
43
- datatype=["number", "string"],
44
  label="Extracted Progression"
45
  )
46
 
@@ -52,5 +75,4 @@ with gr.Blocks(title="Guitar Chord Extractor") as demo:
52
 
53
  # Launch the app
54
  if __name__ == "__main__":
55
- # server_name="0.0.0.0" is required for Hugging Face
56
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
2
  from chord_extractor.extractors import Chordino
3
  import pandas as pd
4
 
5
+ # Initialize the extractor once
 
6
  chordino = Chordino()
7
 
8
+ # Minimum duration to consider a chord "real"
9
+ MIN_DURATION = 0.5 # seconds
10
+
11
+ def format_timestamp(ts):
12
+ """Convert seconds to mm:ss format"""
13
+ minutes = int(ts // 60)
14
+ seconds = int(ts % 60)
15
+ return f"{minutes}:{seconds:02}"
16
+
17
  def process_audio(audio_path):
18
  if audio_path is None:
19
  return None, "Please upload an audio file."
20
 
21
  try:
22
+ # Extract chords
 
23
  extracted_chords = chordino.extract(audio_path)
24
 
25
+ # Filter out very short chords and format timestamps
26
+ filtered_data = []
27
+ prev_timestamp = None
28
+ prev_chord = None
29
+
30
  for c in extracted_chords:
31
+ # Always include the first chord
32
+ if prev_timestamp is None:
33
+ filtered_data.append([format_timestamp(c.timestamp), c.chord])
34
+ prev_timestamp = c.timestamp
35
+ prev_chord = c.chord
36
+ continue
37
+
38
+ # Calculate duration since last chord
39
+ duration = c.timestamp - prev_timestamp
40
+
41
+ # Only include if chord lasted at least MIN_DURATION or chord changed
42
+ if duration >= MIN_DURATION or c.chord != prev_chord:
43
+ filtered_data.append([format_timestamp(c.timestamp), c.chord])
44
+ prev_timestamp = c.timestamp
45
+ prev_chord = c.chord
46
+
47
+ return filtered_data, f"Successfully extracted {len(filtered_data)} chord changes."
48
 
49
  except Exception as e:
50
  return None, f"Error: {str(e)}"
51
 
52
+ # Define Gradio interface
53
  with gr.Blocks(title="Guitar Chord Extractor") as demo:
54
  gr.Markdown("# 🎸 Guitar Chord Extractor")
55
  gr.Markdown("Upload an audio file (mp3, wav, flac) to extract the chord progression using Chordino.")
 
62
  with gr.Column():
63
  status_output = gr.Textbox(label="Status")
64
  chord_output = gr.Dataframe(
65
+ headers=["Timestamp", "Chord"],
66
+ datatype=["string", "string"],
67
  label="Extracted Progression"
68
  )
69
 
 
75
 
76
  # Launch the app
77
  if __name__ == "__main__":
78
+ demo.launch(server_name="0.0.0.0", server_port=7860)