Huiran Yu commited on
Commit
0bfee8f
·
1 Parent(s): bfcbf77

Init commit

Browse files
Files changed (4) hide show
  1. .vscode/settings.json +5 -0
  2. README.md +1 -1
  3. app.py +102 -0
  4. requirements.txt +4 -0
.vscode/settings.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "python-envs.defaultEnvManager": "ms-python.python:conda",
3
+ "python-envs.defaultPackageManager": "ms-python.python:conda",
4
+ "python-envs.pythonProjects": []
5
+ }
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🏢
4
  colorFrom: red
5
  colorTo: gray
6
  sdk: gradio
7
- sdk_version: 6.5.1
8
  app_file: app.py
9
  pinned: false
10
  short_description: Beat detection for music
 
4
  colorFrom: red
5
  colorTo: gray
6
  sdk: gradio
7
+ sdk_version: 5.28.0
8
  app_file: app.py
9
  pinned: false
10
  short_description: Beat detection for music
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import uuid
4
+ from pathlib import Path
5
+ from contextlib import contextmanager
6
+
7
+ import numpy as np
8
+ import torch
9
+ import gradio as gr
10
+ import librosa
11
+
12
+ from BeatNet.BeatNet import BeatNet
13
+
14
+ from pyharp.core import ModelCard, build_endpoint
15
+ from pyharp.media.audio import save_audio
16
+ from pyharp import LabelList, AudioLabel, OutputLabel
17
+ from audiotools import AudioSignal
18
+
19
+ LOUDNESS_DB = -16.
20
+ SAMPLE_RATE = 48_000
21
+ ENCODEC_SAMPLE_RATE = 16_000
22
+ AUDIOSEAL_SAMPLE_RATE = 16_000
23
+
24
+ model_card = ModelCard(
25
+ name="BeatNet Beat Detection",
26
+ description=("Beat detection for audio."),
27
+ author="Mojtaba Heydari, Frank Cwitkowitz, Zhiyao Duan",
28
+ tags=["beat detection"]
29
+ )
30
+
31
+ print("Initializing BeatNet model...")
32
+ estimator = BeatNet(1, mode="offline", inference_model="DBN", plot=[], thread=False)
33
+
34
+ def load_audio(audio_path):
35
+ try:
36
+ wav, sr = librosa.load(audio_path, mono=True)
37
+ return wav, sr
38
+
39
+ except Exception as e:
40
+ print(f"Audio preprocessing failed: {e}")
41
+ raise ValueError(f"Failed to load audio: {str(e)}")
42
+
43
+ def process_fn(inp_audio):
44
+ audio_np, sr = load_audio(inp_audio)
45
+
46
+ print(f"sr: {sr}, audio shape: {audio_np.shape}")
47
+ if audio_np.ndim == 1:
48
+ audio_np = audio_np[None, None, :]
49
+ else:
50
+ audio_np = np.transpose(audio_np, (1, 0))[None, ...]
51
+
52
+ print(f"formatted audio: {audio_np.shape}")
53
+
54
+ output = estimator.process(audio_np)
55
+
56
+ output_labels = LabelList()
57
+
58
+ for t, b in output:
59
+ output_labels.labels.append(
60
+ AudioLabel(
61
+ t = t,
62
+ label = f"{b}",
63
+ description = f"Beat: {b}",
64
+ color = OutputLabel.rgb_color_to_int(),
65
+ amplitude = 1.0
66
+ )
67
+ )
68
+
69
+ return inp_audio, output_labels
70
+
71
+
72
+ with gr.Blocks() as app:
73
+ gr.Markdown("## BeatNet Beat Detection")
74
+
75
+ # Inputs
76
+ input_audio = gr.Audio(
77
+ label="Input Audio",
78
+ type="filepath",
79
+ sources=["upload", "microphone"]
80
+ )
81
+
82
+ # Outputs
83
+ output_wav = gr.Audio(
84
+ type="filepath",
85
+ label="Watermarked Speech"
86
+ )
87
+ output_label = gr.JSON(label="Watermark Confidence")
88
+
89
+ _ = build_endpoint(
90
+ model_card=model_card,
91
+ input_components=[
92
+ input_audio
93
+ ],
94
+ output_components=[
95
+ output_wav,
96
+ output_label
97
+ ],
98
+ process_fn=process_fn
99
+ )
100
+
101
+ if __name__ == '__main__':
102
+ app.launch(share=True, show_error=True, debug=True)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ git+https://github.com/TEAMuP-dev/pyharp.git@v0.3.0
2
+ librosa
3
+ madmom
4
+ BeatNet