Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import torch
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# --- Performance Improvement ---
|
| 7 |
+
# 1. Determine the number of available CPU cores.
|
| 8 |
+
num_cpu_cores = os.cpu_count()
|
| 9 |
+
|
| 10 |
+
# 2. Configure PyTorch to use all available CPU cores for its operations.
|
| 11 |
+
# This is crucial for speeding up model inference on a CPU.
|
| 12 |
+
if num_cpu_cores is not None:
|
| 13 |
+
torch.set_num_threads(num_cpu_cores)
|
| 14 |
+
print(f"✅ PyTorch is configured to use {num_cpu_cores} CPU cores.")
|
| 15 |
+
else:
|
| 16 |
+
print("Could not determine the number of CPU cores. Using default settings.")
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Initialize the audio classification pipeline
|
| 20 |
+
# We specify device=-1 to explicitly enforce running on the CPU.
|
| 21 |
+
pipe = pipeline(
|
| 22 |
+
"audio-classification",
|
| 23 |
+
model="MIT/ast-finetuned-audioset-10-10-0.4593",
|
| 24 |
+
device=-1 # -1 for CPU, 0 for the first GPU, etc.
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# Define the function to classify an audio file
|
| 28 |
+
def classify_audio(audio_filepath):
|
| 29 |
+
"""
|
| 30 |
+
Takes an audio file path, classifies it using the pipeline,
|
| 31 |
+
and returns a dictionary of top labels and their scores.
|
| 32 |
+
"""
|
| 33 |
+
if audio_filepath is None:
|
| 34 |
+
return "Please upload an audio file first."
|
| 35 |
+
|
| 36 |
+
# The pipeline handles the loading, preprocessing, and inference.
|
| 37 |
+
result = pipe(audio_filepath)
|
| 38 |
+
# The output is formatted for the Gradio Label component.
|
| 39 |
+
return {label['label']: label['score'] for label in result}
|
| 40 |
+
|
| 41 |
+
# Set up the Gradio interface
|
| 42 |
+
app = gr.Interface(
|
| 43 |
+
fn=classify_audio, # Function to classify audio
|
| 44 |
+
inputs=gr.Audio(type="filepath", label="Upload Audio"), # Input for uploading an audio file
|
| 45 |
+
outputs=gr.Label(num_top_classes=3, label="Top 3 Predictions"), # Output with top 3 classification results
|
| 46 |
+
title="High-Performance Audio Classification (CPU)", # App title
|
| 47 |
+
description="Upload an audio file to classify it. This app is optimized to run on all available CPU cores.",
|
| 48 |
+
examples=[
|
| 49 |
+
# You can add example audio files here if you have them locally
|
| 50 |
+
# ["path/to/your/example_audio_1.wav"],
|
| 51 |
+
# ["path/to/your/example_audio_2.mp3"],
|
| 52 |
+
]
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Launch the app
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
app.launch()
|