File size: 2,218 Bytes
2db8c7a
 
 
acdedca
2db8c7a
acdedca
 
 
2db8c7a
 
acdedca
 
2db8c7a
 
acdedca
 
 
 
2db8c7a
 
acdedca
2db8c7a
 
 
5721eb7
 
acdedca
 
 
2db8c7a
acdedca
2db8c7a
acdedca
 
2db8c7a
acdedca
 
2db8c7a
 
acdedca
 
 
 
2db8c7a
 
 
 
 
acdedca
2db8c7a
 
acdedca
 
 
 
 
 
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
# 1. Install the necessary libraries first:
# pip install gradio optimum[onnxruntime] transformers

import gradio as gr
from optimum.pipelines import pipeline # Use the pipeline from 'optimum'
import os

# --- Performance Improvement ---
# Configure ONNX Runtime to use all available CPU cores.
# This is done by setting the OMP_NUM_THREADS environment variable.
num_cpu_cores = os.cpu_count()
if num_cpu_cores is not None:
    os.environ["OMP_NUM_THREADS"] = str(num_cpu_cores)
    print(f"✅ ONNX Runtime configured to use {num_cpu_cores} CPU cores.")
else:
    print("Could not determine the number of CPU cores. Using default settings.")


# 2. Initialize the pipeline using the ONNX model from the Hub.
# 'optimum' handles downloading the model and running it with the specified accelerator.
pipe = pipeline(
    task="audio-classification",
    model="onnx-community/ast-finetuned-audioset-10-10-0.4593-ONNX",
    accelerator="ort",  # Specifies to use ONNX Runtime ('ort')
    device="cpu",       # Explicitly run on the CPU
    feature_extractor_kwargs={"use_fast": True}  # Silences the "slow processor" warning
)

# Define the function to classify an audio file
def classify_audio(audio_filepath):
    """
    Takes an audio file path, classifies it using the ONNX pipeline,
    and returns a dictionary of top labels and their scores.
    """
    if audio_filepath is None:
        return "Please upload an audio file first."

    # The 'optimum' pipeline works just like the 'transformers' one
    result = pipe(audio_filepath)
    return {label['label']: label['score'] for label in result}

# Set up the Gradio interface
app = gr.Interface(
    fn=classify_audio,
    inputs=gr.Audio(type="filepath", label="Upload Audio"),
    outputs=gr.Label(num_top_classes=3, label="Top 3 Predictions"),
    title="High-Performance Audio Classification with ONNX",
    description="Upload an audio file to classify it. This app uses a pre-optimized ONNX model and runs on all available CPU cores for maximum speed.",
    examples=[
        # You can add local example audio files here if you have them
        # ["path/to/example_cat_purr.wav"],
    ]
)

# Launch the app
if __name__ == "__main__":
    app.launch()