File size: 1,998 Bytes
59a45b1
750b282
 
 
90964bf
750b282
 
 
 
 
 
 
e510344
 
750b282
 
 
 
fb1d39e
750b282
 
 
 
d852991
 
5a73a18
d852991
 
5a73a18
 
d852991
5a73a18
d852991
 
 
 
750b282
 
 
 
 
 
d852991
5a73a18
750b282
 
 
 
 
 
 
 
5a73a18
 
750b282
d852991
750b282
 
 
e510344
750b282
 
 
 
 
 
 
 
 
 
 
 
e510344
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import spaces
import gradio as gr
import torch
from pyharp import ModelCard, build_endpoint, LabelList, AudioLabel, OutputLabel
from skey.key_detection import detect_key

# Check for GPU availability
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"

# Create a ModelCard
model_card = ModelCard(
    name="S-KEY Detection",
    description="Major/Minor Key detection from audio using Self-supervised Key Detection.",
    author="Deezer Research",
    tags=["key detection", "mir"],
)

# Define the process function
@spaces.GPU
def process_fn(input_audio_path):
    try:
        ext = input_audio_path.split(".")[-1].lower()

        detected_keys = detect_key(
            input_audio_path,
            extension=ext,
            device=DEVICE,
            cli=False
        )

        print("Raw output:", detected_keys)

        if not detected_keys:
            raise ValueError("No key detected")

        detected_key = detected_keys[0]  # because function returns list[str]

        labels = LabelList()
        labels.labels.append(
            AudioLabel(
                t=0.0,
                duration=5.0,
                label=detected_key,
                amplitude=0.8,
                description="Detected by S-KEY",
                color=OutputLabel.rgb_color_to_int(255, 100, 100, 0.8),
            )
        )

        return input_audio_path, labels

    except Exception as e:
        print("Error:", e)
        raise e


# Build Gradio Endpoint
with gr.Blocks() as demo:
    input_audio = gr.Audio(label="Input Audio", type="filepath").harp_required(True)
    # output_text = gr.Textbox(label="Detected Key")
    output_audio = gr.Audio(label="Output Audio", type="filepath")
    output_labels = gr.JSON(label="Analysis Labels")

    build_endpoint(
        model_card=model_card,
        process_fn=process_fn,
        input_components=[input_audio],
        output_components=[output_audio, output_labels],
    )


if __name__ == "__main__":
    demo.queue().launch()