Spaces:
Sleeping
Sleeping
Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import gradio as grfrom transformers import pipelineimport torchimport os
# --- Performance Improvement ---# 1. Determine the number of available CPU cores.num_cpu_cores = os.cpu_count()
# 2. Configure PyTorch to use all available CPU cores for its operations.# This is crucial for speeding up model inference on a CPU.if num_cpu_cores is not None: torch.set_num_threads(num_cpu_cores) print(f"✅ PyTorch is configured to use {num_cpu_cores} CPU cores.")else: print("Could not determine the number of CPU cores. Using default settings.")
# Initialize the audio classification pipeline# We specify device=-1 to explicitly enforce running on the CPU.pipe = pipeline( "audio-classification", model="MIT/ast-finetuned-audioset-10-10-0.4593", device=-1 # -1 for CPU, 0 for the first GPU, etc.)
# Define the function to classify an audio filedef classify_audio(audio_filepath): """ Takes an audio file path, classifies it using the pipeline, and returns a dictionary of top labels and their scores. """ if audio_filepath is None: return "Please upload an audio file first."
# The pipeline handles the loading, preprocessing, and inference. result = pipe(audio_filepath) # The output is formatted for the Gradio Label component. return {label['label']: label['score'] for label in result}
# Set up the Gradio interfaceapp = gr.Interface( fn=classify_audio, # Function to classify audio inputs=gr.Audio(type="filepath", label="Upload Audio"), # Input for uploading an audio file outputs=gr.Label(num_top_classes=3, label="Top 3 Predictions"), # Output with top 3 classification results title="High-Performance Audio Classification (CPU)", # App title description="Upload an audio file to classify it. This app is optimized to run on all available CPU cores.", examples=[ # You can add example audio files here if you have them locally # ["path/to/your/example_audio_1.wav"], # ["path/to/your/example_audio_2.mp3"], ])
# Launch the appif __name__ == "__main__": app.launch()app.pyAdded+57